mybaits十:关联查询
生活随笔
收集整理的這篇文章主要介紹了
mybaits十:关联查询
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
普通javaBean?
package com.atChina.bean;import java.util.Date;public class Employee {private String empno;private String ename;private String job;private Integer mgr;private Date hiredate;private Double sal;private Department dt;public String getEmpno() {return empno;}public void setEmpno(String empno) {this.empno = empno;}public String getEname() {return ename;}public void setEname(String ename) {this.ename = ename;}public String getJob() {return job;}public void setJob(String job) {this.job = job;}public Integer getMgr() {return mgr;}public void setMgr(Integer mgr) {this.mgr = mgr;}public Date getHiredate() {return hiredate;}public void setHiredate(Date hiredate) {this.hiredate = hiredate;}public Double getSal() {return sal;}public void setSal(Double sal) {this.sal = sal;}public Department getDt() {return dt;}public void setDt(Department dt) {this.dt = dt;}@Overridepublic String toString() {return "Employee [empno=" + empno + ", ename=" + ename + ", job=" + job+ ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal+ ", dt=" + dt + "]";}}package com.atChina.bean;public class Department {private Integer id;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}private Integer deptno;private String dname;private String loc;public int getDeptno() {return deptno;}public void setDeptno(int deptno) {this.deptno = deptno;}public String getDname() {return dname;}public void setDname(String dname) {this.dname = dname;}public String getLoc() {return loc;}public void setLoc(String loc) {this.loc = loc;}@Overridepublic String toString() {return "Department [id=" + id + ", deptno=" + deptno + ", dname="+ dname + ", loc=" + loc + "]";} }接口?
package com.atChina.dao;import com.atChina.bean.Employee;public interface EmployeePlusMapper {public Employee getEmpByDepnos(Integer depno);public Employee getEmpAndDeptByEmpno(Integer empno);public Employee getEmpAndDeptByStep(Integer empno); }?方式一:聯(lián)合查詢,通過級聯(lián)屬性封裝結果集
<!-- 聯(lián)合查詢,級聯(lián)屬性封裝結果集 --><resultMap type="com.atChina.bean.Employee" id="defineDifEmp"><id column="empno" property="empno" /><result column="ename" property="ename" /><result column="job" property="job" /><result column="mgr" property="mgr" /><result column="hiredate" property="hiredate" /><result column="sal" property="sal" /><result column="deptno" property="dt.deptno" /><result column="dname" property="dt.dname" /><result column="loc" property="dt.loc" /></resultMap><select id="getEmpAndDeptByEmpno" resultMap="defineDifEmp">select a.empno, a.ename, a.job, a.mgr, a.hiredate, a.sal,b.deptno, b.dname, b.loc from emptest a join depttest bon a.deptno = b.deptnowhere a.empno = #{empno}</select>? 方式二:使用association標簽定義關聯(lián)的單個對象的封裝規(guī)則
<resultMap type="com.atChina.bean.Employee" id="defineDifEmp2"><id column="empno" property="empno" /><result column="ename" property="ename" /><result column="job" property="job" /><result column="mgr" property="mgr" /><result column="hiredate" property="hiredate" /><result column="sal" property="sal" /><!-- association可以指定聯(lián)合的javaBean對象property="dt":指定哪個屬性是聯(lián)合的對象javaType:指定這個屬性對象的類型[不能省略]--><association property="dt" javaType="com.atChina.bean.Department"><result column="deptno" property="deptno" /><result column="dname" property="dname" /><result column="loc" property="loc" /></association></resultMap><select id="getEmpAndDeptByEmpno" resultMap="defineDifEmp2">select a.empno, a.ename, a.job, a.mgr, a.hiredate, a.sal,b.deptno, b.dname, b.loc from emptest a join depttest bon a.deptno = b.deptnowhere a.empno = #{empno}</select>?
測試方法:
@Testpublic void test25() throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession openSession = sqlSessionFactory.openSession();try{// 命名空間.id,這樣別的配置文件里有同名的id,程序也不報錯EmployeePlusMapper em = openSession.getMapper(EmployeePlusMapper.class);System.out.println(em.getClass()); // 動態(tài)代理類Employee ee = em.getEmpAndDeptByEmpno(7369);System.out.println(ee);}finally{// 關閉openSession.close();}}?
總結
以上是生活随笔為你收集整理的mybaits十:关联查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybaits九:自定义结果映射规则
- 下一篇: mybaits十一:使用associat