zoukankan      html  css  js  c++  java
  • mybatis:延迟加载时不要在get/set方法上面添加final关键字(原创)

    1.mybatis-config.xml:

    image

    <?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导致的,果然去掉之后,发现一切正常了。

  • 相关阅读:
    数据库原理 第七章 数据库设计和ER模型
    jeecgboot常见问题及处理方法-found character '@' that cannot start any token. (Do not use @ for indentation)
    jeecgboot积木报表(jimuReport)Oracle切换
    datart表结构
    这几天找工作的经历
    Jenkins 无法登陆解决方法
    Nginx 部署前后端分离项目(SpringBoot Vue)
    CentOS7 用yum方式安装Nginx
    Centos 7 安装 MYSQL 8.0
    Centos 7 安装 JDK1.8
  • 原文地址:https://www.cnblogs.com/huiy/p/9278767.html
Copyright © 2011-2022 走看看