Skip to content

Commit 41c5250

Browse files
committed
Mapping two independent result sets.
1 parent 4e71e5a commit 41c5250

File tree

11 files changed

+336
-0
lines changed

11 files changed

+336
-0
lines changed

so-73685798/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Mapping two independent result sets.
2+
3+
https://stackoverflow.com/q/73685798/1261766
4+
5+
This demo requires a running MS SQL Server instance.
6+
You may need to edit the data source settings in /src/test/resources/test/mybatis-config.xml.
7+
8+
Tested with
9+
10+
- Microsoft SQL Server 15.00.4073
11+
- mssql-jdbc 11.1.2.jre8-preview
12+
13+
```sh
14+
$ svn export https://github.com/harawata/mybatis-issues/trunk/so-73685798
15+
$ cd so-73685798
16+
$ mvn test
17+
```

so-73685798/pom.xml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.mybatis.issues</groupId>
5+
<artifactId>so-73685798</artifactId>
6+
<packaging>jar</packaging>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
</properties>
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.apache.logging.log4j</groupId>
17+
<artifactId>log4j-core</artifactId>
18+
<version>2.18.0</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>4.13.2</version>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.microsoft.sqlserver</groupId>
28+
<artifactId>mssql-jdbc</artifactId>
29+
<version>11.1.2.jre8-preview</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.mybatis</groupId>
33+
<artifactId>mybatis</artifactId>
34+
<version>3.5.11</version>
35+
</dependency>
36+
</dependencies>
37+
</project>
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test;
2+
3+
public class Artist {
4+
private Integer id;
5+
private String name;
6+
7+
public Integer getId() {
8+
return id;
9+
}
10+
11+
public void setId(Integer id) {
12+
this.id = id;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package test;
2+
3+
import java.util.List;
4+
5+
public class DivideOut {
6+
public List<Movie> movie;
7+
public List<Artist> artist;
8+
9+
public List<Movie> getMovie() {
10+
return movie;
11+
}
12+
13+
public void setMovie(List<Movie> movie) {
14+
this.movie = movie;
15+
}
16+
17+
public List<Artist> getArtist() {
18+
return artist;
19+
}
20+
21+
public void setArtist(List<Artist> artist) {
22+
this.artist = artist;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package test;
2+
3+
import java.util.List;
4+
5+
public interface Mapper {
6+
DivideOut selectMultiple();
7+
8+
List<List<?>> selectTwoLists();
9+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package test;
2+
3+
public class Movie {
4+
private Integer id;
5+
private String name;
6+
private Integer year;
7+
8+
9+
public Integer getId() {
10+
return id;
11+
}
12+
13+
public void setId(Integer id) {
14+
this.id = id;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public Integer getYear() {
26+
return year;
27+
}
28+
29+
public void setYear(Integer year) {
30+
this.year = year;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package test;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.Reader;
6+
import java.sql.Connection;
7+
import java.util.List;
8+
9+
import org.apache.ibatis.io.Resources;
10+
import org.apache.ibatis.jdbc.ScriptRunner;
11+
import org.apache.ibatis.session.SqlSession;
12+
import org.apache.ibatis.session.SqlSessionFactory;
13+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
17+
public class SimpleTest {
18+
19+
private static SqlSessionFactory sqlSessionFactory;
20+
21+
@BeforeClass
22+
public static void setUp() throws Exception {
23+
// create an SqlSessionFactory
24+
try (Reader reader = Resources.getResourceAsReader("test/mybatis-config.xml")) {
25+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
26+
}
27+
// prepare in-memory database
28+
try (SqlSession session = sqlSessionFactory.openSession();
29+
Connection conn = session.getConnection();
30+
Reader reader = Resources.getResourceAsReader("test/CreateDB.sql")) {
31+
ScriptRunner runner = new ScriptRunner(conn);
32+
runner.setLogWriter(null);
33+
runner.runScript(reader);
34+
}
35+
}
36+
37+
@Test
38+
public void getOneDivideOut() {
39+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
40+
Mapper mapper = sqlSession.getMapper(Mapper.class);
41+
DivideOut out = mapper.selectMultiple();
42+
List<Movie> movies = out.movie;
43+
assertEquals(3, movies.size());
44+
assertEquals("Movie1", movies.get(0).getName());
45+
assertEquals("Movie2", movies.get(1).getName());
46+
assertEquals("Movie3", movies.get(2).getName());
47+
List<Artist> artists = out.artist;
48+
assertEquals(2, artists.size());
49+
assertEquals("John", artists.get(0).getName());
50+
assertEquals("Jane", artists.get(1).getName());
51+
}
52+
}
53+
54+
@Test
55+
public void getTwoLists() {
56+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
57+
Mapper mapper = sqlSession.getMapper(Mapper.class);
58+
List<List<?>> lists = mapper.selectTwoLists();
59+
@SuppressWarnings("unchecked")
60+
List<Movie> movies = (List<Movie>) lists.get(0);
61+
assertEquals(3, movies.size());
62+
assertEquals("Movie1", movies.get(0).getName());
63+
assertEquals("Movie2", movies.get(1).getName());
64+
assertEquals("Movie3", movies.get(2).getName());
65+
@SuppressWarnings("unchecked")
66+
List<Artist> artists = (List<Artist>) lists.get(1);
67+
assertEquals(2, artists.size());
68+
assertEquals("John", artists.get(0).getName());
69+
assertEquals("Jane", artists.get(1).getName());
70+
}
71+
}
72+
73+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Configuration>
3+
<Appenders>
4+
<Console name="stdout" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%5p [%t] - %m%n" charset="utf-8" />
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="trace">
10+
<AppenderRef ref="stdout" />
11+
</Root>
12+
</Loggers>
13+
</Configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
drop table if exists Movie;
2+
drop table if exists Artist;
3+
4+
create table Movie (
5+
id_ int,
6+
name_ varchar(20),
7+
year_ int
8+
);
9+
create table Artist (
10+
id_ int,
11+
name_ varchar(20)
12+
);
13+
14+
insert into Movie (id_, name_, year_) values (1, 'Movie1', 2020);
15+
insert into Movie (id_, name_, year_) values (2, 'Movie2', 2008);
16+
insert into Movie (id_, name_, year_) values (3, 'Movie3', 1988);
17+
18+
insert into Artist (id_, name_) values (1, 'John');
19+
insert into Artist (id_, name_) values (2, 'Jane');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE mapper
3+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5+
6+
<mapper namespace="test.Mapper">
7+
8+
<resultMap id="movieResult" type="test.Movie">
9+
<id property="id" column="id"/>
10+
<result property="name" column="name"/>
11+
<result property="year" column="year"/>
12+
</resultMap>
13+
14+
<resultMap id="artistResult" type="test.Artist">
15+
<id property="id" column="id"/>
16+
<result property="name" column="name"/>
17+
</resultMap>
18+
19+
<resultMap id="multipleQueriesResult" type="test.DivideOut" autoMapping="true">
20+
<id column="parentId" />
21+
<collection property="movie"
22+
resultSet="movies"
23+
resultMap="movieResult" />
24+
<collection property="artist"
25+
resultSet="artists"
26+
resultMap="artistResult" />
27+
</resultMap>
28+
29+
<select id="selectMultiple" resultSets="dummy,movies,artists"
30+
resultMap="multipleQueriesResult" statementType="CALLABLE">
31+
BEGIN
32+
SELECT 1 AS parentId
33+
END
34+
BEGIN
35+
SELECT
36+
M.ID_ AS id,
37+
M.NAME_ AS name,
38+
M.YEAR_ AS year
39+
FROM Movie AS M
40+
END
41+
BEGIN
42+
SELECT A.ID_ AS id,
43+
A.NAME_ AS name
44+
FROM Artist AS A
45+
END
46+
</select>
47+
48+
<select id="selectTwoLists" resultMap="movieResult,artistResult"
49+
statementType="CALLABLE">
50+
BEGIN
51+
SELECT
52+
M.ID_ AS id,
53+
M.NAME_ AS name,
54+
M.YEAR_ AS year
55+
FROM Movie AS M
56+
END
57+
BEGIN
58+
SELECT A.ID_ AS id,
59+
A.NAME_ AS name
60+
FROM Artist AS A
61+
END
62+
</select>
63+
64+
</mapper>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE configuration
3+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
5+
6+
<configuration>
7+
8+
<environments default="development">
9+
<environment id="development">
10+
<transactionManager type="JDBC">
11+
<property name="" value="" />
12+
</transactionManager>
13+
<dataSource type="UNPOOLED">
14+
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
15+
<property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=test;encrypt=false" />
16+
<property name="username" value="sa" />
17+
<property name="password" value="Abcdefg7" />
18+
</dataSource>
19+
</environment>
20+
</environments>
21+
22+
<mappers>
23+
<mapper class="test.Mapper" />
24+
</mappers>
25+
26+
</configuration>

0 commit comments

Comments
 (0)