mybatis的懒加载:
应用场景: 查询订单信息,有时候需要关联查出用户信息。(提高mybatis的查询效率)。
<!-- 开启懒加载配置 --> <settings> <!-- 全局性设置懒加载。如果设为‘false',则所有相关联的都会被初始化加载。 --> //可以配置lazyLoadingEnabled 值为true,不设置aggressiveLazyLoading,为全局设置 <setting name="lazyLoadingEnabled" value="true"/> <!-- 当设置为‘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
来源:https://www.cnblogs.com/lgg20/p/12030170.html