1. 程式人生 > 實用技巧 >Mybatis的延遲載入

Mybatis的延遲載入

Mybatis是否支援延遲載入?如果支援,它的實現原理是什麼?

Mybatis僅支援association關聯物件和collection關聯集合物件的延遲載入,association指的就是一對一,多對一,collection指的就是一對多,多對多查詢。在Mybatis配置檔案中,可以配置是否啟用延遲載入lazyLoadingEnabled=true|false

延遲載入程式碼實現(一對多collection 查詢部門時,查出該部門的員工):

實體:Dept Emp

需要在Dept中加入private List<Emp> empList; 和getter setter (即構造方法)

dao:

DeptDao:

 /**

     * 查詢所有部門

     * @return

     */

    List<Dept>  listAll();

EmpDao:

   /**

     * 根據部門編號獲取員工

     * @param deptNo

     * @return

     */

    List<Emp> listEmpByDeptNo(int deptNo);

配置檔案:

DeptMapper.xml :

<!--查詢所有部門-->

     <select id="listAll" resultMap="deptEmp">

          select  
* from dept </select> <!--對映--> <resultMap id="deptEmp" type="com.aaa.mybatis.entity.Dept"> <id property="deptNo" column="deptno"/> <result property="deptName" column="deptname"/> <result property="loc" column="loc"/> <collection property="empList" column="deptno" ofType="com.aaa.mybatis.entity.Emp" select="com.aaa.mybatis.dao.EmpDao.listEmpByDeptNo"> </collection> </resultMap>

EmpMapper.xml:

<!--根據部門編號獲取員工-->

    <select  id="listEmpByDeptNo" resultType="com.aaa.mybatis.entity.Emp">

        select empno,empname,salary from emp where deptno=#{deptNo}

    </select>

主配置檔案:mybatis-config.xml 中的 settings

<!--開啟全域性懶載入-->

    <setting name="lazyLoadingEnabled" value="true"></setting>

測試:

/**
     * 測試延時載入
     */
    @Test
    public void testListAll(){
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionFacotryUtil.getSqlSession();
            DeptDao deptDao = sqlSession.getMapper(DeptDao.class);
            List<Dept> depts = deptDao.listAll();
            if(depts!=null&&depts.size()>0){
                for (Dept dept : depts) {
                    dept.getEmpList();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession!=null)
                sqlSession.close();
        }
    }
如果下面程式碼註釋掉,開啟懶載入,底層查詢資料庫時,只執行了部門查詢,如果不開啟懶載入,部門和該部門的員工都查詢了
   if(depts!=null&&depts.size()>0){
                for (Dept dept : depts) {
                    dept.getEmpList();
                }
            }

association指的就是一對一,多對一:

/*實體加入*/
private
Ictionaries ictionaries; <!-- 通用查詢對映結果 --> <resultMap id="CustomerIctionaries" type="com.aaa.entity.Customer"> <id column="customer_id" property="customerId" /> <result column="name" property="name" /> <result column="sex" property="sex" /> <result column="type_id" property="typeId" /> <result column="purchase_quantity" property="purchaseQuantity" /> <result column="purchase_purpose" property="purchasePurpose" /> <result column="purchase_date" property="purchaseDate" /> <result column="nationality" property="nationality" /> <result column="certificateType" property="certificateType" /> <result column="identification_number" property="identificationNumber" /> <result column="date_birth" property="dateBirth" /> <result column="home_address" property="homeAddress" /> <result column="residential_address" property="residentialAddress" /> <result column="issuing_authority" property="issuingAuthority" /> <result column="work_unit" property="workUnit" /> <result column="remarks" property="remarks"/> <association property="ictionaries" javaType="com.aaa.entity.Ictionaries"> <result property="code" column="code"/> </association> </resultMap>