您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“autoMapping和autoMappingBehavior的區(qū)別有哪些”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“autoMapping和autoMappingBehavior的區(qū)別有哪些”這篇文章吧。
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è)置
在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=采購(gòu), 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=采購(gòu), phone=222, card=IdCard [cardId=2222, address=安陽]]
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 在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ī)則。
以上是“autoMapping和autoMappingBehavior的區(qū)別有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。