Mybatis入门-一对一关联查询(八)-resultType示例

这里我们完成一个案例
查询员工信息及对应部门的部门信息(这里一个员工只会隶属于一个部门下)

目录结构

在这里插入图片描述


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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值