victory的博客

长安一片月,万户捣衣声

0%

MyBatis | 多对一查询自定义映射

多对一查询自定义映射

1.项目目录

2.Emp.java

package com.atguigu.bean;

public class Emp {
    private Integer eid;
    private String ename;
    private Integer age;
    private String sex;
    private Dept dept;
    
    
    public Dept getDept() {
        return dept;
    }
    public void setDept(Dept dept) {
        this.dept = dept;
    }
    public Integer getEid() {
        return eid;
    }
    public void setEid(Integer eid) {
        this.eid = eid;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    @Override
    public String toString() {
        return "Emp [eid=" + eid + ", ename=" + ename + ", age=" + age + ", sex=" + sex + ", dept=" + dept + "]";
    }
    public Emp(Integer eid, String ename, Integer age, String sex) {
        super();
        this.eid = eid;
        this.ename = ename;
        this.age = age;
        this.sex = sex;
    }
    public Emp() {
        super();
        // TODO Auto-generated constructor stub
    }
}

3.Dept.java

package com.atguigu.bean;

public class Dept {
    private Integer did;
    private String dname;
    public Integer getDid() {
        return did;
    }
    public void setDid(Integer did) {
        this.did = did;
    }
    public String getDname() {
        return dname;
    }
    public void setDname(String dname) {
        this.dname = dname;
    }
    @Override
    public String toString() {
        return "Dept [did=" + did + ", dname=" + dname + "]";
    }
    
}

4.EmpDeptMapper.java

package com.atguigu.mapper;

import java.util.List;

import com.atguigu.bean.Emp;

public interface EmpDeptMapper {
    List<Emp> getAllEmp();
}

5.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.atguigu.mapper.EmpDeptMapper">
    
    <!-- <select id="getAllEmp" resultType="Emp">
        select e.eid,e.ename,e.age,e.sex,e.did,d.dname from emp e left join dept d on e.did=d.did
        
            查询结果:
            [Emp [eid=1, ename=张三, age=12, sex=男, dept=null], 
            不能够查询出员工所对应的部门,需要进行自定义映射
        
    </select> -->
    
    <!-- 自定义映射-第一种方式 -->
    <!-- <resultMap type="Emp" id="empMap">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <result column="did" property="dept.did"/>
        <result column="dname" property="dept.dname"/>
    </resultMap> -->
    <!--
        <resultMap>:自定义映射,处理复杂的表关系
        <id/>:设置主键的映射关系,column设置字段名,property设置属性名
        <result/>:设置非主键的映射关系, column设置字段名,property设置属性名
     -->
     
     <!-- 自定义映射-第二种方式 -->
     <resultMap type="Emp" id="empMap">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <association property="dept" javaType="Dept">
            <id column="did" property="did"/>
            <result column="dname" property="dname"/>
        </association>
     </resultMap>
    
    <!-- List<Emp> getAllEmp(); -->
    <select id="getAllEmp" resultMap="empMap">
        <!-- select * from emp e, dept d where e.did = d.did -->
        select e.eid,e.ename,e.age,e.sex,e.did,d.dname from emp e left join dept d on e.did=d.did
    </select>
</mapper>

6.TestEmpDept.java

package com.atguigu.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.atguigu.bean.Emp;
import com.atguigu.mapper.EmpDeptMapper;

public class TestEmpDept {
    public static void main(String[] args) throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmpDeptMapper mapper = sqlSession.getMapper(EmpDeptMapper.class);
        
        List<Emp> empList = mapper.getAllEmp();
        System.out.println(empList);
    }
}