autoMapping和autoMappingBehavior的區(qū)別及說明
autoMapping和autoMappingBehavior的區(qū)別
autoMappingBehavior
mybatis核心配置文件中settings中配置,指定 MyBatis 應(yīng)如何自動映射列到字段或?qū)傩浴?NONE 表示取消自動映射;PARTIAL 只會自動映射沒有定義嵌套結(jié)果集映射的結(jié)果集。 FULL 會自動映射任意復(fù)雜的結(jié)果集(無論是否嵌套)。默認(rèn)是partial,這是一種全局設(shè)置
autoMapping
在resultMap或者association,collections中使用,是一個局部開關(guān),開啟后會自動設(shè)置嵌套查詢中的屬性,局部開關(guān)優(yōu)先級大于全部開關(guān),當(dāng)全部開關(guān)開啟FULL映射時,局部開關(guān)關(guān)閉,這時候仍然不會進(jìn)行映射。
例子
配置信息,mybatis的Settings全部為默認(rèn)配置,我們測試局部自動映射的結(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>
測試結(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è)置嵌套查詢不自動映射,必須的有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)行測試
<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)先級大于全局開關(guān)。
如上resultMap配置了autoMapping, 那么mybatis會自動把查詢出來的name、id、cartid都賦值給customer, 如果autoMappng設(shè)為false, 則不會自動映射, 需要你在resultMap中手動配置result,它的作用在collection和association標(biāo)簽中作用是一樣的。
此外, 配置autoMapping這個屬性的優(yōu)先級高于autoMappingBehavior, 也就是即使你autoMappingBehavior配置為FULL, 但是autoMapping配置為false, 那么依舊不會自動映射。
在嵌套影射中通常會同時配置上columnPrefix屬性, 這樣的話可以在一定程度上避免因為實體屬性名相同導(dǎo)致mybatis無法正確賦值的問題。
mybaits collection使用autoMapping注意點
mybaits 在resultMap 中使用autoMapping 時出現(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,也指定了映射字段的列和屬性名,會出現(xiàn)關(guān)聯(lián)查詢時collection返回是null,會直接映射成空對象
? id : 1, ? persons: [ ? ? ?{ ? ? ? personId:null, ? ? ? personName:null ? ? ?} ? ]
實驗幾次后發(fā)現(xiàn)去掉autoMaping就不會出現(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>
一般不會出現(xiàn)同時開啟autoMapping 又使用指定列和類屬性方式的二者取其一就行。
自動映射可以通過columnPrefix指定前綴以及返回在sql中設(shè)置別名的方式來映射。這樣就可以用手動去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è)置好的前綴相同,同時保證別名和類屬性名符合映射規(guī)則。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
通過idea創(chuàng)建Spring Boot項目并配置啟動過程圖解
這篇文章主要介紹了通過idea創(chuàng)建Spring Boot項目并配置啟動過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11一篇文章帶你搞定SpringBoot不重啟項目實現(xiàn)修改靜態(tài)資源
這篇文章主要介紹了一篇文章帶你搞定SpringBoot不重啟項目實現(xiàn)修改靜態(tài)資源,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09關(guān)于Spring?@Transactional事務(wù)傳播機(jī)制詳解
我們?nèi)粘9ぷ髦袠O少使用事務(wù)傳播級別,單純只是使用事務(wù)和rollbackfor拋出異常來解決事務(wù)問題,但其實我們很多時候使用的是不正確的,或者說會造成事務(wù)粒度過大,本文詳解一下事務(wù)傳播級別,也讓自己更好地處理事務(wù)問題,需要的朋友可以參考下2023-08-08詳解Java8新特性之interface中的static方法和default方法
這篇文章主要介紹了Java8新特性之interface中的static方法和default方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08SpringBoot JWT實現(xiàn)token登錄刷新功能
JWT本身是無狀態(tài)的,這點有別于傳統(tǒng)的session,不在服務(wù)端存儲憑證。這種特性使其在分布式場景,更便于擴(kuò)展使用。接下來通過本文給大家分享SpringBoot JWT實現(xiàn)token登錄刷新功能,感興趣的朋友一起看看吧2021-09-09SpringBoot整合Servlet和Filter和Listener組件詳解
這篇文章主要介紹了SpringBoot整合Servlet和Filter和Listener組件詳解,在整合某報表插件時就需要使用Servlet,Spring Boot中對于整合這些基本的Web組件也提供了很好的支持,需要的朋友可以參考下2024-01-01