Mybatis懒加载

一、需求:查询用户信息,有时候需要关联查出部门信息。
 
    第一种方法:我们直接关联查询出所有用户和部门的信息
         select * from tb_user u ,tb_department d where u.dep_id = d.dep_id;
  分析:
  ①这里我们一次查询出所有用户信息,需要什么部门信息的时候直接从查询的结果中筛选。但是如果部门和用户表都比较大的时候,这种关联查询肯定比较耗时。
  ②我们的需求是有时候需要关联查询部门信息。这里不是一定需要部门信息的。即有时候不需要查询部门信息,我们也查了,程序进行了多余的耗时操作。
    第二种方法:分步查询,首先查询出所有的用户信息,然后如果需要部门的信息,我们在根据查询的部门信息去关联部门信息
     //查询所有的用户信息,包括部门id
        select * from tb_user;
    //如果需要部门信息,我们再根据上一步查询的部门id查询部门信息
       
        select * from tb_department where dep_id=id
   分析:
     ①这里两步都是单表查询,执行效率比关联查询要高很多
     ②分为两步,如果我们不需要关联部门信息,那么我们就不必执行第二步,程序没有进行多余的操作。
这第二种方法就是mybatis的懒加载

二、什么是懒加载?

    通俗的讲就是按需加载,我们需要什么的时候再去进行什么操作。而且先从单表查询,需要时再从关联表去关联查询,能大大提高数据库性能,因为查询单表要比关联查询多张表速度要快。
  在mybatis中,resultMap可以实现高级映射,association、collection具备延迟加载功能。
  具体实例:
       1、创建User类(用户类):
    public class User implements Serializable {
         private static final long serialVersionUID = 6716332190979093860L;
         private Integer Id;
         private String username;
         private String sex;
         private String password;
         private Date birthday;
         private Department department;
      }
  
    2、创建Department类(部门类)
     public class Department {
          private Integer dep_id;
           private String  dep_name;
           private String  manager;
       }
    3、创建UserMapper类
     public interface UserMapper {
          public int insert(User u)throws Exception;
          public int delete(Integer id)throws Exception;
          public int updateUser(User u)throws Exception;
          public List<User> selectAll()throws Exception;
          public User selectByUserId(Integer id)throws Exception;
          public Department selectDepartment(Integer id)throws Exception;
        }
     4、定义UserMapper.xml文件
     <mapper namespace="com.mybatis.mapper.UserMapper">
       <resultMap type="User" id="userMap">
         <id property="id" column="id" javaType="java.lang.Integer"/>
         <result property="username" column="username" javaType="java.lang.String"/>
         <result property="password" column="password" javaType="java.lang.String"/>
         <result property="gender" column="gender" javaType="java.lang.String"/>
         <result property="address" column="address" javaType="java.lang.String"/>
         <result property="birthday" column="birthday" javaType="java.util.Date"/>
         <association property="department" column="dep_id" fetchType="lazy" select="selectDepartment"/>
       </resultMap>
    
       <resultMap type="Department" id="departmentMap">
         <id property="dep_id" column="dep_id"/>
         <result property="dep_name" column="dep_name"/>
         <result property="manager" column="dep_manager"/>
       </resultMap>
 
       <select id="selectAll"  resultMap="userMap" >
          select * from tb_user
       </select>
    
       <select id="selectDepartment" parameterType="int" resultMap="departmentMap">
         select * from tb_department where dep_id=#{id}
       </select>
     </mapper>
    5、在mybatis-config.xml文件中开启懒加载
    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
     </settings>
    
    6、测试
        UserMapper mapper = session.getMapper(UserMapper.class);
            List<User> users = mapper.selectAll();
           
            for(User u:users)
            {
                System.out.println(u.getDepartment());
            }
原文地址:https://www.cnblogs.com/lone5wolf/p/10961537.html