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;
	}
	@Override
	public 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;
	}

	@Override
	public 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);
}

 方式一:联合查询,通过级联属性封装结果集

<!-- 联合查询,级联属性封装结果集 -->
	<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 b
		on a.deptno = b.deptno
		where a.empno = #{empno}
	</select>

  方式二:使用association标签定义关联的单个对象的封装规则

<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可以指定联合的javaBean对象
    		 property="dt":指定哪个属性是联合的对象
    		 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 b
		on a.deptno = b.deptno
		where a.empno = #{empno}
	</select>

测试方法:

@Test
	public 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()); // 动态代理类
			
			Employee ee = em.getEmpAndDeptByEmpno(7369);
			
			System.out.println(ee);
		}finally{
			// 关闭
			openSession.close();
		}
	}

猜你喜欢

转载自blog.csdn.net/m0_37564426/article/details/88920687
今日推荐