1. 程式人生 > >[MyBatis]延遲載入/collection/多列向collection傳多值

[MyBatis]延遲載入/collection/多列向collection傳多值

延遲載入

在使用的時候才進行查詢,在分段查詢的基礎上進行查詢,並在全域性配置檔案中配置即可

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--設定列印資訊,不然看不到sql-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <!--延時載入,並且要禁用全部載入-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
 public void test3() throws IOException {
  start();
  Person person = mapper.getPersonAndEmpByIdStep(1);
  System.out.println(person.getpName());//只發一條sql
  System.out.println(person);//發兩條

//
//  DepartmentMapper mapper2 = sqlSession.getMapper(DepartmentMapper.class);
//  System.out.println(mapper2.getDeptById(1));


  sqlSession.close();
 }

collection

public class Department {

 private Integer dId;
 private String dName;
 private List<Person> person;//這個部門下的所有員工
 public Department getDeptByIdPlus(Integer id);//把部門下的所有員工都查出來

參考sql

SELECT d.dId,d.dName,p.pid,p.email,p.p_name pname
from department d
LEFT JOIN person p
ON d.dId=p.d_id
WHERE d.dId=1

mapper

    <!--private Integer dId;
     private String dName;
     private List<Person> persons;||分割
     Person:==>private Integer pId;
     private String pName;
     private String email;
     private Department dept;-->
    <resultMap id="mydept" type="com.yiki.Entity.Department">
        <id column="dId" property="dId"/>
        <result column="dName" property="dName"/>
        <!--conllection定義集合屬性封裝規則-->
        <collection property="persons" ofType="com.yiki.Entity.Person">
            <!--定義集合中的封裝規則-->
            <id column="pId" property="pId"/>
            <!--指定普通列     column是sql裡的,property是bean裡的!-->
            <result column="pname" property="pName"/>
            <result column="email" property="email"/>
        </collection>
    </resultMap>

    <select id="getDeptByIdPlus" resultMap="mydept">
        SELECT d.dId,d.dName,p.pid,p.email,p.p_name pname
        from department d
        LEFT JOIN person p
        ON d.dId=p.d_id
        WHERE d.dId=#{dId}
    </select>

測試

 @Test//級聯collection查詢
 public void test4() throws IOException {
  start();
  Department dept = d_mapper.getDeptByIdPlus(1);
  System.out.println(dept);
  System.out.println(dept.getPersons());

  sqlSession.close();
 }


collection分步查詢

person的接口裡

public List<Person> getListPersonByDeptId(Integer id);

person的mapper裡

<!--這裡是deptmap裡分步用到的select-->
    <select id="getListPersonByDeptId" resultType="com.yiki.Entity.Person">
        select * from person where d_id=#{id}
    </select>

department的接口裡

 public Department getDeptByIdSept(Integer id);

department的mapper裡

 <resultMap id="myStep" type="com.yiki.Entity.Department">
        <id column="dId" property="dId"/>
        <result column="dName" property="dName"/>
        <collection property="persons"
                    select="com.yiki.Dao.PersonMapperPlus.getListPersonByDeptId"
        column="dId">

        </collection>

    </resultMap>

    <select id="getDeptByIdSept" resultMap="myStep">
        select * from  department where dId=#{dId}
    </select>

測試

 @Test//級聯collection分步查詢
 public void test5() throws IOException {
  start();
  Department dept = d_mapper.getDeptByIdSept(1);
  System.out.println(dept);
  System.out.println(dept.getPersons());


  sqlSession.close();
 }

多值傳參

column={key1=column1,key2=column2}

形如

column{dId=id}