-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Mapping two independent result sets.
- Loading branch information
Showing
11 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Mapping two independent result sets. | ||
|
||
https://stackoverflow.com/q/73685798/1261766 | ||
|
||
This demo requires a running MS SQL Server instance. | ||
You may need to edit the data source settings in /src/test/resources/test/mybatis-config.xml. | ||
|
||
Tested with | ||
|
||
- Microsoft SQL Server 15.00.4073 | ||
- mssql-jdbc 11.1.2.jre8-preview | ||
|
||
```sh | ||
$ svn export https://github.com/harawata/mybatis-issues/trunk/so-73685798 | ||
$ cd so-73685798 | ||
$ mvn test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.mybatis.issues</groupId> | ||
<artifactId>so-73685798</artifactId> | ||
<packaging>jar</packaging> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<version>2.18.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.microsoft.sqlserver</groupId> | ||
<artifactId>mssql-jdbc</artifactId> | ||
<version>11.1.2.jre8-preview</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mybatis</groupId> | ||
<artifactId>mybatis</artifactId> | ||
<version>3.5.11</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package test; | ||
|
||
public class Artist { | ||
private Integer id; | ||
private String name; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package test; | ||
|
||
import java.util.List; | ||
|
||
public class DivideOut { | ||
public List<Movie> movie; | ||
public List<Artist> artist; | ||
|
||
public List<Movie> getMovie() { | ||
return movie; | ||
} | ||
|
||
public void setMovie(List<Movie> movie) { | ||
this.movie = movie; | ||
} | ||
|
||
public List<Artist> getArtist() { | ||
return artist; | ||
} | ||
|
||
public void setArtist(List<Artist> artist) { | ||
this.artist = artist; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package test; | ||
|
||
import java.util.List; | ||
|
||
public interface Mapper { | ||
DivideOut selectMultiple(); | ||
|
||
List<List<?>> selectTwoLists(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package test; | ||
|
||
public class Movie { | ||
private Integer id; | ||
private String name; | ||
private Integer year; | ||
|
||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Integer getYear() { | ||
return year; | ||
} | ||
|
||
public void setYear(Integer year) { | ||
this.year = year; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.io.Reader; | ||
import java.sql.Connection; | ||
import java.util.List; | ||
|
||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.jdbc.ScriptRunner; | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class SimpleTest { | ||
|
||
private static SqlSessionFactory sqlSessionFactory; | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
// create an SqlSessionFactory | ||
try (Reader reader = Resources.getResourceAsReader("test/mybatis-config.xml")) { | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); | ||
} | ||
// prepare in-memory database | ||
try (SqlSession session = sqlSessionFactory.openSession(); | ||
Connection conn = session.getConnection(); | ||
Reader reader = Resources.getResourceAsReader("test/CreateDB.sql")) { | ||
ScriptRunner runner = new ScriptRunner(conn); | ||
runner.setLogWriter(null); | ||
runner.runScript(reader); | ||
} | ||
} | ||
|
||
@Test | ||
public void getOneDivideOut() { | ||
try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
DivideOut out = mapper.selectMultiple(); | ||
List<Movie> movies = out.movie; | ||
assertEquals(3, movies.size()); | ||
assertEquals("Movie1", movies.get(0).getName()); | ||
assertEquals("Movie2", movies.get(1).getName()); | ||
assertEquals("Movie3", movies.get(2).getName()); | ||
List<Artist> artists = out.artist; | ||
assertEquals(2, artists.size()); | ||
assertEquals("John", artists.get(0).getName()); | ||
assertEquals("Jane", artists.get(1).getName()); | ||
} | ||
} | ||
|
||
@Test | ||
public void getTwoLists() { | ||
try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
List<List<?>> lists = mapper.selectTwoLists(); | ||
@SuppressWarnings("unchecked") | ||
List<Movie> movies = (List<Movie>) lists.get(0); | ||
assertEquals(3, movies.size()); | ||
assertEquals("Movie1", movies.get(0).getName()); | ||
assertEquals("Movie2", movies.get(1).getName()); | ||
assertEquals("Movie3", movies.get(2).getName()); | ||
@SuppressWarnings("unchecked") | ||
List<Artist> artists = (List<Artist>) lists.get(1); | ||
assertEquals(2, artists.size()); | ||
assertEquals("John", artists.get(0).getName()); | ||
assertEquals("Jane", artists.get(1).getName()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<Configuration> | ||
<Appenders> | ||
<Console name="stdout" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="%5p [%t] - %m%n" charset="utf-8" /> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Root level="trace"> | ||
<AppenderRef ref="stdout" /> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
drop table if exists Movie; | ||
drop table if exists Artist; | ||
|
||
create table Movie ( | ||
id_ int, | ||
name_ varchar(20), | ||
year_ int | ||
); | ||
create table Artist ( | ||
id_ int, | ||
name_ varchar(20) | ||
); | ||
|
||
insert into Movie (id_, name_, year_) values (1, 'Movie1', 2020); | ||
insert into Movie (id_, name_, year_) values (2, 'Movie2', 2008); | ||
insert into Movie (id_, name_, year_) values (3, 'Movie3', 1988); | ||
|
||
insert into Artist (id_, name_) values (1, 'John'); | ||
insert into Artist (id_, name_) values (2, 'Jane'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE mapper | ||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
|
||
<mapper namespace="test.Mapper"> | ||
|
||
<resultMap id="movieResult" type="test.Movie"> | ||
<id property="id" column="id"/> | ||
<result property="name" column="name"/> | ||
<result property="year" column="year"/> | ||
</resultMap> | ||
|
||
<resultMap id="artistResult" type="test.Artist"> | ||
<id property="id" column="id"/> | ||
<result property="name" column="name"/> | ||
</resultMap> | ||
|
||
<resultMap id="multipleQueriesResult" type="test.DivideOut" autoMapping="true"> | ||
<id column="parentId" /> | ||
<collection property="movie" | ||
resultSet="movies" | ||
resultMap="movieResult" /> | ||
<collection property="artist" | ||
resultSet="artists" | ||
resultMap="artistResult" /> | ||
</resultMap> | ||
|
||
<select id="selectMultiple" resultSets="dummy,movies,artists" | ||
resultMap="multipleQueriesResult" statementType="CALLABLE"> | ||
BEGIN | ||
SELECT 1 AS parentId | ||
END | ||
BEGIN | ||
SELECT | ||
M.ID_ AS id, | ||
M.NAME_ AS name, | ||
M.YEAR_ AS year | ||
FROM Movie AS M | ||
END | ||
BEGIN | ||
SELECT A.ID_ AS id, | ||
A.NAME_ AS name | ||
FROM Artist AS A | ||
END | ||
</select> | ||
|
||
<select id="selectTwoLists" resultMap="movieResult,artistResult" | ||
statementType="CALLABLE"> | ||
BEGIN | ||
SELECT | ||
M.ID_ AS id, | ||
M.NAME_ AS name, | ||
M.YEAR_ AS year | ||
FROM Movie AS M | ||
END | ||
BEGIN | ||
SELECT A.ID_ AS id, | ||
A.NAME_ AS name | ||
FROM Artist AS A | ||
END | ||
</select> | ||
|
||
</mapper> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!DOCTYPE configuration | ||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-config.dtd"> | ||
|
||
<configuration> | ||
|
||
<environments default="development"> | ||
<environment id="development"> | ||
<transactionManager type="JDBC"> | ||
<property name="" value="" /> | ||
</transactionManager> | ||
<dataSource type="UNPOOLED"> | ||
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> | ||
<property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=test;encrypt=false" /> | ||
<property name="username" value="sa" /> | ||
<property name="password" value="Abcdefg7" /> | ||
</dataSource> | ||
</environment> | ||
</environments> | ||
|
||
<mappers> | ||
<mapper class="test.Mapper" /> | ||
</mappers> | ||
|
||
</configuration> |