MyBatis resultMap元素详解

目录

一、resultType和resultMap

二、resultMap标签解析及resultMap创建举例

元素标签详解

resultMap创建举例

resultMap如何将结果映射到结果集

如何使用association元素嵌套结果映射(一般使用一对一查询)

如何使用collection元素嵌套结果映射(一般使用一对多查询)

三、测试代码举例(包含一对一关联查询)

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

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

使用resultMap进行关联查询

一对一查询


一、resultType和resultMap

resultType

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

resultMap

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

图片

二、resultMap标签解析及resultMap创建举例

元素标签详解

  • resultMap元素属性
  1.  id:映射规则集的唯一标识,可以被select元素的resultMap属性应用
  2. type:映射的结果类型,这里指定封装成userList实例
  • resultMap元素包含以下子元素
  1. id:指定和数据表主键字段对应的标识属性。设置此项可以提升MyBatis框架的性能,特别是应用缓存和嵌套结果映射的时候。
  2. result:指定结果集字段和实体类属性的映射关系。
  3. association:映射到JavaBean某个“复杂类型”属性,比如JavaBean类
  4. collection:映射到JavaBean某个“复杂类型”属性,比如集合

resultMap创建举例

resultMap如何将结果映射到结果集

<!--id是唯一的,对应resultMap的唯一标识名称  type是里面返回的类型-->
<resultMap id="userMap" type="User">
    <!--id标签表示表的主键-->
    <!--column表示数据库的列   property表示实体类的属性-->
    <id column="id" property="id"></id>
    <!--如果不是主键列,就用result表示-->
    <result column="roleName" property="userRoleName"></result>
</resultMap>

如何使用association元素嵌套结果映射(一般使用一对一查询)

/**
* 对用户和角色进行联表查询
* @return
*/
List<User> findAllUserAndRole();

<resultMap id="UserAndRole" type="User">
        <id property="id" column="id"></id>
        <result property="username" column="userName"></result>
        <!--association表示一个实体类-->
        <!--association对应User类的属性值,javaType表示属性值的类型-->
        <association property="role" javaType="Role">
            <id property="id" column="roleId"></id>
            <result property="roleName" column="roleName"></result>
        </association>
    </resultMap>
    <select id="findAllUserAndRole" resultMap="UserAndRole">
        select user.*,role.id as roleId,role.roleName from smbms_user user inner 		  join smbms_role role on user.userRole = role.id
    </select>

如何使用collection元素嵌套结果映射(一般使用一对多查询)

/**
* 根据用户ID查询所有的地址信息
* @return
*/
List<User> findAddressById(Long id);

    <resultMap id="UserAndAddressList" type="User">
        <id property="id" column="id"></id>
        <result property="username" column="userName"></result>
        <!-- 一对多的关系 -->
        <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
        <collection property="addressList" ofType="com.changan.entity.Address">
            <id property="id" column="addressId"></id>
            <result property="addressDesc" column="addressDesc"></result>
        </collection>
    </resultMap>
    <select id="findAddressById" resultMap="UserAndAddressList">
        select user.*,address.id as addressId,address.addressDesc from smbms_user 			user inner join smbms_address address on
        user.id = address.userId where user.id = #{id}
    </select>

三、测试代码举例(包含一对一关联查询)

先在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>

测试

@Testpublic 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();}

结果

图片

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值