这里我们完成一个案例
查询员工信息及对应部门的部门信息(这里一个员工只会隶属于一个部门下)
目录结构
EmpDept类
public class EmpDept {
//emp表属性
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Double sal;
private Double comm;
//关联外键
private Integer deptno;
//dept表属性
private String dname;
private String loc;
public EmpDept() {
}
public EmpDept(Integer empno, String ename, String job, Integer mgr, Date hiredate, Double sal, Double comm, Integer deptno, String dname, String loc) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
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 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 Double getComm() {
return comm;
}
public void setComm(Double comm) {
this.comm = comm;
}
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;
}
@Override
public String toString() {
return "EmpDept{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate=" + hiredate +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
'}';
}
}
EmpDeptMapper.java
public interface EmpDeptMapper {
List<EmpDept> selectAll();
}
EmpDeptMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.msb.mapper.EmpDeptMapper">
<select id="selectAll" resultType="empDept">
select e.empno,e.ename,e.job,e.deptno,d.dname,d.loc from emp e inner join dept d on e.deptno = d.deptno
</select>
</mapper>
测试类
public class MabatisTest {
private SqlSessionFactory factory;
@Before
public void init() throws IOException {
initLogRecord.initLog();
InputStream resource = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
factory = builder.build(resource);
}
@Test
public void test() throws Exception {
SqlSession sqlSession = factory.openSession();
//传入参数为想获得接口的实现类
EmpDeptMapper mapper = sqlSession.getMapper(EmpDeptMapper.class);
List<EmpDept> list = mapper.selectAll();
for (EmpDept ed : list) {
System.out.println(ed.getDeptno() + ";" + ed.getEname() + ";" + ed.getJob() + ";"
+ ed.getDeptno() + ";" + ed.getDname() + ";" + ed.getLoc());
}
sqlSession.close();
}
}
结果
使用resultType存在的问题
- 浪费了两组pojo,mapper.java,mapper.xml
- 多创建了一组pojo,mapper.java,mapper.xml