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 -
夢想是咸魚
關注一下吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM