码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis:延迟加载时不要在get/set方法上面添加final关键字(原创)

时间:2018-07-07 23:27:46      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:设置   分享图片   oct   ati   enable   src   调用   order   display   

1.mybatis-config.xml:

技术分享图片

<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE configuration     
  PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">    

<configuration>  
    <settings>  
        <setting name="lazyLoadingEnabled" value="true"/>  
        <setting name="aggressiveLazyLoading" value="false"/>  
    </settings>  
</configuration>

以上配置发现延迟加载配置是有效的。

#全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。
mybatis.configuration.lazy-loading-enabled=true
#当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。
mybatis.configuration.aggressive-lazy-loading=false

这是我在springboot中的配置,发现没有作用,请用文件方式的配置

2.Mapper文件配置

<mapper namespace="com.test.dao.base.DaoBaseUser">
    <resultMap id="BaseResultMap"
        type="com.test.entity.base.EntityBaseUser">
        <id column="userID" jdbcType="VARCHAR" property="userID" />
        <result column="orgID" jdbcType="VARCHAR" property="orgID" />
        <result column="postID" jdbcType="VARCHAR" property="postID" />          
        <!-- 关联查询:用户对应的角色 -->
        <association property="roles" javaType="java.util.List" 
            select="com.csget.dao.base.DaoBaseUser.selectRoles" column="userID"/>
    </resultMap>

<select id="selectRoles" resultType="java.lang.String">
     SELECT roleID FROM t_base_role_user_ref WHERE userID=#{userID}
 </select>

<select id="selectByMobile" resultMap="BaseResultMap">
     select
     <include refid="Base_Column_List" />
     from t_base_user where mobile=#{mobile} and isValid=‘1‘        
 </select>    

</mapper>

3.get/set方法

public class EntityBaseUser{

   //其它属性省略
/**
   * 获得:用户的角色列表
   *
   * @return the roles
   */
  public final List<String> getRoles() {
    return roles;
  }

  /**
   * 设置:用户的角色列表
   *
   * @param roles
   *          the roles to set
   */
  public final void setRoles(List<String> roles) {
    this.roles = roles;
  }
}

发现调用getRoles()方法并没有触发延迟加载查询,当断点调试的时候,鼠标放到EntityBaseUser的实例变量上,会立刻触发延迟加载查询,真是很奇怪。

最后怀疑:是不是get/set方法上面加了final导致的,果然去掉之后,发现一切正常了。

mybatis:延迟加载时不要在get/set方法上面添加final关键字(原创)

标签:设置   分享图片   oct   ati   enable   src   调用   order   display   

原文地址:https://www.cnblogs.com/huiy/p/9278767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!