首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Ibatis的两表联查有关问题

2012-10-23 
Ibatis的两表联查问题实体类:多方:??public class Employ {private int idprivate String enployNamepri

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" 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" parameterencoding="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" column="did"/>      <result property="departmentName" column="department_name"/>    </resultMap>      <!-- Select with no parameters using the result map for Account class. -->    <select id="selectDepartmentById" parameterresultMap="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" parametercolumn="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

热点排行