服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - mybatis深入讲解resultMap的定义及用法

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

2022-11-25 14:53羡羡ˇ Java教程

MyBatis的每一个查询映射的返回类型都是ResultMap,当我们提供返回类型属性是resultType时,MyBatis会自动给我们把对应值赋给resultType所指定对象的属性,当我们提供返回类型是resultMap时,将数据库中列数据复制到对象的相应属性上,可

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

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

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

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

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

建立部门表和员工表

?
1
2
3
4
5
6
7
8
9
10
11
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(员工)类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 +
                '}';
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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层接口 : 

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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代码中我们就可以得到我们想要的映射格式

查询部门 : 

?
1
2
3
4
public interface DeptDao {
    //查询部门
    List<Dept> selectDept();
}

定义与使用resultMap

?
1
2
3
4
5
6
7
8
9
10
11
12
<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 我们就可以得到自己想要的映射关系

到此这篇关于mybatis深入讲解resultMap的定义及用法的文章就介绍到这了,更多相关mybatis resultMap内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/xx12321q/article/details/123809440

延伸 · 阅读

精彩推荐