Mybatis——ResultMap(结果集映射)的使用

ResultMap的使用

在Mybatis中,可以使用resultMap(结果集映射)作为sql的返回类型

一般用来解决如下问题:

  • 数据库表字段名和实体类属性名不一致的问题;
  • 多对一问题:
    • 例如:多个学生对应同一个老师,查询每个学生信息(包含老师对象属性)
  • 一对多问题:
    • 例如:一个老师教学多个学生,查询某个老师信息及其属下学生(包含学生列表)

1、字段名和属性名不一致问题

数据库表字段名和实体类属性名不一致的问题处理

id	name	pwd	//数据库字段名
id	name	password	//实体类属性名

Mapper中SQL语句编写如下:

<!--结果集映射-->
<resultMap id="UserMap" type="User">
    <!--column为数据库中的字段名,property为实体中的属性-->
    <result column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="pwd" property="password"/>
</resultMap>

<select id="getUserById" resultType="UserMap">
    select * from mybatis.user where id=#{id};
</select>

2、多对一处理

获得所有学生及其对应老师(多个学生对应一个老师)

Student类

@Data                              //get,set
@NoArgsConstructor                 //无参构造
@AllArgsConstructor                //有参构造
public class Student{
    int id;
    String name;
    Teacher teacher;
}

Teacher类

@Data                              //get,set
@NoArgsConstructor                 //无参构造
@AllArgsConstructor                //有参构造
public class Teacher{
    int id;
    String name;

    public Teacher() {
    }

    public Teacher(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

按照结果集嵌套处理

联表查询

<select id="getStudents" resultMap="StudentMap">
    select s.id sid, s.name sname, t.name tname
    from student s
    join teacher t
    on s.tid = t.id
</select>
<resultMap id="StudentMap" type="Student">
    <!--这里的column是与查询结果的字段名对应,字段重命名了则对应命名后的字段-->
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
    <!--复杂的属性需要单独处理 对象:association  集合:collection-->
    <association property="teacher" javaType="Teacher">
        <result property="name" column="tname"/>
    </association>
</resultMap>

按照查询嵌套处理

相当于子查询:

<select id="getStudent2" resultMap="StudentMap2">
    select * from student;
</select>

<select id="getTeacher" resultType="Teacher">
    select * from teacher where id = #{tid};
</select>

<!--这里将学生和对应老师分开查询,将查询结果组合-->
<resultMap id="StudentMap2" type="Student">
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
    
    <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>

查询结果:

Student{id=1, name='zhangsan', teacher=Teacher{id=1, name='spong'}}
Student{id=2, name='lisi', teacher=Teacher{id=1, name='spong'}}

3、一对多处理

按指定ID查询老师及其所管理的学生(一个老师对应多个学生)

Student类

@Data                              //get,set
@NoArgsConstructor                 //无参构造
@AllArgsConstructor                //有参构造
public class Student {
    int id;
    String name;
    int tid;

    public Student() {
    }

    public Student(int id, String name, int tid) {
        this.id = id;
        this.name = name;
        this.tid = tid;
    }
}

Teacher类

@Data                              //get,set
@NoArgsConstructor                 //无参构造
@AllArgsConstructor                //有参构造
public class Teacher {
    int id;
    String name;
    List<Student> students;
}

按照结果嵌套处理

<select id="getTeacherById" resultMap="TeacherById">
        select t.id id, t.name tname, s.id sid,s.name sname,s.tid tid
        from teacher t
        join student s
        on t.id = s.tid;
</select>

<resultMap id="TeacherById" type="Teacher">
    <result property="id" column="id"/>
    <result property="name" column="tname"/>
    <!--获取List<Student>中的泛型使用 ofType-->
    <collection property="students" ofType="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <result property="tid" column="tid"/>
    </collection>
</resultMap>

查询结果:

[Teacher{id=1, name='spong', students=[Student{id=1, name='zhangsan', tid=1}, Student{id=2, name='lisi', tid=1}]}]

小结:

  • 关联:association【多对一】
  • 集合:collection【一对多】
  • javaType & ofType
    • javaType 用来指定实体类中属性的类型
    • ofType 用来指定映射到List或者集合中pojo类型,即泛型的类型

如有错误,欢迎大佬指正!

posted @ 2020-08-10 00:21  想拥有两颗❤  阅读(466)  评论(0编辑  收藏  举报