resultMap的用法以及关联结果集映射 超详细

转自:https://blog.csdn.net/qq_42780864/article/details/81429114

resultType

resultType可以把查询结果封装到pojo类型中,但必须pojo类的属性名和查询到的数据库表的字段名一致。
如果sql查询到的字段与pojo的属性名不一致,则需要使用resultMap将字段名和属性名对应起来,进行手动配置封装,将结果映射到pojo中

resultMap

resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。
这里写图片描述

先在Mapper文件中,配置基本的sql语句

<!-- 查询所有的订单数据 -->
    <!-- resultMap:填入配置的resultMap标签的id值 -->
    <select id="queryOrderAll" resultMap="orderResultMap">
        SELECT id, user_id,
        number,
        createtime, note FROM `order`
    </select>

配置resultMap标签,映射不同的字段和属性名

<!-- resultMap最终还是要将结果映射到pojo上,type就是指定映射到哪一个pojo -->
    <!-- id:设置ResultMap的id -->
    <resultMap type="order" id="orderResultMap">
        <!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id -->
        <!-- property:主键在pojo中的属性名 -->
        <!-- column:主键在数据库中的列名 -->
        <id property="id" column="id" />

        <!-- 定义普通属性 -->
        <result property="userId" column="user_id" />
        <result property="number" column="number" />
        <result property="createtime" column="createtime" />
        <result property="note" column="note" />
    </resultMap>

结果就可以封装到pojo类型中

使用resultMap进行关联查询
一对一查询

一对一数据模型:订单用户
一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询。如果从用户信息出发查询用户下的订单信息则为一对多查询,因为一个用户可以下多个订单。
这里写图片描述

  • 改造pojo类
    在订单类中添加User属性,User属性是一个引用类型,用于存储关联查询的用户信息,因为关联关系是一对一,所以只需要添加单个属性即可
    这里写图片描述

  • 配置Mapper.xml配置文件
    OrderMapper.xml
    先使用id和result属性,映射order类的结果集,然后在使用association映射关联对象User的结果集

<resultMap type="order" id="orderUserResultMap">
    <id property="id" column="id" />
    <result property="userId" column="user_id" />
    <result property="number" column="number" />
    <result property="createtime" column="createtime" />
    <result property="note" column="note" />

    <!-- association :配置一对一属性 -->
    <!-- property:order里面的User属性名 -->
        <!-- javaType:属性类型 -->
    <association property="user" javaType="user">
        <!-- id:声明主键,表示user_id是关联查询对象的唯一标识-->
        <id property="id" column="user_id" />
        <result property="username" column="username" />
        <result property="address" column="address" />
    </association>

</resultMap>

<!-- 一对一关联,查询订单,订单内部包含用户属性 -->
<select id="queryOrderUserResultMap" resultMap="orderUserResultMap">
    SELECT
    o.id,
    o.user_id,
    o.number,
    o.createtime,
    o.note,
    u.username,
    u.address
    FROM
    `order` o
    LEFT JOIN `user` u ON o.user_id = u.id
</select>
  • 测试
@Test
public void testQueryOrderUserResultMap() {
    // mybatis和spring整合,整合之后,交给spring管理
    SqlSession sqlSession = this.sqlSessionFactory.openSession();
    // 创建Mapper接口的动态代理对象,整合之后,交给spring管理
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    // 使用userMapper执行根据条件查询用户,结果封装到Order类中
    List<Order> list = userMapper.queryOrderUserResultMap();
    for (Order o : list) {
        System.out.println(o);
    }
    // mybatis和spring整合,整合之后,交给spring管理
    sqlSession.close();
}
  • 结果
    这里写图片描述
一对多查询

查询所有用户信息及相关订单。

  • 修改pojo类,在pojo类添加订单集合属性
    这里写图片描述

  • 修改UserMapper.xml配置文件
    先使用id和result配置映射User类的结果,然后使用一对多关系的collection标签配置Order结果

<resultMap type="user" id="userOrderResultMap">
    <id property="id" column="id" />
    <result property="username" column="username" />
    <result property="birthday" column="birthday" />
    <result property="sex" column="sex" />
    <result property="address" column="address" />

    <!-- 配置一对多的关系
        property:填写pojo类中集合类类属性的名称
        javaType:填写集合类型的名称 
    -->
    <collection property="orders" javaType="list" ofType="order">
        <!-- 配置主键,是关联Order的唯一标识 -->
        <id property="id" column="oid" />
        <result property="number" column="number" />
        <result property="createtime" column="createtime" />
        <result property="note" column="note" />
    </collection>
</resultMap>

<!-- 一对多关联,查询订单同时查询该用户下的订单 -->
<select id="queryUserOrder" resultMap="userOrderResultMap">
    SELECT
    u.id,
    u.username,
    u.birthday,
    u.sex,
    u.address,
    o.id oid,
    o.number,
    o.createtime,
    o.note
    FROM
    `user` u
    LEFT JOIN `order` o ON u.id = o.user_id
</select>
  • 测试结果
    这里写图片描述
MyBatis的结果映射是指将查询结果与Java对象之间的映射关系进行配置和管理,使得查询结果可以自动转化为Java对象。在MyBatis中,我们可以使用resultMap来定义结果映射关系。 resultMap是一种自定义的映射关系,它可以将查询结果的列名与Java对象的属性名进行关联。在resultMap中,我们可以定义具体的映射关系,例如: ```xml <resultMap id="userResultMap" type="User"> <id property="id" column="user_id"/> <result property="username" column="user_name"/> <result property="password" column="user_password"/> <result property="email" column="user_email"/> </resultMap> ``` 在上面的例子中,我们定义了一个名为userResultMapresultMap,它与User类关联。该resultMap中通过id、result等元素定义了查询结果列与Java对象属性之间的映射关系。 当我们执行查询语句时,MyBatis会自动将查询结果转化为Java对象。例如: ```java public interface UserMapper { @Select("select * from user where id = #{id}") @ResultMap("userResultMap") public User getUserById(Integer id); } ``` 在上面的代码中,我们定义了一个getUserById方法,它使用了id参数查询用户信息。同时,我们在该方法上使用了@ResultMap注解,指定了查询结果要映射到User对象上,使用了之前定义的userResultMap。 当我们执行该方法时,MyBatis会自动将查询结果映射为User对象,并返回该对象。这样,我们就可以方便地将查询结果转化为Java对象,便于后续的处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值