最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

autoMapping和autoMappingBehavior的區(qū)別及說(shuō)明

 更新時(shí)間:2022年01月20日 15:58:33   作者:Androidbo  
這篇文章主要介紹了autoMapping和autoMappingBehavior的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

autoMapping和autoMappingBehavior的區(qū)別

autoMappingBehavior

mybatis核心配置文件中settings中配置,指定 MyBatis 應(yīng)如何自動(dòng)映射列到字段或?qū)傩浴?NONE 表示取消自動(dòng)映射;PARTIAL 只會(huì)自動(dòng)映射沒(méi)有定義嵌套結(jié)果集映射的結(jié)果集。 FULL 會(huì)自動(dòng)映射任意復(fù)雜的結(jié)果集(無(wú)論是否嵌套)。默認(rèn)是partial,這是一種全局設(shè)置

autoMapping

在resultMap或者association,collections中使用,是一個(gè)局部開(kāi)關(guān),開(kāi)啟后會(huì)自動(dòng)設(shè)置嵌套查詢(xún)中的屬性,局部開(kāi)關(guān)優(yōu)先級(jí)大于全部開(kāi)關(guān),當(dāng)全部開(kāi)關(guān)開(kāi)啟FULL映射時(shí),局部開(kāi)關(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=安陽(yáng)]]

去掉restult,不開(kāi)啟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é)果,可以看出在嵌套查詢(xún)中,mybatis默認(rèn)設(shè)置嵌套查詢(xún)不自動(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é)果,沒(méi)有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=安陽(yáng)]]

小結(jié)一下

autoMappingBehavior是<settings>里面的,是全局總開(kāi)關(guān)。autoMapping是<resultMap>里面的,是局部select語(yǔ)句映射開(kāi)關(guān)。

局部開(kāi)關(guān)優(yōu)先級(jí)大于全局開(kāi)關(guān)。

如上resultMap配置了autoMapping, 那么mybatis會(huì)自動(dòng)把查詢(xún)出來(lái)的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無(wú)法正確賦值的問(wèn)題。

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)查詢(xú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í)開(kāi)啟autoMapping 又使用指定列和類(lèi)屬性方式的二者取其一就行。

自動(dòng)映射可以通過(guò)columnPrefix指定前綴以及返回在sql中設(shè)置別名的方式來(lái)映射。這樣就可以用手動(dòng)去collection中寫(xiě)<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í)保證別名和類(lèi)屬性名符合映射規(guī)則。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

栖霞市| 龙胜| 黎川县| 巫山县| 永川市| 买车| 兰考县| 双柏县| 北宁市| 湖北省| 景德镇市| 平泉县| 安徽省| 永州市| 兴文县| 兴文县| 石泉县| 安岳县| 延庆县| 特克斯县| 乌兰察布市| 新津县| 且末县| 绥阳县| 绥阳县| 瓮安县| 镇雄县| 赤水市| 赣榆县| 芜湖县| 连州市| 皋兰县| 望谟县| 温泉县| 武胜县| 陇川县| 双桥区| 荥阳市| 洞口县| 鱼台县| 那坡县|