Mybatis使用Collection属性的示例代码
作者:保加利亚的风
本文主要介绍了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)]](http://img.jbzj.com/file_images/article/202307/202307240952329.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)]](http://img.jbzj.com/file_images/article/202307/2023072409523210.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": "测码"
}
}
]
}
]总结
这个方式的联表查询用的不算太多,但是在一些特殊情况可以使用这种方式来完成查询子表的某一下数据集合。
到此这篇关于Mybatis使用Collection属性的示例代码的文章就介绍到这了,更多相关Mybatis Collection属性内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- mybatis collection关联查询多个参数方式
- MyBatis使用嵌套查询collection和association的实现
- mybatis resultMap之collection聚集两种实现方式
- mybatis中association和collection的使用与区别
- MyBatis的collection和association的使用解读
- Mybatis中一对多(collection)和一对一(association)的组合查询使用
- Mybatis的collection三层嵌套查询方式(验证通过)
- mybatis collection和association的区别解析
- MyBatis中<collection>标签的多种用法
