mybatis深入讲解resultMap的定义及用法

来自:网络
时间:2022-04-27
阅读:
目录

            我们知道 ,mybatis框架存在pojo对象映射 , 直接将查询到的结果封装到对象中给我们返回, 但如果数据库的中的列和java中类属性名就是不一致,或者如果我们实际返回的对象需要去关联其他的对象(也就是说,其他类的对象作为我们这个类的成员变量),那么这时候使用resultType肯定是不行的

    这里我们则需要去定义 resultMap来完成我们的需求

    定义resultMap的过程就是描述如何从数据库结果集中去加载对象

    resultMap多用于多表查询间的映射关系, 例如 :

    我们以部门和员工为例 , 一个部门有多个员工 , 一个员工属于一个部门

    建立部门表和员工表

    CREATE TABLE dept(      -- 部门表
       id INT PRIMARY KEY AUTO_INCREMENT,
       NAME VARCHAR(10)
    )
         
    CREATE TABLE employee(  -- 员工表
       id INT PRIMARY KEY AUTO_INCREMENT,
       NAME VARCHAR(10),
       age INT,
       deptId INT
    )

    在java中创建 Dept(部门)类 和 Employee(员工)类

    public class Employee {       //员工类
        private Integer id;
        private String name;
        private Integer age;
        // 一个员工关联一个部门
        private Dept dept;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public Dept getDept() {
            return dept;
        }
        public void setDept(Dept dept) {
            this.dept = dept;
        }
        @Override
        public String toString() {
            return "Employee{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", dept=" + dept +
                    '}';
        }
    }
    public class Dept {
        private Integer id;
        private String name;
        // 一个部门有多个员工 ,使用List集合存储
        private List<Employee> list;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public List<Employee> getList() {
            return list;
        }
        public void setList(List<Employee> list) {
            this.list = list;
        }
        @Override
        public String toString() {
            return "Dept{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", list=" + list +
                    '}';
        }
    }

    首先我们查询员工 , 定义dao层接口 : 

    public interface EmployeeDao {
        // 查询所有员工
        List<Employee> selectAllEmployee();
    }

    由于我们在对象中关联了其他对象, 所以已经不是普通映射 ,这里我们定义 resultMap

        <resultMap id="employeeMap" type="Employee">
            <!--主键列-->
            <id column="id" property="id"/>
            <!--其他属性映射-->
            <result property="name" column="ename"/>
            <result property="age" column="age"/>
            <!--一对一关联-->
            <association property="dept" javaType="Dept">
                <!--需要映射的对象属性-->
                <result property="name" column="dname"/>
            </association>
        </resultMap>
        <select id="selectAllEmployee" resultMap="employeeMap">
            SELECT e.id,e.name ename,e.age,d.name dname FROM employee e
                     LEFT JOIN dept d ON e.deptId = d.id
        </select>

    resultMap 中的id 是唯一标识 , 相当于名字 , type类型是我们要返回的类型

    <select>中使用resultMap , 传入刚定义的id即可

    这样在java代码中我们就可以得到我们想要的映射格式

    查询部门 : 

    public interface DeptDao {
        //查询部门
        List<Dept> selectDept();
    }

    定义与使用resultMap

    <resultMap id="deptMap" type="Dept">
            <id column="id" property="id"/>
            <result column="dname" property="name"/>
            <!--collection为一对多 , 这里一个部门包含多个员工-->
            <collection property="list" javaType="List" ofType="Employee">
                <result property="name" column="ename"/>
            </collection>
        </resultMap>
        <select id="selectDept" resultMap="deptMap">
             SELECT d.id,d.name dname,e.name ename FROM dept d
                  LEFT JOIN employee e ON d.id = e.deptId
        </select>

    这里 JavaType我们选择list , 因为用list集合来存储多个员工信息, ofType是list集合中实际包含的对象名,这里是员工 Employee

    通过resultMap 我们就可以得到自己想要的映射关系

    返回顶部
    顶部