欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

autoMapping和autoMappingBehavior的区别及说明

程序员文章站 2022-03-02 23:10:44
目录automapping和automappingbehavior的区别automappingbehaviorautomapping例子小结一下mybaits collection使用automapp...

automapping和automappingbehavior的区别

automappingbehavior

mybatis核心配置文件中settings中配置,指定 mybatis 应如何自动映射列到字段或属性。 none 表示取消自动映射;partial 只会自动映射没有定义嵌套结果集映射的结果集。 full 会自动映射任意复杂的结果集(无论是否嵌套)。默认是partial,这是一种全局设置

automapping

在resultmap或者association,collections中使用,是一个局部开关,开启后会自动设置嵌套查询中的属性,局部开关优先级大于全部开关,当全部开关开启full映射时,局部开关关闭,这时候仍然不会进行映射。

例子

配置信息,mybatis的settings全部为默认配置,我们测试局部自动映射的结果

    <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>

测试结果

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>

结果,可以看出在嵌套查询中,mybatis默认设置嵌套查询不自动映射,必须的有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进行测试

<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>

结果,没有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=李四, jobs=采购, phone=222, card=idcard [cardid=2222, address=安阳]]

小结一下

automappingbehavior是<settings>里面的,是全局总开关。automapping是<resultmap>里面的,是局部select语句映射开关。

局部开关优先级大于全局开关。

如上resultmap配置了automapping, 那么mybatis会自动把查询出来的name、id、cartid都赋值给customer, 如果automappng设为false, 则不会自动映射, 需要你在resultmap中手动配置result,它的作用在collection和association标签中作用是一样的。

此外, 配置automapping这个属性的优先级高于automappingbehavior, 也就是即使你automappingbehavior配置为full, 但是automapping配置为false, 那么依旧不会自动映射。

在嵌套影射中通常会同时配置上columnprefix属性, 这样的话可以在一定程度上避免因为实体属性名相同导致mybatis无法正确赋值的问题。

mybaits collection使用automapping注意点

mybaits 在resultmap 中使用automapping 时出现以下情况

<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 中设置了automapping,也指定了映射字段的列和属性名,会出现关联查询时collection返回是null,会直接映射成空对象

  id : 1,
  persons: [
     {
      personid:null,
      personname:null
     }
  ]

实验几次后发现去掉automaping就不会出现这种情况

  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>

一般不会出现同时开启automapping 又使用指定列和类属性方式的二者取其一就行。

自动映射可以通过columnprefix指定前缀以及返回在sql中设置别名的方式来映射。这样就可以用手动去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中映射的别名必须以设置好的前缀相同,同时保证别名和类属性名符合映射规则。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。