一对一 一个员工对应一个部门
实体类 emp
package com.entity;
import java.io.Serializable;
import java.math.BigDecimal;
public class EmpVo implements Serializable {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private String hiredate;
private BigDecimal sal;
private BigDecimal comm;
private Dept dept;//一个员工对应一个部门
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) { this.dept = dept; }
public Integer getEmpno() { return empno; }
public void setEmpno(Integer 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 String getHiredate() { return hiredate; }
public void setHiredate(String hiredate) {this.hiredate =hiredate; }
public BigDecimal getSal() { return sal; }
public void setSal(BigDecimal sal) { this.sal = sal; }
public BigDecimal getComm() { return comm; }
public void setComm(BigDecimal comm) { this.comm = comm; }
@Override
public String toString() {
return "EmpVo{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate='" + hiredate + '\'' +
", sal=" + sal +
", comm=" + comm +
'}';
}
}
接口 EmpMapper
public interface EmpMapper {
//多表查询
List<EmpVo> selectEmpDeptMapping();
}
<resultMap id="empDeptMapping" type="empVo">
<id column="empno" property="empno"></id>
<result column="ename" property="ename"></result>
<result column="job" property="job"></result>
<result column="mgr" property="mgr"></result>
<result column="hiredate" property="hiredate"></result>
<result column="sal" property="sal"></result>
<result column="comm" property="comm"></result>
<association property="dept" javaType="dept">
<id column="deptno" property="deptno"></id>
<result column="dname" property="dname"></result>
<result column="loc" property="loc"></result>
</association>
</resultMap>
<select id="selectEmpDeptMapping" resultMap="empDeptMapping">
select e.*,d.* from emp e,dept d where e.deptno=d.deptno
</select>
@Test
public void selectEmpDeptMapping() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
List<EmpVo> empList = empMapper.selectEmpDeptMapping();
for (EmpVo empVo : empList) {
System.out.println(empVo+"---"+empVo.getDept());
}
sqlSession.close();
}
一对多 一个部门多个员工
实体类 Dept
package com.entity;
import java.util.List;
public class Dept {
private Integer deptno;
private String dname;
private String loc;
List<EmpVo> empVos; //一个部门多个员工
public List<EmpVo> getEmpVos() {
return empVos;
}
public void setEmpVos(List<EmpVo> empVos) {
this.empVos = empVos;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer 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;
}
public Dept(Integer deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public Dept() {
}
@Override
public String toString() {
return "Dept{" +
"deptno=" + deptno +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
'}';
}
}
public interface EmpMapper {
List<Dept> selectDeptEmpMapping(); //查询各个部门的员工信息
}
<!--一对多 一个部门多个员工-->
<resultMap id="deptEmpMapping" type="dept">
<id column="deptno" property="deptno"></id>
<result column="dname" property="dname"></result>
<result column="loc" property="loc"></result>
<collection property="empVos" ofType="empVo">
<id column="empno" property="empno"></id>
<result column="ename" property="ename"></result>
<result column="job" property="job"></result>
<result column="mgr" property="mgr"></result>
<result column="hiredate" property="hiredate"></result>
<result column="sal" property="sal"></result>
<result column="comm" property="comm"></result>
</collection>
</resultMap>
<select id="selectDeptEmpMapping" resultMap="deptEmpMapping" >
select e.*,d.* from emp e,dept d where e.deptno=d.deptno
</select>
/*配置 一对多关联映射 一个部门对应多个员工*/
@Test
public void selectDeptEmpMapping() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
List<Dept> deptList = empMapper.selectDeptEmpMapping();
for (Dept dept : deptList) {
System.out.println(dept+"---"+dept.getEmpVos());
}
sqlSession.close();
}
标签:return,String,映射,void,private,Mybatis,deptno,public,04
From: https://www.cnblogs.com/oyww-2027/p/17716855.html