一、延迟加载的两种配置方式(全局配置/resultMap配置)
1、全局配置
在mybatis-config.xml的文件中增加setting的配置标签
<settings> <!--打开延迟加载的开关 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 当为true的时候,调用任意延迟属性,会去加载所有延迟属性,如果为false,则调用某个属性的时候,只会加载指定的属性 --> <setting name="aggressiveLazyLoading" value="false"/> </settings>
resultMap配置
<resultMap id="BaseResultMap7" type="entity.TOrder"> <id column="id" property="id" /> <result column="user_id" property="userId" /> <result column="create_time" property="createTime" /> <result column="up_time" property="upTime" /> <association property="tUser" javaType="entity.TUser" column="user_id" select="dao.TUserDao.selectById"/> <collection property="orderDetailList" ofType="entity.TOrderDetail" column="id" select="dao.TOrderDetailDao.selectByOrderid"/> </resultMap>
TOrderDao.xml的sql方法
<select id="selectOrderAllInfoById2" parameterType="java.lang.Integer" resultMap="BaseResultMap7"> select t.id, t.user_id, t.create_time, t.up_time from t_order t where t.id = #{id,jdbcType=INTEGER} </select>
TUserDao.xml的sql方法
<select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from t_user where id = #{id,jdbcType=INTEGER} </select>
TOrderDetailDao.xml的sql方法
<select id="selectByOrderid" parameterType="integer" resultMap="BaseResultMap"> SELECT id, order_id, goods_id, num, total_price from t_order_detail WHERE order_id = #{orderId,jdbcType=INTEGER} </select>
test方法查询结果:
test方法代码,可以在下面的执行结果中看出来,在获取order的tUser属性和orderDetailList属性时才会去触发级联属性的查询方法。
@Test public void test15(){ SqlSession sqlSession = sqlSessionFactory.openSession(true); TOrderDao orderDao = sqlSession.getMapper(TOrderDao.class); TOrder order = orderDao.selectOrderAllInfoById2(1); log.info("-----------------1"); log.info("{}", order.gettUser()); log.info("-----------------2"); log.info("{}", order.getOrderDetailList()); sqlSession.close(); }
2、resultMap配置,取消setting标签作为全局配置,在resultMap的级联查询中增加fetchType属性,并将其设置为lazy。
<resultMap id="BaseResultMap7" type="entity.TOrder"> <id column="id" property="id" /> <result column="user_id" property="userId" /> <result column="create_time" property="createTime" /> <result column="up_time" property="upTime" /> <association property="tUser" javaType="entity.TUser" column="user_id" select="dao.TUserDao.selectById" fetchType="lazy"/> <collection property="orderDetailList" ofType="entity.TOrderDetail" column="id" select="dao.TOrderDetailDao.selectByOrderid" fetchType="lazy"/> </resultMap>
各个dao层方法的sql语句按照上面的不做更改。执行test方法,结果截图如下。
二、discriminator(鉴别器的使用)
resultMap配置
<resultMap id="BaseResultMap8" type="entity.TOrder"> <id column="id" property="id" /> <result column="user_id" property="userId" /> <result column="create_time" property="createTime" /> <result column="up_time" property="upTime" /> <discriminator javaType="int" column="id" > <case value="1"> <association property="tUser" javaType="entity.TUser" column="user_id" select="dao.TUserDao.selectById" fetchType="lazy"/> </case> <case value="2"> <collection property="orderDetailList" ofType="entity.TOrderDetail" column="id" select="dao.TOrderDetailDao.selectByOrderid" fetchType="lazy"/> </case> </discriminator> </resultMap>
在test方法中传入主键id值1,运行结果:
在test方法中传入主键id值2,运行结果:
三、extends继承的使用
extends继承的使用,使用在resultMap中,可以简化resultMap标签的书写。
resultMap配置调整
<resultMap id="BaseResultMap9" type="entity.TOrder" extends="BaseResultMap"> <discriminator javaType="int" column="id" > <case value="1"> <association property="tUser" javaType="entity.TUser" column="user_id" select="dao.TUserDao.selectById" fetchType="lazy"/> </case> <case value="2"> <collection property="orderDetailList" ofType="entity.TOrderDetail" column="id" select="dao.TOrderDetailDao.selectByOrderid" fetchType="lazy"/> </case> </discriminator> </resultMap>
sql方法调整:
<select id="selectOrderAllInfoById2" parameterType="java.lang.Integer" resultMap="BaseResultMap9"> select t.id, t.user_id, t.create_time, t.up_time from t_order t where t.id = #{id,jdbcType=INTEGER} </select>
test方法运行结果获取。