前言
这篇文章实现一下不使用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)]](https://img-blog.csdnimg.cn/a581066bff0649cca5a2b120319c884a.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)]](https://img-blog.csdnimg.cn/1030e1f152654a0bad8be0cd53011a5b.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": "测码"
}
}
]
}
]
总结
这个方式的联表查询用的不算太多,但是在一些特殊情况可以使用这种方式来完成查询子表的某一下数据集合。


















