假设我有以下dto类:
class Item {
int id;
List<Detail> details;
@Override
public String toString() {
return "{id: " + id + ", details: " + details + "}";
}
}
class Detail {
String name;
String value;
@Override
public String toString() {
return "{" + name + ": " + value + "}";
}
}
是否可以编写mapper xml来检索具有正确填充详细信息的Items列表,并且将使用两个查询检索所有数据(1st表示项目,2nd表示详细信息)。在下面的示例中,将有N + 1个查询(N - 项目数)。
完整示例(用于示例模式,测试数据和用法)
Sandbox.java:
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;
import java.sql.*;
import java.sql.Statement;
import java.util.List;
public class Sandbox {
public static void main(String... args) throws Throwable {
try (Connection connection = DriverManager.getConnection("jdbc:sqlite:sample.db")) {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("drop table if exists Item");
statement.executeUpdate("create table Item (id integer)");
statement.executeUpdate("insert into Item values(1)");
statement.executeUpdate("insert into Item values(2)");
statement.executeUpdate("drop table if exists Detail");
statement.executeUpdate("create table Detail (id integer, name string, value string)");
statement.executeUpdate("insert into Detail values(1, 'name', 'foo')");
statement.executeUpdate("insert into Detail values(1, 'purpose', 'test')");
statement.executeUpdate("insert into Detail values(2, 'name', 'bar')");
}
}
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
try (SqlSession session = sqlSessionFactory.openSession()) {
MyMapper mapper = session.getMapper(MyMapper.class);
List<Item> items = mapper.selectItems();
System.out.println("items = " + items);
}
}
}
MyMapper.java:
import java.util.List;
public interface MyMapper {
List<Item> selectItems();
}
Mapper.xml:
<?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="MyMapper">
<resultMap id="ItemMap" type="Item">
<id column="id" property="id"/>
<collection column="id" property="details" select="selectDetails"/>
</resultMap>
<select id="selectItems" resultMap="ItemMap">
select * from Item
</select>
<select id="selectDetails" parameterType="int" resultType="Detail">
select * from Detail WHERE id=#{id}
</select>
</mapper>
mybatis-config.xml:
<?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"/>
<dataSource type="POOLED">
<property name="driver" value="net.sf.log4jdbc.DriverSpy"/>
<property name="url" value="jdbc:log4jdbc:sqlite:sample.db"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="Mapper.xml"/>
</mappers>
</configuration>
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>ru.urururu</groupId>
<artifactId>mybatis-batching</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.15.1</version>
</dependency>
<dependency>
<groupId>com.googlecode.log4jdbc</groupId>
<artifactId>log4jdbc</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:1)
如果您查看参考文档的 Mappers XML 中的Multiple ResultSets for Association部分,则会对其进行说明:
从版本3.2.3开始,MyBatis提供了另一种解决方法 N + 1问题。
某些数据库允许存储过程返回多个 结果集或一次执行多个语句并返回一个 每个结果集。这可以用于仅打一次数据库 并且在不使用连接的情况下返回相关数据。
有一个例子。因此,您需要一个包含查询的存储过程:
select * from Item
select * from Detail WHERE id=#{id}
然后select将调用存储过程作为下一个:
<select id="selectItems" resultSets="item,details" resultMap="ItemMap">
{call getItemAndDetails(#{id,jdbcType=INTEGER,mode=IN})}
</select>
最后是结果图:
指定将从名为“details”
的结果集中包含的数据中填充“details”集合
结果图中的集合标记将是下一个:
<collection property="details" ofType="Detail" resultSet="details" column="id" foreignColumn="foreign_id">