说明:Orders订单表,Person人员表,Order_details订单明细表,从mybatis一对多查询中已经说明。
1.开启延迟加载(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>
<!--开启延迟加载
注意:有顺序问题,必须放在最前面
会报错误:最下面有解决方法
-->
<settings>
<!--
lazyLoadingEnabled:延迟加载的总开关
aggressiveLazyLoading:启用延迟加载
-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<!--自定义别名-->
<typeAliases>
<!--type: 要被定义成别名的数据类型-->
<typeAlias type="zhou.model.Person" alias="person"/>
</typeAliases>
<!--
数据库环境配置如果和spring整合就不需要配了,完全交给spring
default:决定你要用哪个数据源
-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
<!-- <environment id="oracle">-->
<!-- <transactionManager type="JDBC"/>-->
<!-- <dataSource type="POOLED">-->
<!-- <property name="driver" value="${driver}"/>-->
<!-- <property name="url" value="${url}"/>-->
<!-- <property name="username" value="${username}"/>-->
<!-- <property name="password" value="${password}"/>-->
<!-- </dataSource>-->
<!-- </environment>-->
</environments>
<!--
管理每张表的映射文件
resource: 引入映射文件,注意包之间用/
-->
<mappers>
<mapper resource="mappings/PersonMapper.xml"/>
<mapper resource="mappings/OrdersMapper.xml"/>
<mapper resource="mappings/OrderDetailsMapper.xml"/>
<mapper resource="mappings/PersonRoleMapper.xml"/>
<mapper resource="mappings/RoleMapper.xml"/>
<mapper resource="mappings/IdCardMapper.xml"/>
</mappers>
</configuration>
2.一对多延迟加载
(1)PersonMapper.xml
<!-- 延迟加载 -->
<resultMap id="selectPersonByIdLazyRM" type="person" extends="BaseResultMap">
<!--
column:主sql查询出来的结果中的某一列作为子sql的参数
select:子sql中的位置
-->
<collection property="orderList" column="id" select="mappings.OrdersMapper.selectOrdersByPersonIdLazy"></collection>
</resultMap>
<!--主sql -->
<select id="selectPersonByIdLazy" parameterType="int" resultMap="selectPersonByIdLazyRM">
select * from Person t where t.id = #{id}
</select>
(2) OrdersMapper.xml
<!--子sql -->
<select id="selectOrdersByPersonIdLazy" parameterType="int" resultMap="BaseResultMap">
select * from orders t where t.order_id = #{pid}
</select>
3.多对一延迟加载
(1) OrdersMapper.xml
<resultMap id="selectOrdersByIdLazyRM" type="zhou.model.Orders" extends="BaseResultMap">
<association property="person" column="id" select="mappings.PersonMapper.selectPersonById">
</association>
</resultMap>
<!--主sql -->
<select id="selectOrdersByIdLazy" parameterType="int" resultMap="selectOrdersByIdLazyRM">
select * from orders where t.order_id = #{orderId}
</select>
(2) PersonMapper.xml
<!--子sql -->
<select id="selectPersonById" parameterType="int" resultMap="BaseResultMap">
select * from Person where id = #{id}
</select>
4. 一对多和多对一联合查询延迟加载
(1)OrderDetailsMapper.xml
<!--子sql -->
<select id="selectDetailByOrderIdLazy" parameterType="int" resultMap="BaseResultMap">
select * from order_details t where t.oid = #{oid}
</select>
(2)OrdersMapper.xml
<!--上面已经写过多对一得延迟加载得配置了,在这里直接继承即可-->
<resultMap id="selectPersonAndDetailByOrdersByIdLazyRM" type="zhou.model.Orders" extends="selectOrdersByIdLazyRM">
<collection property="detailList" column="order_id"
select="mappings.OrderDetailsMapper.selectDetailByOrderIdLazy"></collection>
</resultMap>
<!--主sql -->
<select id="selectPersonAndDetailByOrdersByIdLazy" parameterType="int"
resultMap="selectPersonAndDetailByOrdersByIdLazyRM">
select * from orders t where t.order_id = #{orderId}
</select>
5.测试
//一对多延迟加载
@Test
public void selectPersonByIdLazy() {
SqlSession session = sessionFactory.openSession();
try {
// 发出主sql
Orders orders = session.selectOne("mappings.OrdersMapper.selectPersonByIdLazy", 1);
// 发出子sql
System.out.println(orders.getPerson());
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
//多对一延迟加载
@Test
public void selectOrdersByIdLazy() {
SqlSession session = sessionFactory.openSession();
try {
// 发出主sql
Person person = session.selectOne("mappings.PersonMapper.selectOrdersByIdLazy", 1);
// 发出子sql
System.out.println(person.getOrderList());
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
// 联合
@Test
public void selectPersonAndDetailByOrdersByIdLazy() {
SqlSession session = sessionFactory.openSession();
try {
// 发出主sql
Orders orders = session.selectOne("mappings.OrdersMapper.selectPersonAndDetailByOrdersByIdLazy", 1);
// 发出子sql
System.out.println(orders.getDetailList());
System.out.println(orders.getPerson());
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
异常:
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in SQL Mapper Configuration
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.IllegalStateException: Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:79)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:63)
at zhou.AppTest02.setUp(AppTest02.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.IllegalStateException: Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:105)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:88)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:77)
... 24 more
Caused by: java.lang.IllegalStateException: Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.
at org.apache.ibatis.session.Configuration.setLazyLoadingEnabled(Configuration.java:211)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.settingsElement(XMLConfigBuilder.java:197)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:99)
... 26 more
Caused by: java.lang.ClassNotFoundException: Cannot find class: net.sf.cglib.proxy.Enhancer
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:188)
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:87)
at org.apache.ibatis.io.Resources.classForName(Resources.java:250)
at org.apache.ibatis.session.Configuration.setLazyLoadingEnabled(Configuration.java:209)
... 28 more
解决办法:pom文件中加入以下内容
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.1</version>
</dependency>