溫馨提示×

溫馨提示×

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

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

mybatis-puls中resultMap數(shù)據(jù)映射的示例分析

發(fā)布時(shí)間:2021-08-31 15:12:55 來源:億速云 閱讀:197 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)mybatis-puls中resultMap數(shù)據(jù)映射的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

mybatis-puls resultMap數(shù)據(jù)映射

resultType

resultType可以把查詢結(jié)果封裝到pojo類型中,但必須pojo類的屬性名和查詢到的數(shù)據(jù)庫表的字段名一致。

如果sql查詢到的字段與pojo的屬性名不一致,則需要使用resultMap將字段名和屬性名對應(yīng)起來,進(jìn)行手動配置封裝,將結(jié)果映射到pojo中!

resultMap

resultMap可以實(shí)現(xiàn)將查詢結(jié)果映射為復(fù)雜類型的pojo,比如在查詢結(jié)果映射對象中包括pojo和list實(shí)現(xiàn)一對一查詢和一對多查詢。

  • 數(shù)據(jù)庫字段:user_id,

  • 實(shí)體類字段:userId

  • 需要手動配置設(shè)置resultMap

Mapper中基本查詢語句

<!-- 查詢所有的訂單數(shù)據(jù) -->
    <!-- resultMap:填入配置的resultMap標(biāo)簽的id值 -->
    <select id="queryOrderAll" resultMap="orderResultMap">
        SELECT id, user_id,
        number,
        createtime, note FROM `order`
    </select>

resultMap中字段映射

<!-- resultMap最終還是要將結(jié)果映射到pojo上,type就是指定映射到哪一個(gè)pojo -->
    <!-- id:設(shè)置ResultMap的id -->
    <resultMap type="order" id="orderResultMap">
        <!-- 定義主鍵 ,非常重要。如果是多個(gè)字段,則定義多個(gè)id -->
        <!-- property:主鍵在pojo中的屬性名 -->
        <!-- column:主鍵在數(shù)據(jù)庫中的列名 -->
        <id property="id" column="id" />
 
        <!-- 定義普通屬性 -->
        <result property="userId" column="user_id" />
        <result property="number" column="number" />
        <result property="createtime" column="createtime" />
        <result property="note" column="note" />
    </resultMap>

Mybatis ResultMap結(jié)果集映射

當(dāng)我們的POJO中的字段與數(shù)據(jù)庫中的字段不一致時(shí),在利用resultType進(jìn)行結(jié)果集映射時(shí),不一致的字段將會被賦值為null,這時(shí)我們可以利用ResultMap映射進(jìn)行賦值

POJO

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private String password;
}

數(shù)據(jù)庫字段

mybatis-puls中resultMap數(shù)據(jù)映射的示例分析

ResultType

<select id="getUserList" resultType="User">
    select * from user;
</select>

可以出數(shù)據(jù)庫中是pwd,而pojo中是password,這樣利用resultType是無法映射的,password查出來的結(jié)果為null

mybatis-puls中resultMap數(shù)據(jù)映射的示例分析

ResultMap

<select id="getUserList" resultMap="UserMap">
    select * from user;
</select>
<resultMap id="UserMap" type="User">
    <result column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="pwd" property="password"/>
</resultMap>

我們用resultMap替換resultType

UserMap與下面resultMap里的id屬性的值保持一致即可

type為返回值類型,這里我之所以是User是因?yàn)槲移鹆藙e名,如果沒有起別名要寫出完整的路徑

在resultMap中,column代表數(shù)據(jù)庫中的列,也就是數(shù)據(jù)庫里的字段,property為數(shù)據(jù)庫中的字段映射的結(jié)果,這里我們與pojo保持一致即可,數(shù)據(jù)庫中的pwd被映射為password,pojo里的password也就能賦值成功了

mybatis-puls中resultMap數(shù)據(jù)映射的示例分析

關(guān)于“mybatis-puls中resultMap數(shù)據(jù)映射的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

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

AI