溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mybatis-plus中resultMap怎么用

發(fā)布時間:2021-11-23 11:06:27 來源:億速云 閱讀:389 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下mybatis-plus中resultMap怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

不一致,那么用來接收查詢出來的result對應(yīng)的數(shù)據(jù)將會是Null,如果不使用resultMap,那么一般為了避免pojo對象對應(yīng)的屬性為Null,會采用SQL語句中的別名,將查詢出的數(shù)據(jù)庫中的字段as pojo對象屬性,而且,resultMap支持延遲加載

mybatis-plus中resultMap怎么用

但是為了避免sql語句看著 臃腫,所以就使用了resultMap

  簡單使用

 1.    選定你要進行的resultMap映射的model,如下是我要進行映射的model,model=======>Category.java【進行resultMap的pojo類】

package com.atguigu.gulimall.product.vo;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Category {
    private Long catId;
    private String name;
    private Long parentCid;
    private Integer catLevel;
    private Integer status;
    private Integer sort;
    private String iconImage;
    private String userName;
    private String unit;
    private Integer count;
    private Integer isDelete;
}

             注意

                   不要在你要映射成resultMap的pojo類上加lombok的@Accessors(chain=true)的鏈?zhǔn)骄幊痰淖⒔?,因為resultMap中的屬性會報紅【但是仍可使用】,如下圖這樣

                  mybatis-plus中resultMap怎么用

2.       在mapper.xml中編寫resultMap,并在要使用的sql查詢語句里,指定result標(biāo)簽為resultMap和其名稱

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
<mapper namespace="com.atguigu.gulimall.product.dao.CategoryDao">
	<!-- 可根據(jù)自己的需求,是否要使用 -->
    <resultMap type="com.atguigu.gulimall.product.vo.Category" id="categoryMap">
        <result property="catId" column="cat_id"/>
        <result property="name" column="name"/>
        <result property="parentCid" column="parent_cid"/>
        <result property="catLevel" column="cat_level"/>
        <result property="status" column="show_status"/>
        <result property="sort" column="sort"/>
        <result property="iconImage" column="icon"/>
        <result property="count" column="product_count"/>
    </resultMap>
     <!--直接在sql查詢返回的結(jié)果里指定resultMap-->
    <select id="queryByCid" parameterType="int" resultMap="categoryMap">
        select * from pms_category where cat_id = #{cId}
    </select>
</mapper>

         resultMap中各標(biāo)簽代表含義

type: resultMap最終映射的java對象,可以使用別名【如果使用resultMap實際返回的對象類型】
id:   resultMap的唯一標(biāo)識【隨便起】
result: 對普通名映射定義
property: type指定的返回的pojo對象中的屬性名  寫category里的屬性名
column: 數(shù)據(jù)庫中要查詢出的字段【列名】           寫要映射的數(shù)據(jù)庫表里的字段名
property對應(yīng)column形成數(shù)據(jù)庫中字段和pojo屬性對應(yīng)關(guān)系

          注意

              如果此時resultMap在另外一個nameSpace里邊,即其他的mapper文件里,那么在跨mapper使用時resultMap注明命名空間

3.  測試,直接運行項目

            如果在數(shù)據(jù)庫里查詢的字段個數(shù)少于或多于resultMap,那么依然能夠映射成功,就映射對應(yīng)的字段,因為名稱映射不上或者pojo里沒有的字段對應(yīng)的屬性,那么直接在返回的pojo 對象里返回null和沒有該字段

                                      mybatis-plus中resultMap怎么用

以上是“mybatis-plus中resultMap怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI