Ibatis N:1避免N+1查詢的方法

一、實體類

多方:
public class Employ {
private int id;
private String enployName;
private int salary;
private Department department;

public Employ() {
}

public int getId() {
return id;
}

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

public String getEnployName() {
return enployName;
}

public void setEnployName(String enployName) {
this.enployName = enployName;
}

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}

public Department getDepartment() {
return department;
}

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

一方:
public class Department {
private int did;
private String departmentName;
private List<Employ> employees;


public int getDid() {
return did;
}

public void setDid(int did) {
this.did = did;
}

public String getDepartmentName() {
return departmentName;
}

public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}

public List<Employ> getEmployees() {
return employees;
}

public void setEmployees(List<Employ> employees) {
this.employees = employees;
}
}
 



二、映射文件

多方:
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="Employ">

  <!-- Use type aliases to avoid typing the full classname every time. -->
  <typeAlias alias="Employ" type="com.test.domain.Employ"/>

  <!-- Result maps describe the mapping between the columns returned
       from a query, and the class properties.  A result map isn't
       necessary if the columns (or aliases) match to the properties
       exactly. -->
  <resultMap id="EmployResult" class="Employ">
    <result property="id" column="id"/>
    <result property="enployName" column="employ_name"/>
    <result property="salary" column="salary"/>
    <result property="department.did" column="did"/>
    <result property="department.departmentName" column="department_name"/>
  </resultMap>

  <!-- Select with no parameters using the result map for Account class. -->
  <select id="selectAllEmploy" resultMap="EmployResult">
  <![CDATA[
  select * from employees e, departments d where e.departmentid = d.did
  ]]>
  </select>
  <!-- A simpler select example without the result map.  Note the
       aliases to match the properties of the target result class. -->
  
  <!-- Insert example, using the Account parameter class -->
  <insert id="insertEmploy" parameterClass="Employ">
  <![CDATA[
  insert into employees (employ_name, salary, departmentid) values(#enployName#, #salary#, #department.did#)
  ]]>
  </insert>

  <!-- Update example, using the Account parameter class -->

  <!-- Delete example, using an integer as the parameter class -->
</sqlMap>

一方:
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="Department">

  <!-- Use type aliases to avoid typing the full classname every time. -->
  <typeAlias alias="Department" type="com.test.domain.Department"/>

  <!-- Result maps describe the mapping between the columns returned
       from a query, and the class properties.  A result map isn't
       necessary if the columns (or aliases) match to the properties
       exactly. -->
  <resultMap id="DepartmentResult" class="Department">
    <result property="did" column="did"/>
    <result property="departmentName" column="department_name"/>
  </resultMap>

  <!-- Select with no parameters using the result map for Account class. -->
  <select id="selectDepartmentById" parameterClass="int" resultMap="DepartmentResult">
  <![CDATA[
  select * from departments where did = #did#
  ]]>
  </select>
  <!-- A simpler select example without the result map.  Note the
       aliases to match the properties of the target result class. -->
  
  <!-- Insert example, using the Account parameter class -->
  <insert id="insertDepartment" parameterClass="Department">
  <![CDATA[
  insert into departments (department_name) values(#departmentName#)
  ]]>
  </insert>

  <!-- Update example, using the Account parameter class -->

  <!-- Delete example, using an integer as the parameter class -->
</sqlMap>


 

 

發佈了49 篇原創文章 · 獲贊 0 · 訪問量 2720
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章