Mybatis:resultMap的使用总结

柔情只为你懂 2022-05-07 04:53 214阅读 0赞

Mybatis的介绍以及使用:http://www.mybatis.org/mybatis-3/zh/index.html
转载自:https://www.cnblogs.com/kenhome/p/7764398.html

resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中。
resultMap包含的元素:

  1. <!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性-->
  2. <resultMap id="唯一的标识" type="映射的pojo对象">
  3. <id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性" />
  4. <result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/>
  5. <association property="pojo的一个对象属性" javaType="pojo关联的pojo对象">
  6. <id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主席属性"/>
  7. <result column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>
  8. </association>
  9. <!-- 集合中的property须为oftype定义的pojo对象的属性-->
  10. <collection property="pojo的集合属性" ofType="集合中的pojo对象">
  11. <id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" />
  12. <result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" />
  13. </collection>
  14. </resultMap>

如果collection标签是使用嵌套查询,格式如下:

  1. <collection column="传递给嵌套查询语句的字段参数" property="pojo对象中集合属性" ofType="集合属性中的pojo对象" select="嵌套的查询语句" >
  2. </collection>

注意:标签中的column:要传递给select查询语句的参数,如果传递多个参数,格式为column= ” {参数名1=表字段1,参数名2=表字段2} ;
以下以实例介绍resultMap的用法:

一、简单需求:一个商品的结果映射;

1、创建商品pojo对象:

  1. public class TShopSku {
  2. /**
  3. * 主键ID
  4. */
  5. private Long id;
  6. /**
  7. * 商品名
  8. */
  9. private String skuName;
  10. /**
  11. * 分类ID
  12. */
  13. private Long categoryId;
  14. /**
  15. * 主键ID
  16. * @return ID
  17. */
  18. public Long getId() {
  19. return id;
  20. }
  21. /**
  22. * 主键ID,
  23. * @param id
  24. */
  25. public void setId(Long id) {
  26. this.id = id;
  27. }
  28. /**
  29. * 商品名
  30. * @return SKU_NAME 商品名
  31. */
  32. public String getSkuName() {
  33. return skuName;
  34. }
  35. /**
  36. * 商品名
  37. * @param skuName 商品名
  38. */
  39. public void setSkuName(String skuName) {
  40. this.skuName = skuName == null ? null : skuName.trim();
  41. }
  42. /**
  43. * 分类ID
  44. * @return CATEGORY_ID 分类ID
  45. */
  46. public Long getCategoryId() {
  47. return categoryId;
  48. }
  49. /**
  50. * 分类ID
  51. * @param categoryId 分类ID
  52. */
  53. public void setCategoryId(Long categoryId) {
  54. this.categoryId = categoryId;
  55. }

对应的resultMap:

  1. <resultMap id="BaseResultMap" type="com.meikai.shop.entity.TShopSku">
  2. <id column="ID" jdbcType="BIGINT" property="id" />
  3. <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
  4. <result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
  5. </resultMap>

二、商品pojo类添加属性集合:

一个商品会有一些属性,现在需要将查询出的商品属性添加到商品对象中,首先需要在原商品pojo类的基础上中添加属性的集合:

  1. /**
  2. * 属性集合
  3. */
  4. private List<TShopAttribute> attributes;
  5. /**
  6. * 获得属性集合
  7. */
  8. public List<TShopAttribute> getAttributes() {
  9. return attributes;
  10. }
  11. /**
  12. * 设置属性集合
  13. * @param attributes
  14. */
  15. public void setAttributes(List<TShopAttribute> attributes) {
  16. this.attributes = attributes;
  17. }

将Collection标签添加到resultMap中,这里有两种方式:

1、嵌套结果:

对应的resultMap:

  1. <resultMap id="BasePlusResultMap" type="com.meikai.shop.entity.TShopSku">
  2. <id column="ID" jdbcType="BIGINT" property="id" />
  3. <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
  4. <result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
  5. <collection property="attributes" ofType="com.meikai.shop.entity.TShopAttribute" >
  6. <id column="AttributeID" jdbcType="BIGINT" property="id" />
  7. <result column="attribute_NAME" jdbcType="VARCHAR" property="attributeName" />
  8. </collection>
  9. </resultMap>

查询语句:

  1. <select id="getById" resultMap="basePlusResultMap">
  2. select s.ID,s.SKU_NAME,s.CATEGORY_ID,a.ID,a.ATTRIBUTE_NAME
  3. from t_shop_sku s,t_shop_attribute a
  4. where s.ID =a.SKU_ID and s.ID = #{id,jdbcType =BIGINT};
  5. </select>

2、关联的嵌套查询(在collection中添加select属性):

商品结果集映射resultMap:

  1. <resultMap id="BasePlusResultMap" type="com.meikai.shop.entity.TShopSku">
  2. <id column="ID" jdbcType="BIGINT" property="id" />
  3. <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
  4. <result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
  5. <collection column="{skuId=ID}" property="attributes" ofType="com.meikai.shop.entity.TShopAttribute" select="getAttribute" >
  6. </collection>
  7. </resultMap>

collection的select会执行下面的查询属性语句:

  1. <select id="getAttribute" resultMap="AttributeResultMap">
  2. select a.ID,s.ATTRIBUTE_NAME
  3. from t_shop_attribute a
  4. where a.ID = #{skuId,jdbcType =BIGINT};
  5. </select>

属性结果集映射:

  1. <resultMap id="AttributeResultMap" type="com.meikai.shop.entity.TShopAttribute">
  2. <id column="ID" jdbcType="BIGINT" property="id" />
  3. <result column="ATTRIBUTE_NAME" jdbcType="VARCHAR" property="attributeName" />
  4. </resultMap>

BasePlusResultMap包含了属性查询语句的Collection

所以通过下面的查询商品语句就可获得商品以及其包含的属性集合:

  1. <select id="getById" resultMap="BasePlusResultMap">
  2. select s.ID,s.SKU_NAME,s.CATEGORY_ID
  3. from t_shop_sku s
  4. where s.ID = #{id,jdbcType =BIGINT};
  5. </select>

发表评论

表情:
评论列表 (有 0 条评论,214人围观)

还没有评论,来说两句吧...

相关阅读

    相关 UDP使用总结

    UDP是短连接,作为客户端的时候, 就两步: 1、socket 2、sendto 没有tcp的connect操作,使用netstat -an  参数也无法查看udp连接

    相关 rsync使用总结

    rsync远程同步:rsync是以文件名来区分的文件是否已经同步,文件同步之后进程会自动中断 rync的 核心算法介绍: 假定在名为α和β的两台计算机之间同步相似的文件