主要体现在关联查询上
一、开启延时加载
需要在核心配置文件中做两个必须的配置
<configuration>
<settings>
<!– 延迟加载总开关 –>
<setting name="lazyLoadingEnabled" value="true" />
<!– 禁止积极主动的加载 –>
<setting name="aggressiveLazyLoading" value="false" />
</settings>
二、一对多
对于Person来说
一条SQL语句查询Person的基本信息
一条SQL语句查询Person的订单(懒加载的语句)
1.在PersonMapper中
<resultMap type="com.yutouxiuxiu.model.Person" id="selectOrderByPersonIdLazyRM" extends="personRM">
<!–
column - 主SQL语句查询的结果集的某一字段作为参数传给子SQL语句
select - 子SQL语句 –>
<collection property="orderList" column="person_id" select="com.yutouxiuxiu.Orders.selectOrderByPersonId">
</collection>
</resultMap>
<select id="selectOrderByPersonIdLazy" parameterType="int" resultMap="selectOrderByPersonIdLazyRM">
select * from person where person_id = #{personId}
</select>
2.在OrdersMapper中
<!– 一对多的时候的延迟加载的子SQL语句 –>
<select id="selectOrderByPersonId" parameterType="int" resultMap="BaseResultMap">
select * from orders o where o.person_id = #{personId}
</select>
@Test
public void selectOrderByPersonIdLazy() {
// 获得session
SqlSession session = sqlSessionFactory.openSession();
try {
Person person = session.selectOne("com.yutouxiuxiu.Person.selectOrderByPersonIdLazy", 1);
// 发出查询person信息的SQL语句
System.out.println(person);
// 发出查询订单信息的SQL语句
System.out.println(person.getOrderList());
} finally {
session.close();
}
}
执行结果:
2014-02-11 11:17:30,550 [main] DEBUG [com.yutouxiuxiu.Person.selectOrderByPersonIdLazy] – ooo Using Connection [com.mysql.jdbc.Connection@4e94ac10]
2014-02-11 11:17:30,551 [main] DEBUG [com.yutouxiuxiu.Person.selectOrderByPersonIdLazy] – ==> Preparing: select * from person where person_id = ?
2014-02-11 11:17:30,602 [main] DEBUG [com.yutouxiuxiu.Person.selectOrderByPersonIdLazy] – ==> Parameters: 1(Integer)
2014-02-11 11:17:30,702 [main] DEBUG [com.yutouxiuxiu.Orders.selectOrderByPersonId] – ooo Using Connection [com.mysql.jdbc.Connection@4e94ac10]
2014-02-11 11:17:30,702 [main] DEBUG [com.yutouxiuxiu.Orders.selectOrderByPersonId] – ==> Preparing: select * from orders o where o.person_id = ?
2014-02-11 11:17:30,703 [main] DEBUG [com.yutouxiuxiu.Orders.selectOrderByPersonId] – ==> Parameters: 1(Integer)
Person [id=1, name=赵四, birthday=Mon Feb 10 00:00:00 CST 2014, address=象牙山, salary=1000, orderList=[Orders [orderId=1, orderSum=1000.0, orderTime=Sun Feb 09 16:28:26 CST 2014, personId=1, detialList=null, person=null], Orders [orderId=2, orderSum=200.0, orderTime=Sun Feb 09 09:09:00 CST 2014, personId=1, detialList=null, person=null]], roleList=null]
[Orders [orderId=1, orderSum=1000.0, orderTime=Sun Feb 09 16:28:26 CST 2014, personId=1, detialList=null, person=null], Orders [orderId=2, orderSum=200.0, orderTime=Sun Feb 09 09:09:00 CST 2014, personId=1, detialList=null, person=null]]
三、多对一
对于Orders来说
一条SQL语句查询Order是的基本信息
一条SQL语句查询Order的所属人(懒加载的语句)
1.Order的映射文件
<resultMap type="com.yutouxiuxiu.model.Orders" id="selectPersonByOrderIdLazyRM" extends="BaseResultMap">
<association property="person" column="person_id" select="com.yutouxiuxiu.Person.selectPersonByOrderId">
</association>
</resultMap>
<select id="selectPersonByOrderIdLazy" parameterType="int" resultMap="selectPersonByOrderIdLazyRM">
select * from orders where order_id = #{orderId}
</select>
2.Person的映射文件
<select id="selectPersonByOrderId">
select * from person where person_id = #{personId}
</select>
@Test
public void selectPersonByOrderIdLazy() {
// 获得session
SqlSession session = sqlSessionFactory.openSession();
try {
Orders orders = session.selectOne("com.yutouxiuxiu.Orders.selectPersonByOrderIdLazy", 1);
// 发出查询person信息的SQL语句
System.out.println(orders);
// 发出查询订单信息的SQL语句
System.out.println(orders.getPerson());
} finally {
session.close();
}
}
相关推荐
mybatis_plus 2.92,可破解,可使用,目前最新版3.0不好破解,此版本适合开发使用
MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, ...
如果你必须在一个旧版本的MyBatis运行,设置在配置属性“useLegacyBuilder”为“true”。 问题#11日 - Maven插件现在记录到日志行家(感谢保罗克劳斯) 问题#4 - Generator现在将抛出一个警告,如果任何列解析为一...
MyBatis Log Plugin 这款插件是直接将Mybatis执行的sql脚本显示出来,把 mybatis 输出的sql日志还原成完整的sql语句 1、本地安装 2、Tools 》mybatis_log_plugin
mybatis_plugin2.9.2
适用于idea的mybatis_plus插件,mybatis_plus插件的版本号为2.92 安装方法: 在IDEA中打开Settings-->Plugins-->Install plugin from disk...选择mybatis_plus.jar即可
mybatis_plus_2.6_crack破解版
mybatis_log_plugin_2020idea.jar
idea升级到2017 2.5 时手贱更新了 mybatis_plugin到3.15版本导致不能用了.现在把原插件删掉在放进去.就可以继续用了.
mybatis_plus idea逆向生成时用到的mybatis插件,版本为2.91的版本,需要下载对应 破解文件替换,idea反向生成才可以用
mybatis_plugin_注意说明文档中的mybatis_plus文件夹是最后的那个
IDEA 版本2.91的mybatis_plus.jar破解
尚硅谷_张晓飞_myBatis_视频源代码.zip,包括myBatis的所有视频和src
增加了程序员的一些操作,但是带来了设计上的灵活,并且也是支持hibernate的一些特性,如延迟加载,缓存和映射等;对数据库的兼容性比hibernate差。移植性不好,但是可编写灵活和高性能的sql语句。
mybatis_01 入门配置 Dao的两种开发方法,非常适合新手学习。
Mybatis和spring的整合,适合刚开始学的练练手
Mybatis-plugin 2.87最新版破解,完美支持idea2016.3,亲测亲破,收2点积分作为酬劳,望理解,谢谢.
生成已tk.mybatis封装的mybatis代码
MyBatis_demo.zip;MyBatis_demo.zip
MyBatis Generator