【Java】Mybatis使用Collection属性

前言

这篇文章实现一下不使用left join等连接关键字来实现Mybatis的联表查询功能。包括json类型数据映射到Java实体类中。

库表

父表db1_json

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zIuG3a9a-1689752532289)(C:\Users\Admin\AppData\Roaming\marktext\images\2023-07-19-15-19-09-image.png)]

子表db1_json_attach,子表parent_id对应副本的id

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LMlft32m-1689752532290)(C:\Users\Admin\AppData\Roaming\marktext\images\2023-07-19-15-19-47-image.png)]

实体类

新建Db1JsonDTO数据传递对象实体。

@Data
public class Db1JsonDTO {
    //父表id
    private Long id;

    //父表信息
    private JSONObject info;

    //子表数据
    private List<Db1JsonAttach> attaches;
}

查看子表实体属性

@TableName(value ="db1_json_attach")
@Data
public class Db1JsonAttach implements Serializable {
    private Long parentId;

    @TableId(type = IdType.ID_WORKER)
    private Long id;

    @TableField(typeHandler = JacksonTypeHandler.class)
    private JSONObject info;

    private static final long serialVersionUID = 1L;
}

Mapper.xml

父xml处理,sql没什么好看的,看一下<resultMap>中各个属性吧。

<resultMap id="Db1JsonDTOResultMap" type="com.it.dto.Db1JsonDTO">
    <result column="info" property="info" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
  
    <collection property="attaches" column="{parentId = id}"
                select="com.it.mapper.Db1JsonAttachMapper.getAttachList"/>
</resultMap>
<select id="selectDb1JsonList" resultMap="Db1JsonDTOResultMap">
    select id,info from db1_json
</select>
  • id:自定义id,在SQL查询后使用resultMap属性并指定id返回。

  • type:返回字段对应的实体类,也可以是DTO,实体需要有Getter、Setter方法。

  • <result column:数据库中的字段名称。

  • <result property:与数据库字段对应的属性名。

  • <result typeHandler:对特殊类型进行处理,例如当前为json类型,指定将返回的结果进行转化后映射到实体属性中。

  • <collection property:父实体中的list集合数据,即为子实体类的集合。

  • <collection column:id字段赋值,将父id字段赋值给子id。

  • <collection select :指定子xmlSQL查询的方法id。

子xml处理

<resultMap id="BaseResultMap" type="com.it.entity.Db1JsonAttach">
        <id property="parentId" column="parent_id" jdbcType="BIGINT"/>
        <result property="id" column="id" jdbcType="BIGINT"/>
    <result property="info" column="info"  typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
</resultMap>


<select id="getAttachList" resultMap="BaseResultMap">
    select * from db1_json_attach where parent_id = #{parentId}
</select>

注:Mapper中不需要定义查询方法,只在XML中定义即可。

测试

看一下测试结果

[
  {
    "id": 1681557955655049218,
    "info": {
      "address": "洛杉矶",
      "name": "科比",
      "hobby": "直升机"
    },
    "attaches": [
      {
        "parentId": 1681557955655049218,
        "id": 1681557956250640385,
        "info": {
          "address": "洛杉矶",
          "name": "科比",
          "hobby": "直升机"
        }
      }
    ]
  },
  {
    "id": 1681558109766361089,
    "info": {
      "address": "美国",
      "name": "蔡徐坤",
      "hobby": "唱跳rap篮球"
    },
    "attaches": [
      {
        "parentId": 1681558109766361089,
        "id": 1681558109766361090,
        "info": {
          "address": "美国",
          "name": "蔡徐坤",
          "hobby": "唱跳rap篮球"
        }
      }
    ]
  },
  {
    "id": 1681558181665120257,
    "info": {
      "address": "理塘",
      "name": "丁真",
      "hobby": "测码"
    },
    "attaches": [
      {
        "parentId": 1681558181665120257,
        "id": 1681558181732229122,
        "info": {
          "address": "理塘",
          "name": "丁真",
          "hobby": "测码"
        }
      }
    ]
  }
]

总结

这个方式的联表查询用的不算太多,但是在一些特殊情况可以使用这种方式来完成查询子表的某一下数据集合。

如果对你有帮助请点个赞!

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值