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是为了指定映射到集合的类型,即集合的泛型
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值