当前位置: 移动技术网 > IT编程>数据库>Mysql > Mybatis之ResultMap的使用详解

Mybatis之ResultMap的使用详解

2019年09月21日  | 移动技术网IT编程  | 我要评论

mybatis的定义

mybatis

mybatis 是一款优秀的持久层框架,它支持定制化 sql、存储过程以及高级映射。mybatis 避免了几乎所有的 jdbc 代码和手动设置参数以及获取结果集。mybatis 可
以使用简单的 xml 或注解来配置和映射原生信息,将接口和 java 的 pojos(plain old java objects,普通的 java对象)映射成数据库中的记录。

mybaits如何使用,这里有一篇关于mybatis的介绍,没有使用过的童鞋可以参考,今天主要说的是resultmap。

reultmap

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

resultmap包含的元素:

<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性-->
<resultmap id="唯一的标识" type="映射的pojo对象">
<id column="表的主键字段,或者可以为查询语句中的别名字段" jdbctype="字段类型" property="映射pojo对象的主键属性" />
<result column="表的一个字段(可以为任意表的一个字段)" jdbctype="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属                        
性)"/>
<association property="pojo的一个对象属性" javatype="pojo关联的pojo对象">
<id column="关联pojo对象对应表的主键字段" jdbctype="字段类型" property="关联pojo对象的主席属性"/>
<result  column="任意表的字段" jdbctype="字段类型" property="关联pojo对象的属性"/>
</association>
<!-- 集合中的property须为oftype定义的pojo对象的属性-->
<collection property="pojo的集合属性" oftype="集合中的pojo对象">
<id column="集合中pojo对象对应的表的主键字段" jdbctype="字段类型" property="集合中pojo对象的主键属性" />
<result column="可以为任意表的字段" jdbctype="字段类型" property="集合中的pojo对象的属性" />  
</collection>
</resultmap>

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

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

注意:
collection标签中的column:要传递给select查询语句的参数,如果传递多个参数,格式为column= ” {参数名1=表字段1,参数名2=表字段2} ;

实例场景介绍resultmap的用法:


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


1、创建商品pojo对象:

public class tshopsku  {
    /**
     * 主键id
     */
    private long id;

    /**
     * 商品名
     */
private string skuname;

    /**
     * 分类id
     */
private long categoryid;


    /**
     * 主键id
     * @return id 
     */
public long getid() {
    return id;
}

    /**
     * 主键id,
     * @param id 
     */
public void setid(long id) {
    this.id = id;
}

    /**
     * 商品名
     * @return sku_name 商品名
     */
public string getskuname() {
    return skuname;
}

    /**
     * 商品名
     * @param skuname 商品名
     */
public void setskuname(string skuname) {
    this.skuname = skuname == null ? null : skuname.trim();
}

    /**
     * 分类id
     * @return category_id 分类id
     */
public long getcategoryid() {
    return categoryid;
}

    /**
     * 分类id
     * @param categoryid 分类id
     */
public void setcategoryid(long categoryid) {
    this.categoryid = categoryid;
}

对应的resultmap:

<resultmap id="baseresultmap" type="com.meikai.shop.entity.tshopsku">
    <id column="id" jdbctype="bigint" property="id" />
    <result column="sku_name" jdbctype="varchar" property="skuname" />
    <result column="category_id" jdbctype="bigint" property="categoryid" />
</resultmap> 

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

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

    /**
     * 获得属性集合
     */
public list<tshopattribute> getattributes() {
    return attributes;
}

    /**
     * 设置属性集合
     * @param attributes
     */
 public void setattributes(list<tshopattribute> attributes) {
    this.attributes = attributes;
 }

将collection标签添加到resultmap中,有两种方式

1、嵌套结果:
对应的resultmap:

<resultmap id="baseplusresultmap" type="com.meikai.shop.entity.tshopsku">
    <id column="id" jdbctype="bigint" property="id" />
    <result column="sku_name" jdbctype="varchar" property="skuname" />
    <result column="category_id" jdbctype="bigint" property="categoryid" />
    <collection property="attributes" oftype="com.meikai.shop.entity.tshopattribute" > 
        <id column="attributeid" jdbctype="bigint" property="id" />
        <result column="attribute_name" jdbctype="varchar" property="attributename" />
    </collection>
</resultmap>

查询语句:

<select id="getbyid"  resultmap="baseplusresultmap">
    select s.id,s.sku_name,s.category_id,a.id,a.attribute_name
    from t_shop_sku s,t_shop_attribute a 
    where s.id =a.sku_id and s.id = #{id,jdbctype =bigint};
</select>

2、关联的嵌套查询(在collection中添加select属性):
商品结果集映射resultmap:

<resultmap id="baseplusresultmap" type="com.meikai.shop.entity.tshopsku">
    <id column="id" jdbctype="bigint" property="id" />
    <result column="sku_name" jdbctype="varchar" property="skuname" />
    <result column="category_id" jdbctype="bigint" property="categoryid" />
    <collection column="{skuid=id}" property="attributes" oftype="com.meikai.shop.entity.tshopattribute" select="getattribute" > 
    </collection>
</resultmap>

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

<select id="getattribute"  resultmap="attributeresultmap">
    select a.id,s.attribute_name
    from t_shop_attribute a
    where  a.id = #{skuid,jdbctype =bigint};
</select>

属性结果集映射:

<resultmap id="attributeresultmap" type="com.meikai.shop.entity.tshopattribute">
    <id column="id" jdbctype="bigint" property="id" />
    <result column="attribute_name" jdbctype="varchar" property="attributename" />
</resultmap>

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

<select id="getbyid"  resultmap="baseplusresultmap">
    select s.id,s.sku_name,s.category_id
    from t_shop_sku s
    where  s.id = #{id,jdbctype =bigint};
</select>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网