autoMapping和autoMappingBehavior的區(qū)別及說明
autoMapping和autoMappingBehavior的區(qū)別
autoMappingBehavior
mybatis核心配置文件中settings中配置,指定 MyBatis 應(yīng)如何自動(dòng)映射列到字段或?qū)傩浴?NONE 表示取消自動(dòng)映射;PARTIAL 只會(huì)自動(dòng)映射沒有定義嵌套結(jié)果集映射的結(jié)果集。 FULL 會(huì)自動(dòng)映射任意復(fù)雜的結(jié)果集(無論是否嵌套)。默認(rèn)是partial,這是一種全局設(shè)置
autoMapping
在resultMap或者association,collections中使用,是一個(gè)局部開關(guān),開啟后會(huì)自動(dòng)設(shè)置嵌套查詢中的屬性,局部開關(guān)優(yōu)先級(jí)大于全部開關(guān),當(dāng)全部開關(guān)開啟FULL映射時(shí),局部開關(guān)關(guān)閉,這時(shí)候仍然不會(huì)進(jìn)行映射。
例子
配置信息,mybatis的Settings全部為默認(rèn)配置,我們測(cè)試局部自動(dòng)映射的結(jié)果
?? ?<select id="findCustomerByIdResultMap" parameterType="int" resultMap="CustomerResultMap">
?? ?SELECT
?? ??? ?id,
?? ??? ?username,
?? ??? ?jobs,
?? ??? ?phone,
?? ??? ?idCard.cardId as cardId,
?? ??? ?idcard.address as address
?? ??? ?FROM
?? ??? ?t_customer ,
?? ??? ?idcard
?? ??? ?WHERE t_customer.cardId=idcard.cardId and t_customer.id=#{id}
?? ?</select>
?? ?
?? ?<resultMap type="cn.edu.huel.po.Customer" id="CustomerResultMap">
?? ?<id column="id" property="id"/>
?? ?<result column="username" property="username"/>
?? ?<result column="jobs" property="jobs"/>
?? ?<result column="phone" property="phone"/>
?? ?<association ?property="card" ?javaType="cn.edu.huel.po.IdCard">
?? ??? ?<id column="cardId" property="cardId"/>
?? ??? ?<result column="address" property="address"/>
?? ?</association>測(cè)試結(jié)果
DEBUG [main] - ==> Preparing: SELECT id, username, jobs, phone, idCard.cardId as cardId, idcard.address as address FROM t_customer , idcard WHERE t_customer.cardId=idcard.cardId and t_customer.id=?
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <== Total: 1
Customer [id=2, username=李四, jobs=采購, phone=222, card=IdCard [cardId=2222, address=安陽]]
去掉restult,不開啟autoMapping
<select id="findCustomerByIdResultMap" parameterType="int" resultMap="CustomerResultMap">
?? ?SELECT
?? ??? ?id,
?? ??? ?username,
?? ??? ?jobs,
?? ??? ?phone,
?? ??? ?idCard.cardId as cardId,
?? ??? ?idcard.address as address
?? ??? ?FROM
?? ??? ?t_customer ,
?? ??? ?idcard
?? ??? ?WHERE t_customer.cardId=idcard.cardId and t_customer.id=#{id}
?? ?</select>
?? ?
?? ?<resultMap type="cn.edu.huel.po.Customer" id="CustomerResultMap">
?? ?<id column="id" property="id"/>
?? ?<association ?property="card" ?javaType="cn.edu.huel.po.IdCard">
?? ??? ?<id column="cardId" property="cardId"/>
?? ?</association>
?? ?</resultMap>結(jié)果,可以看出在嵌套查詢中,mybatis默認(rèn)設(shè)置嵌套查詢不自動(dòng)映射,必須的有result
DEBUG [main] - ==> Preparing: SELECT id, username, jobs, phone, idCard.cardId as cardId, idcard.address as address FROM t_customer , idcard WHERE t_customer.cardId=idcard.cardId and t_customer.id=?
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <== Total: 1
Customer [id=2, username=null, jobs=null, phone=null, card=IdCard [cardId=2222, address=null]]
加上autoMapping為ture進(jìn)行測(cè)試
<select id="findCustomerByIdResultMap" parameterType="int" resultMap="CustomerResultMap">
?? ?SELECT
?? ??? ?id,
?? ??? ?username,
?? ??? ?jobs,
?? ??? ?phone,
?? ??? ?idCard.cardId as cardId,
?? ??? ?idcard.address as address
?? ??? ?FROM
?? ??? ?t_customer ,
?? ??? ?idcard
?? ??? ?WHERE t_customer.cardId=idcard.cardId and t_customer.id=#{id}
?? ?</select>
?? ?
?? ?<resultMap type="cn.edu.huel.po.Customer" autoMapping="true" id="CustomerResultMap">
?? ??? ?<id column="id" property="id"/>
?? ??? ?<association ?property="card" autoMapping="true" ?javaType="cn.edu.huel.po.IdCard">
?? ??? ??? ?<id column="cardId" property="cardId"/>
?? ??? ?</association>
?? ?</resultMap>結(jié)果,沒有result,結(jié)果照樣映射
DEBUG [main] - ==> Preparing: SELECT id, username, jobs, phone, idCard.cardId as cardId, idcard.address as address FROM t_customer , idcard WHERE t_customer.cardId=idcard.cardId and t_customer.id=?
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <== Total: 1
Customer [id=2, username=李四, jobs=采購, phone=222, card=IdCard [cardId=2222, address=安陽]]
小結(jié)一下
autoMappingBehavior是<settings>里面的,是全局總開關(guān)。autoMapping是<resultMap>里面的,是局部select語句映射開關(guān)。
局部開關(guān)優(yōu)先級(jí)大于全局開關(guān)。
如上resultMap配置了autoMapping, 那么mybatis會(huì)自動(dòng)把查詢出來的name、id、cartid都賦值給customer, 如果autoMappng設(shè)為false, 則不會(huì)自動(dòng)映射, 需要你在resultMap中手動(dòng)配置result,它的作用在collection和association標(biāo)簽中作用是一樣的。
此外, 配置autoMapping這個(gè)屬性的優(yōu)先級(jí)高于autoMappingBehavior, 也就是即使你autoMappingBehavior配置為FULL, 但是autoMapping配置為false, 那么依舊不會(huì)自動(dòng)映射。
在嵌套影射中通常會(huì)同時(shí)配置上columnPrefix屬性, 這樣的話可以在一定程度上避免因?yàn)閷?shí)體屬性名相同導(dǎo)致mybatis無法正確賦值的問題。
mybaits collection使用autoMapping注意點(diǎn)
mybaits 在resultMap 中使用autoMapping 時(shí)出現(xiàn)以下情況
<collection property="persons" ? ?ofType="io.ztx.infra.dto.PersonDTO" autoMapping = "true"> ? ? <id property="id" column="person_id"/> ? ? <result property="name" column="person_name"/> </collection>
在collection 中設(shè)置了autoMapping,也指定了映射字段的列和屬性名,會(huì)出現(xiàn)關(guān)聯(lián)查詢時(shí)collection返回是null,會(huì)直接映射成空對(duì)象
? id : 1,
? persons: [
? ? ?{
? ? ? personId:null,
? ? ? personName:null
? ? ?}
? ]實(shí)驗(yàn)幾次后發(fā)現(xiàn)去掉autoMaping就不會(huì)出現(xiàn)這種情況
? id : 1, ? persons:[]
還有一種方法是把<result/> 換成<id/> 同樣能夠解決
<collection property="persons" ? ?ofType="io.ztx.infra.dto.PersonDTO" autoMapping = "true"> ? ? <id property="id" column="person_id"/> ? ? <id property="name" column="person_name"/> </collection>
一般不會(huì)出現(xiàn)同時(shí)開啟autoMapping 又使用指定列和類屬性方式的二者取其一就行。
自動(dòng)映射可以通過columnPrefix指定前綴以及返回在sql中設(shè)置別名的方式來映射。這樣就可以用手動(dòng)去collection中寫<id/> <resule/>了。
<collection property="persons" ? ?ofType="io.ztx.infra.dto.PersonDTO" autoMapping = "true" columnPrefix="p_"> </collection>
select? ? ? a.id as id, ? ? p.id as p_id, ? ? p.name as p_name ? ? fron A a ? ? left Join person p on p.id = a.pid
注意:SQL中映射的別名必須以設(shè)置好的前綴相同,同時(shí)保證別名和類屬性名符合映射規(guī)則。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot項(xiàng)目中kaptcha驗(yàn)證碼的使用方式
這篇文章主要介紹了Springboot項(xiàng)目中kaptcha驗(yàn)證碼的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Flink流處理引擎零基礎(chǔ)速通之?dāng)?shù)據(jù)的抽取篇
今天不分享基礎(chǔ)概念知識(shí)了,來分享一個(gè)馬上工作需要的場(chǎng)景,要做數(shù)據(jù)的抽取,不用kettle,想用flink。實(shí)際就是flink的sql、table層級(jí)的api2022-05-05
向量數(shù)據(jù)庫之如何使用Elasticsearch實(shí)現(xiàn)向量數(shù)據(jù)存儲(chǔ)與搜索
這篇文章主要介紹了向量數(shù)據(jù)庫之如何使用Elasticsearch實(shí)現(xiàn)向量數(shù)據(jù)存儲(chǔ)與搜索,在向量函數(shù)的計(jì)算過程中,會(huì)對(duì)所有匹配的文檔進(jìn)行線性掃描,因此,查詢預(yù)計(jì)時(shí)間會(huì)隨著匹配文檔的數(shù)量線性增長(zhǎng),本文給大家講解的非常詳細(xì),需要的朋友參考下吧2023-06-06
Feign遠(yuǎn)程調(diào)用傳遞對(duì)象參數(shù)并返回自定義分頁數(shù)據(jù)的過程解析
這篇文章主要介紹了Feign遠(yuǎn)程調(diào)用傳遞對(duì)象參數(shù)并返回自定義分頁數(shù)據(jù)的過程解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
spring-cloud入門之eureka-client(服務(wù)注冊(cè))
本篇文章主要介紹了spring-cloud入門之eureka-client(服務(wù)注冊(cè)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)
Hibernate想要實(shí)現(xiàn)雙向的關(guān)聯(lián)就必須在映射文件的兩端同時(shí)配置<one-to-one>,另外還要在主映射的一端采用foreign外鍵關(guān)聯(lián)屬性,下面我們就一起來看一下Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)方法:2016-06-06
Java如何解決發(fā)送Post請(qǐng)求報(bào)Stream?closed問題
這篇文章主要介紹了Java如何解決發(fā)送Post請(qǐng)求報(bào)Stream?closed問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

