美文网首页java
MyBatis基于注解开发(一对多,多对一)

MyBatis基于注解开发(一对多,多对一)

作者: _FireFly_ | 来源:发表于2020-11-23 09:24 被阅读0次

Emp(一个员工对应一个部门)

package domain;
import java.sql.Date;
public class Emp {

    //自有属性
    private Integer empno;
    private String ename;
    private String job;
    private Integer mgr;
    private Date hiredate;
    private Float sal;
    private Float comm;
    //为了通过员工 查询员工对应的部门信息 关联属性
    private Dept dept;//(deptno dname loc)


    public Emp() {
    }

    public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, Float sal, Float comm, Dept dept) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.mgr = mgr;
        this.hiredate = hiredate;
        this.sal = sal;
        this.comm = comm;
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "empno=" + empno +
                ", ename='" + ename + '\'' +
                ", job='" + job + '\'' +
                ", mgr=" + mgr +
                ", hiredate=" + hiredate +
                ", sal=" + sal +
                ", comm=" + comm +
                ", 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 Date getHiredate() {
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public Float getSal() {
        return sal;
    }

    public void setSal(Float sal) {
        this.sal = sal;
    }

    public Float getComm() {
        return comm;
    }

    public void setComm(Float comm) {
        this.comm = comm;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }
}

Dept(一个部门下有多个员工)

public class Dept {
    //自有属性
    private Integer deptno;
    private String dname;
    private String loc;
    //为了通过部门查询到 部门中的所有员工 关联属性
    private List<Emp> empList;

    public Dept() {
    }
    public Dept(Integer deptno, String dname, String loc, List<Emp> empList) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
        this.empList = empList;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                ", empList=" + empList +
                '}';
    }

    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 List<Emp> getEmpList() {
        return empList;
    }

    public void setEmpList(List<Emp> empList) {
        this.empList = empList;
    }
}

EmpDao

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;

public interface EmpDao {

    //设计一个方法  根据员工empno编号 查询员工信息+所属部门信息
    @Results(
            id="selectEmp",
            value={
                    @Result(property="empno",column="empno",id=true),
                    @Result(property="ename",column="ename"),
                    @Result(property="job",column="job"),
                    @Result(property="mgr",column="mgr"),
                    @Result(property="hiredate",column="hiredate"),
                    @Result(property="sal",column="sal"),
                    @Result(property="comm",column="comm"),
                    @Result(property="dept",javaType=Dept.class,column="deptno",one=@One(select="selectDept",fetchType=FetchType.LAZY))
            })
    @Select("select * from emp where empno = #{empno}")
    public Emp selectOne(Integer empno);

    //辅助方法
    @Select("select * from dept where deptno = #{deptno}")
    public Dept selectDept(Integer deptno);
    //====================================================
    //查询所有的emp信息+每个emp对应的dept对象
    @Select("select * from emp")    //会通过辅助方法public Dept selectDept(Integer deptno);查询多次
    @ResultMap("selectEmp")       //结果集存放方式会调用上面的@Results
    public List<Emp> selectAll();
}

DeptDao

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;

public interface DeptDao {
    //设计一个方法  根据部门编号deptno   查询部门的信息+所属于当前部门的所有员工信息
    @Results(
            id="selectDept",
            value={
                    @Result(property="deptno",column="deptno",id=true),
                    @Result(property="dname",column="dname"),
                    @Result(property="loc",column="loc"),
                    @Result(property="empList",javaType=List.class,column="deptno",many=@Many(select="selectEmp",fetchType=FetchType.LAZY)),
            }
    )
    @Select("select * from dept where deptno = #{deptno}")
    public Dept selectOne(Integer deptno);

    //辅助方法
    @Select("select * from emp where deptno = #{deptno}")
    public Emp selectEmp(Integer deptno);

    //==========================================================

    @Select("select * from dept")    //通过多次调用 public Emp selectEmp(Integer deptno);为dept中的empList赋值
    @ResultMap("selectDept")       //结果集存放方式会调用上面的@Results
    public List<Dept> selectAll();
}

EmpService

public class EmpService {

    private EmpDao dao = MyUtil.getMapper(EmpDao.class,true);

    //设计一个业务方法  根据给定的员工empno编号 查询员工的信息+所属的部门信息
    public Emp selectOne(Integer empno){
        return dao.selectOne(empno);
    }

    //设计一个业务方法  查询表格中全部的emp信息+每个员工所属的部门信息
    public List<Emp> selectAll(){
        return dao.selectAll();
    }
}

DeptService

public class DeptService {

    private DeptDao dao = MyUtil.getMapper(DeptDao.class,true);

    //设计一个业务方法   给定部门编号  查询部门信息+所属于当前部门的所有员工信息
    public Dept selectOne(Integer deptno){
        return dao.selectOne(deptno);
    }

    //设计一个业务方法   查询全部的部门信息+每个部门中的所有员工信息
    public List<Dept> selectAll(){
        return dao.selectAll();
    }
}

相关文章

网友评论

    本文标题:MyBatis基于注解开发(一对多,多对一)

    本文链接:https://www.haomeiwen.com/subject/avfqbktx.html