mybatis的延迟加载、一级缓存、二级缓存

1.一级缓存没什么可说的,默认开启。

2.二级缓存:

1.  mybatis配置文件配置: <setting name="cacheEnabled" value="true" />

2. public class Department implements Serializable,缓存类实现序列化接口 .

3. mapper映射文件添加<cache />.

详情请看:


3.延迟加载,这个我举例说明(一个部门有多个员工):

package com.cn.su.entity;

import java.io.Serializable;
import java.util.List;

/**
 * 部门
 * @author su
 *
 */
@SuppressWarnings("serial")
public class Department implements Serializable{
    private Integer id;

    private String dtName;

    private String dtCreatetime;

    private String dtBz;
    
    private List<Employee> employeeList;

	public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDtName() {
        return dtName;
    }

    public void setDtName(String dtName) {
        this.dtName = dtName == null ? null : dtName.trim();
    }

    public String getDtCreatetime() {
        return dtCreatetime;
    }

    public void setDtCreatetime(String dtCreatetime) {
        this.dtCreatetime = dtCreatetime == null ? null : dtCreatetime.trim();
    }

    public String getDtBz() {
        return dtBz;
    }

    public void setDtBz(String dtBz) {
        this.dtBz = dtBz == null ? null : dtBz.trim();
    }

	public List<Employee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<Employee> employeeList) {
		this.employeeList = employeeList;
	}

	@Override
	public String toString() {
		return "Department [id=" + id + ", dtName=" + dtName
				+ ", dtCreatetime=" + dtCreatetime + ", dtBz=" +dtBz+"]";
	}
	
}


package com.cn.su.entity;

import java.io.Serializable;

/**
 * 员工
 * @author su
 *
 */
@SuppressWarnings("serial")
public class Employee implements Serializable{
    private Integer id;

    private String emSerialnumber;

    private String emName;

    private String emSex;

    private Integer emAge;

    private String emIdcard;

    private String emBorn;

    private String emNation;

    private String emMarriage;

    private String emVisage;

    private String emAncestralhome;

    private String emTel;

    private String emAddress;

    private String emAfterschool;

    private String emSpeciality;

    private String emCulture;

    private String emStartime;

    private Integer emDepartmentid;

    private String emTypework;

    private String emCreatime;

    private String emCreatename;

    private String emBz;
    
    private Department department;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEmSerialnumber() {
        return emSerialnumber;
    }

    public void setEmSerialnumber(String emSerialnumber) {
        this.emSerialnumber = emSerialnumber == null ? null : emSerialnumber.trim();
    }

    public String getEmName() {
        return emName;
    }

    public void setEmName(String emName) {
        this.emName = emName == null ? null : emName.trim();
    }

    public String getEmSex() {
        return emSex;
    }

    public void setEmSex(String emSex) {
        this.emSex = emSex == null ? null : emSex.trim();
    }

    public Integer getEmAge() {
        return emAge;
    }

    public void setEmAge(Integer emAge) {
        this.emAge = emAge;
    }

    public String getEmIdcard() {
        return emIdcard;
    }

    public void setEmIdcard(String emIdcard) {
        this.emIdcard = emIdcard == null ? null : emIdcard.trim();
    }

    public String getEmBorn() {
        return emBorn;
    }

    public void setEmBorn(String emBorn) {
        this.emBorn = emBorn == null ? null : emBorn.trim();
    }

    public String getEmNation() {
        return emNation;
    }

    public void setEmNation(String emNation) {
        this.emNation = emNation == null ? null : emNation.trim();
    }

    public String getEmMarriage() {
        return emMarriage;
    }

    public void setEmMarriage(String emMarriage) {
        this.emMarriage = emMarriage == null ? null : emMarriage.trim();
    }

    public String getEmVisage() {
        return emVisage;
    }

    public void setEmVisage(String emVisage) {
        this.emVisage = emVisage == null ? null : emVisage.trim();
    }

    public String getEmAncestralhome() {
        return emAncestralhome;
    }

    public void setEmAncestralhome(String emAncestralhome) {
        this.emAncestralhome = emAncestralhome == null ? null : emAncestralhome.trim();
    }

    public String getEmTel() {
        return emTel;
    }

    public void setEmTel(String emTel) {
        this.emTel = emTel == null ? null : emTel.trim();
    }

    public String getEmAddress() {
        return emAddress;
    }

    public void setEmAddress(String emAddress) {
        this.emAddress = emAddress == null ? null : emAddress.trim();
    }

    public String getEmAfterschool() {
        return emAfterschool;
    }

    public void setEmAfterschool(String emAfterschool) {
        this.emAfterschool = emAfterschool == null ? null : emAfterschool.trim();
    }

    public String getEmSpeciality() {
        return emSpeciality;
    }

    public void setEmSpeciality(String emSpeciality) {
        this.emSpeciality = emSpeciality == null ? null : emSpeciality.trim();
    }

    public String getEmCulture() {
        return emCulture;
    }

    public void setEmCulture(String emCulture) {
        this.emCulture = emCulture == null ? null : emCulture.trim();
    }

    public String getEmStartime() {
        return emStartime;
    }

    public void setEmStartime(String emStartime) {
        this.emStartime = emStartime == null ? null : emStartime.trim();
    }

    public Integer getEmDepartmentid() {
        return emDepartmentid;
    }

    public void setEmDepartmentid(Integer emDepartmentid) {
        this.emDepartmentid = emDepartmentid;
    }

    public String getEmTypework() {
        return emTypework;
    }

    public void setEmTypework(String emTypework) {
        this.emTypework = emTypework == null ? null : emTypework.trim();
    }

    public String getEmCreatime() {
        return emCreatime;
    }

    public void setEmCreatime(String emCreatime) {
        this.emCreatime = emCreatime == null ? null : emCreatime.trim();
    }

    public String getEmCreatename() {
        return emCreatename;
    }

    public void setEmCreatename(String emCreatename) {
        this.emCreatename = emCreatename == null ? null : emCreatename.trim();
    }

    public String getEmBz() {
        return emBz;
    }

    public void setEmBz(String emBz) {
        this.emBz = emBz == null ? null : emBz.trim();
    }

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", emSerialnumber=" + emSerialnumber
				+ ", emName=" + emName + ", emSex=" + emSex + ", emAge="
				+ emAge + ", emIdcard=" + emIdcard + ", emBorn=" + emBorn
				+ ", emNation=" + emNation + ", emMarriage=" + emMarriage
				+ ", emVisage=" + emVisage + ", emAncestralhome="
				+ emAncestralhome + ", emTel=" + emTel + ", emAddress="
				+ emAddress + ", emAfterschool=" + emAfterschool
				+ ", emSpeciality=" + emSpeciality + ", emCulture=" + emCulture
				+ ", emStartime=" + emStartime + ", emDepartmentid="
				+ emDepartmentid + ", emTypework=" + emTypework
				+ ", emCreatime=" + emCreatime + ", emCreatename="
				+ emCreatename + ", emBz=" + emBz + ", department="
				+ department.toString() + "]";
	}
    
}

  
mapper映射文件配置:

Department.xml

<?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.cn.su.dao.DepartmentMapper" >
	<!-- 二级缓存配置 -->
  <cache eviction="FIFO" flushInterval="60000" size="512" readOnly=""></cache>
	<!-- 下面是重点 -->
  <resultMap id="BaseResultMap" type="com.cn.su.entity.Department" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="dt_name" property="dtName" jdbcType="VARCHAR" />
    <result column="dt_createTime" property="dtCreatetime" jdbcType="VARCHAR" />
    <result column="dt_bz" property="dtBz" jdbcType="VARCHAR" />
	<!-- 1.这个是集合类型,一对多 2.嵌入select懒加载查询,用到的时候才会执行这个select语句,注意这里引用到了Employee.xml映射文件里的查询语句,只要写
对命名空间,我们是可以调用其他映射文件的操作语句的 -->
    <collection property="employeeList" ofType="com.cn.su.entity.Employee" select="com.cn.su.dao.EmployeeMapper.selectByDepartNo" column="id">
    </collection>
  </resultMap>
  <sql id="Base_Column_List" >
    id, dt_name, dt_createTime, dt_bz
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from tb_department
    where id = #{id,jdbcType=INTEGER}
  </select>
  <select id="selectAllDepartment" resultMap="BaseResultMap" parameterType="com.cn.su.entity.Department">
  	select * from tb_department
  </select>
  
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tb_department
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.cn.su.entity.Department" >
    insert into tb_department (id, dt_name, dt_createTime, 
      dt_bz)
    values (#{id,jdbcType=INTEGER}, #{dtName,jdbcType=VARCHAR}, #{dtCreatetime,jdbcType=VARCHAR}, 
      #{dtBz,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.cn.su.entity.Department" >
    insert into tb_department
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="dtName != null" >
        dt_name,
      </if>
      <if test="dtCreatetime != null" >
        dt_createTime,
      </if>
      <if test="dtBz != null" >
        dt_bz,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="dtName != null" >
        #{dtName,jdbcType=VARCHAR},
      </if>
      <if test="dtCreatetime != null" >
        #{dtCreatetime,jdbcType=VARCHAR},
      </if>
      <if test="dtBz != null" >
        #{dtBz,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.cn.su.entity.Department" >
    update tb_department
    <set >
      <if test="dtName != null" >
        dt_name = #{dtName,jdbcType=VARCHAR},
      </if>
      <if test="dtCreatetime != null" >
        dt_createTime = #{dtCreatetime,jdbcType=VARCHAR},
      </if>
      <if test="dtBz != null" >
        dt_bz = #{dtBz,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.cn.su.entity.Department" >
    update tb_department
    set dt_name = #{dtName,jdbcType=VARCHAR},
      dt_createTime = #{dtCreatetime,jdbcType=VARCHAR},
      dt_bz = #{dtBz,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>



Employee.xml文件配置:

<?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.cn.su.dao.EmployeeMapper" >
	<select id="selectDepartmentById" parameterType="int" resultType="com.cn.su.entity.Department">
		select * from tb_department where id = #{id}
	</select>
  <resultMap id="BaseResultMap" type="com.cn.su.entity.Employee" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="em_serialNumber" property="emSerialnumber" jdbcType="VARCHAR" />
    <result column="em_name" property="emName" jdbcType="VARCHAR" />
    <result column="em_sex" property="emSex" jdbcType="VARCHAR" />
    <result column="em_age" property="emAge" jdbcType="INTEGER" />
    <result column="em_IDCard" property="emIdcard" jdbcType="VARCHAR" />
    <result column="em_born" property="emBorn" jdbcType="VARCHAR" />
    <result column="em_nation" property="emNation" jdbcType="VARCHAR" />
    <result column="em_marriage" property="emMarriage" jdbcType="VARCHAR" />
    <result column="em_visage" property="emVisage" jdbcType="VARCHAR" />
    <result column="em_ancestralHome" property="emAncestralhome" jdbcType="VARCHAR" />
    <result column="em_tel" property="emTel" jdbcType="VARCHAR" />
    <result column="em_address" property="emAddress" jdbcType="VARCHAR" />
    <result column="em_afterSchool" property="emAfterschool" jdbcType="VARCHAR" />
    <result column="em_speciality" property="emSpeciality" jdbcType="VARCHAR" />
    <result column="em_culture" property="emCulture" jdbcType="VARCHAR" />
    <result column="em_startime" property="emStartime" jdbcType="VARCHAR" />
    <result column="em_departmentId" property="emDepartmentid" jdbcType="INTEGER" />
    <result column="em_typeWork" property="emTypework" jdbcType="VARCHAR" />
    <result column="em_creatime" property="emCreatime" jdbcType="VARCHAR" />
    <result column="em_createName" property="emCreatename" jdbcType="VARCHAR" />
    <result column="em_bz" property="emBz" jdbcType="VARCHAR" />
    <association property="department" javaType="com.cn.su.entity.Department" 
    	select="com.cn.su.dao.DepartmentMapper.selectByPrimaryKey" column="em_departmentId">
    </association>
  </resultMap>
  <sql id="Base_Column_List" >
    id, em_serialNumber, em_name, em_sex, em_age, em_IDCard, em_born, em_nation, em_marriage, 
    em_visage, em_ancestralHome, em_tel, em_address, em_afterSchool, em_speciality, em_culture, 
    em_startime, em_departmentId, em_typeWork, em_creatime, em_createName, em_bz
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from tb_employee
    where id = #{id,jdbcType=INTEGER}
  </select>
  <!-- 通过部门类型查询员工 -->
  <select id="selectByDepartNo" parameterType="int" resultMap="BaseResultMap">
  	select 
    <include refid="Base_Column_List" />
    from tb_employee
    where em_departmentId = #{emDepartmentid}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tb_employee
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.cn.su.entity.Employee" >
    insert into tb_employee (id, em_serialNumber, em_name, 
      em_sex, em_age, em_IDCard, 
      em_born, em_nation, em_marriage, 
      em_visage, em_ancestralHome, em_tel, 
      em_address, em_afterSchool, em_speciality, 
      em_culture, em_startime, em_departmentId, 
      em_typeWork, em_creatime, em_createName, 
      em_bz)
    values (#{id,jdbcType=INTEGER}, #{emSerialnumber,jdbcType=VARCHAR}, #{emName,jdbcType=VARCHAR}, 
      #{emSex,jdbcType=VARCHAR}, #{emAge,jdbcType=INTEGER}, #{emIdcard,jdbcType=VARCHAR}, 
      #{emBorn,jdbcType=VARCHAR}, #{emNation,jdbcType=VARCHAR}, #{emMarriage,jdbcType=VARCHAR}, 
      #{emVisage,jdbcType=VARCHAR}, #{emAncestralhome,jdbcType=VARCHAR}, #{emTel,jdbcType=VARCHAR}, 
      #{emAddress,jdbcType=VARCHAR}, #{emAfterschool,jdbcType=VARCHAR}, #{emSpeciality,jdbcType=VARCHAR}, 
      #{emCulture,jdbcType=VARCHAR}, #{emStartime,jdbcType=VARCHAR}, #{emDepartmentid,jdbcType=INTEGER}, 
      #{emTypework,jdbcType=VARCHAR}, #{emCreatime,jdbcType=VARCHAR}, #{emCreatename,jdbcType=VARCHAR}, 
      #{emBz,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.cn.su.entity.Employee" >
    insert into tb_employee
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="emSerialnumber != null" >
        em_serialNumber,
      </if>
      <if test="emName != null" >
        em_name,
      </if>
      <if test="emSex != null" >
        em_sex,
      </if>
      <if test="emAge != null" >
        em_age,
      </if>
      <if test="emIdcard != null" >
        em_IDCard,
      </if>
      <if test="emBorn != null" >
        em_born,
      </if>
      <if test="emNation != null" >
        em_nation,
      </if>
      <if test="emMarriage != null" >
        em_marriage,
      </if>
      <if test="emVisage != null" >
        em_visage,
      </if>
      <if test="emAncestralhome != null" >
        em_ancestralHome,
      </if>
      <if test="emTel != null" >
        em_tel,
      </if>
      <if test="emAddress != null" >
        em_address,
      </if>
      <if test="emAfterschool != null" >
        em_afterSchool,
      </if>
      <if test="emSpeciality != null" >
        em_speciality,
      </if>
      <if test="emCulture != null" >
        em_culture,
      </if>
      <if test="emStartime != null" >
        em_startime,
      </if>
      <if test="emDepartmentid != null" >
        em_departmentId,
      </if>
      <if test="emTypework != null" >
        em_typeWork,
      </if>
      <if test="emCreatime != null" >
        em_creatime,
      </if>
      <if test="emCreatename != null" >
        em_createName,
      </if>
      <if test="emBz != null" >
        em_bz,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="emSerialnumber != null" >
        #{emSerialnumber,jdbcType=VARCHAR},
      </if>
      <if test="emName != null" >
        #{emName,jdbcType=VARCHAR},
      </if>
      <if test="emSex != null" >
        #{emSex,jdbcType=VARCHAR},
      </if>
      <if test="emAge != null" >
        #{emAge,jdbcType=INTEGER},
      </if>
      <if test="emIdcard != null" >
        #{emIdcard,jdbcType=VARCHAR},
      </if>
      <if test="emBorn != null" >
        #{emBorn,jdbcType=VARCHAR},
      </if>
      <if test="emNation != null" >
        #{emNation,jdbcType=VARCHAR},
      </if>
      <if test="emMarriage != null" >
        #{emMarriage,jdbcType=VARCHAR},
      </if>
      <if test="emVisage != null" >
        #{emVisage,jdbcType=VARCHAR},
      </if>
      <if test="emAncestralhome != null" >
        #{emAncestralhome,jdbcType=VARCHAR},
      </if>
      <if test="emTel != null" >
        #{emTel,jdbcType=VARCHAR},
      </if>
      <if test="emAddress != null" >
        #{emAddress,jdbcType=VARCHAR},
      </if>
      <if test="emAfterschool != null" >
        #{emAfterschool,jdbcType=VARCHAR},
      </if>
      <if test="emSpeciality != null" >
        #{emSpeciality,jdbcType=VARCHAR},
      </if>
      <if test="emCulture != null" >
        #{emCulture,jdbcType=VARCHAR},
      </if>
      <if test="emStartime != null" >
        #{emStartime,jdbcType=VARCHAR},
      </if>
      <if test="emDepartmentid != null" >
        #{emDepartmentid,jdbcType=INTEGER},
      </if>
      <if test="emTypework != null" >
        #{emTypework,jdbcType=VARCHAR},
      </if>
      <if test="emCreatime != null" >
        #{emCreatime,jdbcType=VARCHAR},
      </if>
      <if test="emCreatename != null" >
        #{emCreatename,jdbcType=VARCHAR},
      </if>
      <if test="emBz != null" >
        #{emBz,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.cn.su.entity.Employee" >
    update tb_employee
    <set >
      <if test="emSerialnumber != null" >
        em_serialNumber = #{emSerialnumber,jdbcType=VARCHAR},
      </if>
      <if test="emName != null" >
        em_name = #{emName,jdbcType=VARCHAR},
      </if>
      <if test="emSex != null" >
        em_sex = #{emSex,jdbcType=VARCHAR},
      </if>
      <if test="emAge != null" >
        em_age = #{emAge,jdbcType=INTEGER},
      </if>
      <if test="emIdcard != null" >
        em_IDCard = #{emIdcard,jdbcType=VARCHAR},
      </if>
      <if test="emBorn != null" >
        em_born = #{emBorn,jdbcType=VARCHAR},
      </if>
      <if test="emNation != null" >
        em_nation = #{emNation,jdbcType=VARCHAR},
      </if>
      <if test="emMarriage != null" >
        em_marriage = #{emMarriage,jdbcType=VARCHAR},
      </if>
      <if test="emVisage != null" >
        em_visage = #{emVisage,jdbcType=VARCHAR},
      </if>
      <if test="emAncestralhome != null" >
        em_ancestralHome = #{emAncestralhome,jdbcType=VARCHAR},
      </if>
      <if test="emTel != null" >
        em_tel = #{emTel,jdbcType=VARCHAR},
      </if>
      <if test="emAddress != null" >
        em_address = #{emAddress,jdbcType=VARCHAR},
      </if>
      <if test="emAfterschool != null" >
        em_afterSchool = #{emAfterschool,jdbcType=VARCHAR},
      </if>
      <if test="emSpeciality != null" >
        em_speciality = #{emSpeciality,jdbcType=VARCHAR},
      </if>
      <if test="emCulture != null" >
        em_culture = #{emCulture,jdbcType=VARCHAR},
      </if>
      <if test="emStartime != null" >
        em_startime = #{emStartime,jdbcType=VARCHAR},
      </if>
      <if test="emDepartmentid != null" >
        em_departmentId = #{emDepartmentid,jdbcType=INTEGER},
      </if>
      <if test="emTypework != null" >
        em_typeWork = #{emTypework,jdbcType=VARCHAR},
      </if>
      <if test="emCreatime != null" >
        em_creatime = #{emCreatime,jdbcType=VARCHAR},
      </if>
      <if test="emCreatename != null" >
        em_createName = #{emCreatename,jdbcType=VARCHAR},
      </if>
      <if test="emBz != null" >
        em_bz = #{emBz,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.cn.su.entity.Employee" >
    update tb_employee
    set em_serialNumber = #{emSerialnumber,jdbcType=VARCHAR},
      em_name = #{emName,jdbcType=VARCHAR},
      em_sex = #{emSex,jdbcType=VARCHAR},
      em_age = #{emAge,jdbcType=INTEGER},
      em_IDCard = #{emIdcard,jdbcType=VARCHAR},
      em_born = #{emBorn,jdbcType=VARCHAR},
      em_nation = #{emNation,jdbcType=VARCHAR},
      em_marriage = #{emMarriage,jdbcType=VARCHAR},
      em_visage = #{emVisage,jdbcType=VARCHAR},
      em_ancestralHome = #{emAncestralhome,jdbcType=VARCHAR},
      em_tel = #{emTel,jdbcType=VARCHAR},
      em_address = #{emAddress,jdbcType=VARCHAR},
      em_afterSchool = #{emAfterschool,jdbcType=VARCHAR},
      em_speciality = #{emSpeciality,jdbcType=VARCHAR},
      em_culture = #{emCulture,jdbcType=VARCHAR},
      em_startime = #{emStartime,jdbcType=VARCHAR},
      em_departmentId = #{emDepartmentid,jdbcType=INTEGER},
      em_typeWork = #{emTypework,jdbcType=VARCHAR},
      em_creatime = #{emCreatime,jdbcType=VARCHAR},
      em_createName = #{emCreatename,jdbcType=VARCHAR},
      em_bz = #{emBz,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

测试效果:

public List<DepartmentVo> selectAllDepartment() {
		List<Department> departmentList = departmentMapper.selectAllDepartment();
		List<DepartmentVo> departmentVoList = new ArrayList<DepartmentVo>();
		for(int i=0;i<departmentList.size();i++){
			//一直到这里都没有执行查询Employee
			int sumPeople = caculateSumPeople(departmentList.get(i));
			DepartmentVo departmentVo = new DepartmentVo();
			departmentVo.setDepartment(departmentList.get(i));
			departmentVo.setSumPeople(sumPeople);
			departmentVoList.add(departmentVo);
		}
		return departmentVoList;
	}
	
	/**
	 * 
	 * @Title: caculateSumPeople
	 * @Description: 统计每个部门的人数
	 * @param @return 参数
	 * @return int 返回类型
	 * @throws
	 */
	private int caculateSumPeople(Department department){
		logger.info("执行前。。。。。。加载了查询语句吗?");//这里还没有执行查询employee
		List<Employee> employeeList = department.getEmployeeList();
		logger.info("执行后。。。。。。现在加载了查询语句吗?");//这里才查询了
		for(Employee employee : employeeList){
			logger.info(employee.toString());
		}
		return employeeList.size();
	}
	
可以看出,懒加载目的是,当我们只需要用到Department主干内容时,它是不加载子级下的employee的,而需要用到某个departmentId下的employee时,才去查询它!这样节省了许多资源,减轻了访问数据的压力。


猜你喜欢

转载自blog.csdn.net/qq_25313187/article/details/78359933