Mybatis使用Collection属性的示例代码
前言
这篇文章实现一下不使用left join
等连接关键字来实现Mybatis的联表查询功能。包括json
类型数据映射到Java实体类
中。
库表
父表db1_json
子表db1_json_attach
,子表parent_id
对应副本的id
。
实体类
新建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
:指定子xml
SQL查询的方法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": "测码" } } ] } ]
总结
这个方式的联表查询用的不算太多,但是在一些特殊情况可以使用这种方式来完成查询子表的某一下数据集合。
到此这篇关于Mybatis使用Collection属性的示例代码的文章就介绍到这了,更多相关Mybatis Collection属性内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
RestTemplate发送HTTP POST请求使用方法详解
这篇文章主要为大家介绍了RestTemplate发送HTTP POST请求的使用方法详解,有需要的朋友可以借鉴参考下希望能够有所帮助,祝大家多多进步2022-03-03SpringBoot RESTful 应用中的异常处理梳理小结
这篇文章主要介绍了SpringBoot RESTful 应用中的异常处理梳理小结,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-05-05关于 Math.random()生成指定范围内的随机数的公式推导问题
在 java 中,用于生成随机数的 Math 方法 random()只能生成 0-1 之间的随机数,而对于生成指定区间,例如 a-b 之间的随机数,却只能用相关计算公式,今天通过本文给大家介绍Math.random()生成随机数的公式推导问题,感兴趣的朋友一起看看吧2022-09-09
最新评论