Ibatis的两表联查问题

实体类:

多方: 

 

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>  

 <typeAlias  alias  = "user"  type  = "model.User"   />
    <resultMap id="userResult" class="user">
        <result property="name" column="uname"/>
        <result property="test.name" column="tname"/>
    </resultMap>
    <select id="getUser1" resultMap="userResult">
        select t_useR.name as uname , test.name as tname from t_useR ,test  where t_useR.test_id=test.id

    </select>

 

 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值