MyBatis resultMap与resultType

本文详细介绍了MyBatis中resultMap和resultType的区别。当表字段与实体类属性名不一致或者需要处理多表查询时,resultMap显得更为灵活。resultMap可以通过配置实现字段映射,甚至可以处理复杂的一对一、一对多关系映射,如示例所示,展示了如何通过association和collection标签将关联表的数据映射到实体类中。

resultMap和resultType一样都是为了将表查询的结果集映射到自定义的实体类中

但在使用MyBatis进行表查询的时候可能会遇到两个问题:表字段与实体类属性名不一致、多表查询需要将返回的结果集封装为多个实体类对象。这时候resultType便无法解决这两个问题,而resultMap可以根据配置信息将表字段与实体类属性名直接进行映射,不需要像resultType一样需要表字段和属性名一致才能进行映射

    <resultMap id="orderMap" type="order">
        <id column="oid" property="id"/>
        <result column="ordertime" property="ordertime"/>
        <result column="total" property="total"/>
        <result column="uid" property="user.id"/>
        <result column="username" property="user.username"/>
        <result column="password" property="user.password"/>
        <result column="birthday" property="user.birthday"/>
    </resultMap>

    <select id="findAll" resultMap="orderMap">
        select *,o.id oid from orders o, user u where o.uid = u.id
    </select>

而且resultMap可以根据association和collection标签将其他表的字段映射到实体类的引用数据类型属性中,主要用于多表查询

    <resultMap id="orderMap" type="order">
        <id column="oid" property="id"/>
        <result column="ordertime" property="ordertime"/>
        <result column="total" property="total"/>
<!--        <result column="uid" property="user.id"/>-->
<!--        <result column="username" property="user.username"/>-->
<!--        <result column="password" property="user.password"/>-->
<!--        <result column="birthday" property="user.birthday"/>-->
        <!-- property 指的是order中的成员变量名 -->
        <association property="user" javaType="user">
            <id column="uid" property="id"/>
            <result column="username" property="username"/>
            <result column="password" property="password"/>
            <result column="birthday" property="birthday"/>
        </association>
    </resultMap>
    
    <select id="findAll" resultMap="orderMap">
        select *,o.id oid from orders o, user u where o.uid = u.id
    </select>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.itheima.Mapper.UserMapper">
    
    <resultMap id="userMap" type="user">
        <id column="uid" property="id"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="birthday" property="birthday"/>
        
        <collection property="orderList" ofType="order">
            <id column="oid" property="id"/>
            <result column="ordertime" property="ordertime"/>
            <result column="total" property="total"/>
        </collection>
    </resultMap>
    
    <select id="findAll" resultMap="userMap">
        select *, o.id oid from orders o, user u where u.id = o.uid
    </select>

    <resultMap id="userAndRoleMap" type="user">
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>

        <collection property="roleList" ofType="role">
            <id column="roleId" property="id"/>
            <result column="roleName" property="roleName"/>
            <result column="roleDesc" property="roleDesc"/>
        </collection>
    </resultMap>

    <select id="findUserAndRoleAll" resultMap="userAndRoleMap">
        select *
        from sys_user as su
                 LEFT JOIN (select * from sys_role as sr, sys_user_role ur where sr.id = ur.roleId) as res
                           on res.userId = su.id;
    </select>
    
</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值