慎用mybatis里的columnPrefix(会导致多层嵌套映射失效)

 今天使用单表查省市区三级数据联动,发现mybatis嵌套映射时,第二级的children(也就是第三级 区级别)死活映射不出来数据。

原始配置:

<resultMap id="AreaResultMap" type="com.jz.own.dev.entity.DevDatadicDetail">
    <id column="id" jdbcType="CHAR" property="id" />
    <result column="sort" jdbcType="VARCHAR" property="sort" />
    <result column="label" jdbcType="VARCHAR" property="label" />
    <result column="val" jdbcType="VARCHAR" property="val" />
    <result column="pid" jdbcType="VARCHAR" property="pid" />
    <result column="order_number" jdbcType="INTEGER" property="orderNumber" />
    <result column="del" jdbcType="CHAR" property="del" />
    <collection property="children" ofType="com.jz.own.dev.entity.DevDatadicDetail" resultMap="BaseAreaMap" columnPrefix="c_">
      <collection property="children" ofType="com.jz.own.dev.entity.DevDatadicDetail" resultMap="BaseAreaMap" columnPrefix="c2_">
      </collection>
    </collection>
  </resultMap>

 愿因是columnPrefix会使每个子标签内的column属性前面加上对应的前缀,使第二级的children属性对应的collection标签内的column也被加上了前缀(虽然没有指定出来),导致字段不匹配从而无法映射到对象中。

 修改后:

<resultMap id="AreaResultMap" type="com.jz.own.dev.entity.DevDatadicDetail">
  <id column="id" jdbcType="CHAR" property="id" />
  <result column="sort" jdbcType="VARCHAR" property="sort" />
  <result column="label" jdbcType="VARCHAR" property="label" />
  <result column="val" jdbcType="VARCHAR" property="val" />
  <result column="pid" jdbcType="VARCHAR" property="pid" />
  <result column="order_number" jdbcType="INTEGER" property="orderNumber" />
  <result column="del" jdbcType="CHAR" property="del" />
  <collection property="children" ofType="com.jz.own.dev.entity.DevDatadicDetail">
    <id column="c_id" jdbcType="CHAR" property="id" />
    <result column="c_sort" jdbcType="VARCHAR" property="sort" />
    <result column="c_label" jdbcType="VARCHAR" property="label" />
    <result column="c_val" jdbcType="VARCHAR" property="val" />
    <result column="c_pid" jdbcType="VARCHAR" property="pid" />
    <result column="c_order_number" jdbcType="INTEGER" property="orderNumber" />
    <result column="c_del" jdbcType="CHAR" property="del" />
    <collection property="children" ofType="com.jz.own.dev.entity.DevDatadicDetail" resultMap="BaseAreaMap" columnPrefix="c2_">
    </collection>
  </collection>
</resultMap>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis嵌套映射(Nested Mapping)是MyBatis框架用于处理复杂数据结构和关联查询的一种高级映射技术。在传统的一对一、一对多和多对多的关系映射,当数据模型包含嵌套的对象或者集合时,MyBatis嵌套映射就显得尤为重要。 嵌套映射允许你在SQL查询结果集直接获取嵌套的对象或集合,而不需要显式地进行多次对象操作。它通常通过`<select>`标签的`resultType`或`resultMap`元素来配置,同时可能涉及到`association`、`collection`、`discriminator`等元素来指定关联关系的处理方式。 例如,如果你有一个`User`对象,它有一个`Address`对象作为嵌套,你可以这样配置: ```xml <select id="selectUserWithAddress" resultType="com.example.User"> SELECT * FROM user LEFT JOIN address ON user.id = address.user_id </select> <resultMap id="userResultMap" type="com.example.User"> <id property="id" column="user_id"/> <result property="name" column="user_name"/> <!-- 使用association来映射嵌套的Address --> <association property="address" javaType="com.example.Address" select="selectAddressById"> <id property="id" column="address_id"/> <result property="street" column="street"/> </association> </resultMap> <select id="selectAddressById" parameterType="int" resultType="com.example.Address"> SELECT * FROM address WHERE id = #{id} </select> ``` 在这个例子,`selectUserWithAddress`方法返回一个包含`User`对象的结果集,其的`address`属性被自动解析为从`selectAddressById`查询得到的`Address`对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值