Mybatis

前言

记录下最近项目中用到的Mybatis实体类与数据结果集的映射方式。


测试用例

  • 查询商品及其子项信息
  • Table
CREATE TABLE `product` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

CREATE TABLE `product_item` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `product_id` int(10) unsigned NOT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
  • ProductController.java
/**
 * 查询商品明细
 * @param id
 * @return
 */
@GetMapping("/{id}/detail")
public ProductDetailVo getDetail(@PathVariable(value = "id") Integer id) {
    return productService.getDetail(id);
}
  • ProductService.java
public ProductDetailVo getDetail(Integer id) {
    return productMapper.getDetail(id);
}
  • ProductMapper.java
/**
 * 查询明细
 * @param id
 * @return
 */
ProductDetailVo getDetail(Integer id);
  • Product.java
@Getter
@Setter
public class Product {
    private Integer id;
    private String title;
    private Date createTime;
}
  • ProductDetailVo.java
@Data
public class ProductDetailVo extends Product {
    private List<String> itemTitleList;
}

映射方式

resultType使用as指定别名

  • ProductMapper.xml
<select id="getDetail" resultType="com.coisini.mybatislearn.vo.ProductDetailVo">
     select a.id as id, a.title as title, a.create_time as createTime
         from product a
     where id = #{id}
 </select>
  • 查询结果:

在这里插入图片描述


reusultMap对应实体

  • ProductMapper.xml
<resultMap id="BaseResultMap" type="com.coisini.mybatislearn.model.Product">
    <id column="id" property="id"/>
    <result column="title" property="title"/>
    <result column="create_time" property="createTime"/>
</resultMap>

<!-- extends="BaseResultMap" 继承BaseResultMap的所有属性 -->
<resultMap id="DetailResultMap" type="com.coisini.mybatislearn.vo.ProductDetailVo" extends="BaseResultMap">

</resultMap>

<!-- reusultMap对应实体 -->
<select id="getDetail" resultMap="DetailResultMap">
    select * from product
        where id = #{id}
</select>
  • 查询结果:

在这里插入图片描述


Collection集合映射

  • 上述示例都没有查询Item子项,productItem子项是一对多的关系,这种情况多在java代码中处理,这里我们用Collection集合映射来查询一下
  • ProductMapper.xml
<resultMap id="BaseResultMap" type="com.coisini.mybatislearn.model.Product">
    <id column="id" property="id"/>
    <result column="title" property="title"/>
    <result column="create_time" property="createTime"/>
</resultMap>

<!-- 非同名转换 autoMapping 自动映射 -->
<resultMap autoMapping="true" id="DetailResultMap" type="com.coisini.mybatislearn.vo.ProductDetailVo">
    <id column="id" property="id"/>
    <!-- property 模型中映射的字段名 ofType 模型中映射的字段类型 -->
    <collection property="itemTitleList" ofType="java.lang.String">
        <constructor>
            <!-- column 指定数据集的字段名 -->
            <arg column="item_title"></arg>
        </constructor>
    </collection>
</resultMap>

<select id="getDetail" resultMap="DetailResultMap">
    select a.id, a.title, a.create_time,
           b.title as item_title
        from product a
            left join product_item b on a.id = b.product_id
    where a.id = #{id}
</select>
  • 如上所示,可通过collection标签将查询出来的数据集映射到指定的模型上,查询结果如下:

在这里插入图片描述


- End -
梦想是咸鱼
关注一下吧
以上为本篇文章的主要内容,希望大家多提意见,如果喜欢记得点个推荐哦
作者:Maggieq8324
本文版权归作者和博客园共有,欢迎转载,转载时保留原作者和文章地址即可。
原文地址:https://www.cnblogs.com/maggieq8324/p/15243348.html