Mybatis_lazyLoadingEnabled延迟加载配置

Mybatis_lazyLoadingEnabled延迟加载配置

一、什么是延迟加载
  resultMap可以实现高级映射(使用association、collection实现一对一及一对多映射),association、collection具备延迟加载功能。

需求:如果查询订单并且关联查询用户信息。如果先查询订单信息即可满足要求,当我们需要查询用户信息时再查询用户信息。把对用户信息的按需去查询就是延迟加载。

延迟加载:先从单表查询、需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速度要快。

二、使用association实现延迟加载
需要定义两个mapper的方法对应的statement。

1、只查询订单信息

SELECT * FROM orders

在查询订单的statement中使用association去延迟加载(执行)下边的satatement(关联查询用户信息)

2、关联查询用户信息

通过上边查询到的订单信息中user_id去关联查询用户信息

OrderMapper.xml的延迟加载的核心代码:

使用association中的select指定延迟加载去执行的statement的id。

<!-- 查询订单关联查询用户,用户信息按需延迟加载 的 resultMap定义 -->
<resultMap type="com.mybatis.entity.Orders" id="ordersUserLazyLoading">
            <!--对订单信息进行映射配置  -->
        <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"/>
         <!-- 实现对用户信息进行延迟加载
            select:指定延迟加载需要执行的statement的id(是根据user_id查询用户信息的statement) 
            column:订单信息中关联用户信息查询的列,是user_id
            关联查询的sql理解为:
             SELECT orders.*,
                (SELECT username FROM USER WHERE orders.user_id = user.id)username,
                (SELECT sex FROM USER WHERE orders.user_id = user.id)sex
             FROM orders
        -->
        <association property="user"  javaType="com.mybatis.entity.User" select="findUserById" column="user_id"/>
</resultMap>
    <!-- 根据Id查询用户,用于测试延迟加载 -->
    <select id="findUserById" parameterType="int" resultType="com.mybatis.entity.User" >
            select * from t_user where id=#{id}
    </select>
<!-- 查询订单关联用户,用户信息延迟加载 -->
<select id="findOrdersUserLazyLoading" resultMap="ordersUserLazyLoading">
        select * from orders
</select>    

OrderMapper.java的代码:

public interface OrdersCustomMapper {
/*查询订单,关联查询用户,用户按需延迟加载/
public ListfindOrdersUserLazyLoading();
/**根据Id查询用户(这个方法本应该放在UserMapper类的,测试方便先放在这)*/
public User findUserById(int id);
}
测试思路及代码:

1、执行上边mapper方法(findOrdersUserLazyLoading),内部去调用OrdersMapperCustom中的findOrdersUserLazyLoading只查询orders信息(单表)。

2、在程序中去遍历上一步骤查询出的List,当我们调用Orders中的getUser方法时,开始进行延迟加载。

3、延迟加载,去调用findUserbyId这个方法获取用户信息。

// 查询用户关联查询用户,用户信息延迟加载
@Test
public void TestFindOrdersUserLazyLoading() {
SqlSession sqlSession = sqlSessionFactory.openSession();
// 创建代理对象
OrdersCustomMapper oc = sqlSession.getMapper(OrdersCustomMapper.class);
// 调用mapper的方法
List list = oc.findOrdersUserLazyLoading();
for(Orders order: list){
//执行getUser()去查询用户信息,这里实现延迟加载
User user = order.getUser();
System.out.println(user);
}
sqlSession.close();
}
三、延迟加载在mybatis核心配置文件sqlMapConfig.xml中的配置
mybatis默认没有开启延迟加载,需要在SqlMapConfig.xml中setting配置。

在mybatis核心配置文件中配置:

lazyLoadingEnabled、aggressiveLazyLoading

设置项

描述

允许值

默认值

lazyLoadingEnabled

全局性设置懒加载。如果设为‘false’,则所有相关联的都会被初始化加载。

true | false

false

aggressiveLazyLoading

当设置为‘true’的时候,懒加载的对象可能被任何懒属性全部加载。否则,每个属性都按需加载。

true | false

true

<settings>
        <!--打开延迟加载的开关  -->
    <setting name="lazyLoadingEnabled" value="true"/>
        <!--将积极加载改为消极加载及按需加载  -->
    <setting name="aggressiveLazyLoading" value="false"/>
</settings>

小结:使用延迟加载方法,先去查询简单的sql(最好单表,也可以关联查询),再去按需要加载关联查询的其它信息。

MyBatis 延迟加载 懒加载
主要体现在关联查询上

一、开启延时加载
需要在核心配置文件中做两个必须的配置

<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();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值