mybatis的延迟加载

mybatis的懒加载:

应用场景: 查询订单信息,有时候需要关联查出用户信息。(提高mybatis的查询效率)。


<settings>
       //可以配置lazyLoadingEnabled 值为true,不设置aggressiveLazyLoading,为全局设置
     <setting name="lazyLoadingEnabled" value="true"/>
   
    <setting name="aggressiveLazyLoading" value="false"/>
settings>
xml version="1.0" encoding="UTF-8" ?>
DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ys.lazyload.OrdersMapper">
    
    <select id="getOrderByOrderId" resultMap="getOrderMap">
        select * from orders
    select>
    <resultMap type="com.ys.lazyload.Orders" id="getOrderMap">
        <id column="id" property="id"/>
        <result column="number" property="number"/>
        <association property="user" javaType="com.ys.lazyload.User"  column="user_id" select="getUserByUserId">association>
    resultMap>
    <select id="getUserByUserId" resultType="com.ys.lazyload.User">
        select * from user where id=#{id}
    select>  
mapper>

代码地址:https://github.com/forgeorgeo/myInfo.git    //(MybatisRelation懒加载demo)。

相关链接:https://www.cnblogs.com/ysocean/p/7336945.html

 

 

 

 

 

你可能感兴趣的:(mybatis的延迟加载)