当你要使用one to one,many to one 就会碰到N+1的问题。很明显,对象之间关联很多有A关联B,B关联C,C关联A这样的关系,如果不是采用延迟加载,很容易一下在出现成千上万对象,造成N+1的问题。
而Mybatis 设置延迟加载主要2个属性配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
</configuration>
lazyLoadingEnabled:true使用延迟加载,false禁用延迟加载。默认为true
aggressiveLazyLoading:true启用时,当延迟加载开启时访问对象中一个懒对象属性时,将完全加载这个对象的所有懒对象属性。false,当延迟加载时,按需加载对象属性(即访问对象中一个懒对象属性,不会加载对象中其他的懒对象属性)。默认为true
加载所有懒对象
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
</configuration>
按需加载对象属性
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
</configuration>
对继承spring配置如下:
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:setting.xml"/>
<property name="dataSource" ref="dataSource" />
</bean>
setting.xml是mybatis全局属性配置文件。
分享到:
相关推荐
mybatis 延迟加载样例,附uml图说明
spring boot中配置mybatis xml资源文件热加载的方法以及相关文件
Mybatis延迟加载和缓存
mybatis+spring 框架中配置日志中显示sql语句
spring boot中配置mybatis xml资源文件热加载的方法以及相关文件
一般使用mybatis的环境,大多都是别人已经配置...我文章中的方法不基于spring,一般很少会在真实项目中直接使用。我把我的搭建过程记录下来给有用的人吧。 https://blog.csdn.net/lxyoucan/article/details/126162493
MyBatis懒加载的Demo
一个简单的mybatis 实例项目,包含基本的增删改查 和 关联操作。未整合 spring。使用 maven 管理。
SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+...
mybatis-plus学习,spring和mybatis-plus集成配置
SpringMVC+Spring+Mybatis集成开发环境
Mybatis框架 struts2框架 spring框架集成 mysql脚本 完整项目 实现增删改查
该项目通过springcloud集成了mybatis操作mysql数据库,项目主要分为7个model模块,配置模块(config),注册模块(discovery),监控模块(monitor),服务模块_mybatisService(通过mybatis操作mysql),服务模块_...
spring整合mybatis时需要用到的配置文件
赠送jar包:mybatis-spring-2.0.0.jar; 赠送原API文档:mybatis-spring-2.0.0-javadoc.jar; 赠送源代码:mybatis-spring-2.0.0-sources.jar; 赠送Maven依赖信息文件:mybatis-spring-2.0.0.pom; 包含翻译后的API...
Mybatis延迟加载和缓存的相关配置和使用。案例中代码演示了使用步骤,包含详细注释)
这是一个web程序的后台代码,整合了spring mvc和mybatis的配置,实现了各地方的电量增删改查,该代码持久层利用mybatis框架,简化了dao层的实现,spring实现了controller层、service层。
使用这个类库中的类, Spring 将会加载必要的 MyBatis 工厂类和 session 类。 这个类库也提供一个简单的方式来注入 MyBatis 数据映射器和 SqlSession 到业务层的 bean 中。 而且它也会处理事务, 翻译 MyBatis 的异常...
mybatis-spring1.2的最新中文API CHM
该jar包是Mybatis和Spring整合必备jar包,适配Mybatis-3.4.0及以上版本。