Mybatis结果集映射(resultMap)

Posted 滑稽404#

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis结果集映射(resultMap)相关的知识,希望对你有一定的参考价值。

resultMap:描述数据库属性列与实体类属性之间的映射关系

一、数据库属性列与实体类属性字段不一致

public class User {
    private int id;
    private String username;
    private String pwd;
}
CREATE TABLE USER(
	id INT PRIMARY KEY AUTO_INCREMENT,
	username VARCHAR(15) DEFAULT NULL,
	password VARCHAR(15) DEFAULT NULL
);

password字段不一致,会导致获取到的结果集pwd为空
这种情况就需要AS起别名、传Map参数、已经resultMap描述映射关系

	<resultMap id="userResultMap" type="User">
                <!-- 数据库属性列      实体类属性-->
        <result column="password" property="pwd"/>
    </resultMap>
    <!--查询-->
    <select id="getAll" resultMap="userResultMap">
        select * from user;
    </select>

二、多对一关联(对象属性)

CREATE TABLE student(
	id INT PRIMARY KEY,
	NAME VARCHAR(10),
	tid INT
);

CREATE TABLE teacher(
	id INT PRIMARY KEY,
	NAME VARCHAR(10)
);

对学生来说,只有一位老师

public class Student {
    private int id;
    private String name;
    Teacher teacher;
}
public class Teacher {
    private int id;
    private String name;
}
List<Student> getAllStudent();

1、根据查询嵌套(SQL子查询)

	<select id="getTeacher" resultType="Teacher">
        SELECT * FROM teacher WHERE id=#{tid}
    </select>
    <!--根据查询嵌套-->
    <!--子查询-->
    <resultMap id="studentResultMap" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
                                        <!--teacher字段由子查询给出 返回的javaType为Teacher-->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>

    <select id="getAllStudent" resultMap="studentResultMap">
        SELECT * FROM student
    </select>

2、根据结果嵌套(联表查询)

	<resultMap id="studentResultMap2" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" column="tid" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

    <!--根据结果嵌套-->
    <select id="getAllStudent2" resultMap="studentResultMap2">
        SELECT S.id sid,S.name sname,T.name tname
        FROM student S,teacher T
        WHERE T.id=S.tid
    </select>

三、一对多集合(集合属性)

public class Student {
    private int id;
    private String name;
    private int tid;
}
public class Teacher {
    private int id;
    private String name;
    private List<Student> students;
}    

1、根据结果嵌套

	<resultMap id="teacherResultMap" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>
    <select id="getTeacherById" resultMap="teacherResultMap">
        SELECT T.id tid,T.name tname,S.id sid,S.name sname,tid
        FROM teacher T,student S
        WHERE T.id=#{id} AND S.tid=T.id
    </select>

2、根据查询嵌套

	<select id="getStudents" resultType="Student">
        SELECT * FROM student WHERE tid=#{tid}
    </select>

    <resultMap id="teacherResultMap2" type="Teacher">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <collection property="students" column="id" javaType="ArrayList" ofType="Student" select="getStudents"/>
    </resultMap>

    <select id="getTeacherById2" resultMap="teacherResultMap2">
        SELECT * FROM teacher WHERE id=#{id}
    </select>

因为子查询是根据教师id查的,所以property=“students” 需要映射column=“id”

3、查询所有老师

	<resultMap id="allResultMap" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

    <select id="getAllTeacher" resultMap="allResultMap">
        SELECT T.id tid,T.name tname,S.id sid,S.name sname,tid
        FROM student S,teacher T
        WHERE S.tid=T.id
    </select>

4、javaType&ofType

  • javaType是为了指定实体类中属性类型
  • ofType是为了指定映射到集合的类型,即集合的泛型

以上是关于Mybatis结果集映射(resultMap)的主要内容,如果未能解决你的问题,请参考以下文章

mybatis: resultMap 结果集映射和多表查询

[mybatis]select_resultMap_自定义结果集映射规则

mybatis ResultMap详解

Mybatis 高级结果映射 ResultMap Association Collection

ResultMap

Mybatis 高级结果映射 ResultMap Association Collection