Mybatis中的延时加载
来源:互联网 发布:win7网络发现无法启用 编辑:程序博客网 时间:2024/06/06 09:03
resultMap可以实现高级映射(使用association、collection实现一对一及一对多映射),association、collection具备延迟加载功能
延迟加载resultMap
使用association中的select指定延迟加载去执行的statement的id
实例:查询订单关联用户,用户信息延迟加载
全局配置:SqlMapConfig.xml中添加
<settings> <!-- 打开延迟加载得开关 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 将积极加载改为消极加载 即按需加载 --> <setting name="aggressiveLazyLoading" value="false"/></settings>
mapper中的配置:
<mapper namespace="dancheng.mybatis.mapper.OrdersMapperCustom"> <select id="findUserById" parameterType="int" resultType="user"> SELECT * FROM USER WHERE id=#{value} </select> <!-- 延迟加载的resultMap --> <resultMap type="dancheng.mybatis.po.Orders" id="OrdersUserLazyLoadingResultMap"> <!-- 对订单信息进行映射配置 --> <id column="id" property="id"/> <result column="user_id" property="userId" /> <result column="number" property="number" /> <result column="createtime" property="createtime" /> <result column="note" property="note" /> <!-- 对用户信息进行延迟加载 --> <association property="user" javaType="dancheng.mybatis.po.User" select="findUserById" column="user_id"> </association> </resultMap> <!-- 查询订单关联用户,用户信息需要延迟加载 --> <select id="findOrdersUserLazyLoading" resultMap="OrdersUserLazyLoadingResultMap"> SELECT * FROM orders </select></mapper>
接口定义:
public interface OrdersMapperCustom { public List<Orders> findOrdersUserLazyLoading() throws Exception;}
Test:
public class OrdersMapperCustomTest { private SqlSessionFactory sqlSessionFactory; @Before public void setUp() throws Exception { InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void testFindOrdersUserLazyLoading() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class); List<Orders> ordersList = ordersMapperCustom.findOrdersUserLazyLoading(); System.out.println(ordersList); sqlSession.close(); }}
0 0
- Mybatis中的延时加载
- mybatis 深度延时加载
- hibernate中的延时加载
- MyBatis入门(五)---延时加载、缓存
- MyBatis Review——延时加载
- IOS中的懒加载(延时加载)
- MyBatis中的延迟加载
- mybatis中的延迟加载
- 延时加载
- 延时加载
- 【MyBatis学习11】MyBatis中的延迟加载
- 【MyBatis学习11】MyBatis中的延迟加载
- 【MyBatis学习11】MyBatis中的延迟加载
- Mybatis学习(10)-MyBatis中的延迟加载
- 【MyBatis学习11】MyBatis中的延迟加载
- MyBatis中的懒加载设置
- 延时加载 懒加载
- 延时加载|懒加载
- 【JAVA】3.使用eclipse编写JAVA程序
- ogg-00869的一个乌龙事件
- 学学Java反射
- thinkphp连接sybase数据库
- Scanner类概述及其构造方法
- Mybatis中的延时加载
- MTK 65XX系列通过USB OTG添加Ethernet RJ45有线网卡支持
- Linux_Hi3518E开发_制作根文件系统镜像
- Ubuntu下安装beta版R报dpkg: dependency problems prevent configuration of r-recommended错误[已解决]
- Compound Words(复合词) UVA 10391
- OpenGL ES 2.0 笔记 - 特殊的内建变量
- 【WPF】【MVVM】把鼠标事件写到Controller层
- android源码集合989个实例 (从网上摘抄,在此记录下)
- 数组高级(排序和查找)