就像这样,查出主表的实体Xxx并加上关联表的ids
被关联的表table3,本文以下内容称为“子表”
@Data
public class XxxVO extends Xxx {
/** 关联表的rIds */
private List rIds;
/** 关联表的rIds数据名称 */
private List names;
}
用mybatis直接实现映射Object实体类的List rIds,不用分步多查询并代码循环处理
表之间的关系: table1为主表,table2是中间表(table1关联table3),table3是要查询的关联数据表《子表》。
当前SQL中,是先查询 子查询,关联表的数据。因为主表与子表是1对多的关系,所以中间表table2会有多条数据,再被主表左连接。
"records": [
{
"id": "1277860509421953026",
"name": "name1",
"rIds": ["1","2"]
},
{
"id": "2277860509421953026",
"name": "name2",
"rIds": ["3","4"]
}
]
然后你会发现,这样的处理(LEFT JOIN关联分页)会有问题!虽然能实现想要的一个效果,但在分页的时候会产生问题,如下图返回的结果集。
SELECT t1.id AS id, t1.name AS name, t.r_id AS rIds FROM table1 AS t1
LEFT JOIN
(SELECT t2.id, t2.r_id
FROM table2 AS t2
LEFT JOIN table3 AS t3
ON t2.r_id = t3.id) AS t
ON t1.id = t.id LIMIT ?,?
解析:
Service调Mapper的getList,先执行了查主表table1的操作。映射resultMap=“Result”
resultMap中包含子查询,再去查询
property 对应实体类中的变量名
ofType集合类型
select子查询对应定义的id=getRelation
column是主表的id传过来的(重要),不然WHERE后面的#{id},是取不到的
SQL查询简洁log:
c.s.c.s.o.d.G.getList : ==> Preparing: select t1.id as id, t1.name as name from table1 as t1 LIMIT ?,?
c.s.c.s.o.dao.XxxMapper.getRelation : ====> Preparing: select t2.id from table2 as t2 LEFT JOIN table3 as t3 ON t2.id = t3.id WHERE t2.t1_id = ?
结果集也是这样的
"records": [
{
"id": "1277860509421953026",
"name": "name1",
"rIds": ["1","2"]
},
{
"id": "2277860509421953026",
"name": "name2",
"rIds": ["3","4"]
}
]
前端循环一下处理
data.records.forEach(group => {
group.roleIds = group.roles.map(role => role.id);
group.roleNames = group.roles.map(role => role.name);
})
collection中的property好像就只能绑定一个,查询了半天,暂不晓得能不能直接这么实现
有大佬可以给我指点一下嘛,能不能这样/??用mybatis把id跟name集合一起查询出来并返回,哈哈