Ibatisの2つの表を組み合わせて問題を調べる

6969 ワード

エンティティークラス:
複数:
 
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>  
  

一方:
  
<!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>  

                             
   u = (User) exampleMain.getSqlMap().queryForList("getUser1").get(0);
        System.out.println("user.id:"+u.getId()+", user.name:"+u.getName()+", test.name: "+u.getTest().getName());
    
out:
user.id:null , user.name:Sat Jul 30 11:28:20 CST 2011 , test.name: 11111111