Mybatis-----查询结果合并成集合

在这里插入图片描述
如上查询结果,通过主表左连接出来多条记录。
目前的查询结果共有12条记录,但是根据 id来看,实际上只有2条主表记录,其它的都是
通过主表左连接出来的。

如何让查询结果只有2条?

MyBatis提供了合并的语法

@Data
public class Subsystem {
    private int subsystemId;
    private String subsystemKey;
    private String subsystemName;
    private Integer sn;
    private boolean selected;
    //持有Page对象集合
    private List<Page> pages;
}

@Data
public class Page {
    private int pageId;
    private String pageKey;
    private String pageName;
    private boolean selected;
    private Integer parentPageId;
    private Integer sn;
    private List<Page> subPages;
}
    <select id="list" resultMap="listWithPage">
         SELECT
             ts.id,
             ts.subsystem_key,
             ts.subsystem_name,
             tp.id as page_id,
             tp.page_key,
             tp.page_name,
             ts.SN,
             tp.SN as page_sn,
             tp.parent_page_id
         FROM t_subsystem ts
         LEFT JOIN t_page tp ON ts.id = tp.subsystem_id
         ORDER BY ts.SN , tp.SN
    </select>

    <resultMap id="listWithPage"  
               <!-- 这里返回最终的对象类型-->
               type="${package}.Subsystem">
        <id property="subsystemId" column="id"/>
        <result property="subsystemName" column="subsystem_name"/>
        <result property="subsystemKey" column="subsystem_key"/>
        <result property="sn" column="sn"/>
        <collection property="pages" 
                       <!-- 这里填写持有的集合对象类型-->
                     ofType="${package}.Page">
            <id property="pageId" column="page_id"/>
            <result property="pageKey" column="page_key"/>
            <result property="pageName" column="page_name"/>
            <result property="sn" column="page_sn"/>
            <result property="parentPageId" column="parent_page_id"/>
        </collection>
    </resultMap>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
batis-plus是Mybatis的增强工具,在分页查询方面提供了很好的支持。下面是mybatis-plus分页递归查询的步骤: 1.在实体类中添加@TableField注解,指定父节点id的字段名和子节点集合的字段名。 2.在Mapper接口中添加递归查询方法,使用@Select注解指定SQL语句,使用@Param注解指定参数。 3.在XML文件中编写递归查询SQL语句,使用WITH RECURSIVE关键字实现递归查询,使用UNION ALL关键字将多个查询结果合并。 4.在Service层中调用递归查询方法,使用Page对象实现分页查询。 下面是mybatis-plus分页递归查询的示例代码: 1.实体类: ```java @Data @TableName("tree") public class Tree { @TableId(type = IdType.AUTO) private Integer id; private String name; @TableField("parent_id") private Integer parentId; @TableField(exist = false) private List<Tree> children; } ``` 2.Mapper接口: ```java public interface TreeMapper extends BaseMapper<Tree> { @Select("WITH RECURSIVE cte(id, name, parent_id) AS (SELECT id, name, parent_id FROM tree WHERE id = #{id} UNION ALL SELECT t.id, t.name, t.parent_id FROM tree t JOIN cte ON t.parent_id = cte.id) SELECT * FROM cte") List<Tree> selectChildrenById(@Param("id") Integer id, Page<Tree> page); } ``` 3.XML文件: ```xml <select id="selectChildrenById" resultMap="BaseResultMap"> WITH RECURSIVE cte(id, name, parent_id) AS ( SELECT id, name, parent_id FROM tree WHERE id = #{id} UNION ALL SELECT t.id, t.name, t.parent_id FROM tree t JOIN cte ON t.parent_id = cte.id ) SELECT * FROM cte LIMIT #{page.offset}, #{page.size} </select> ``` 4.Service层: ```java @Service public class TreeService extends ServiceImpl<TreeMapper, Tree> { public IPage<Tree> selectChildrenById(Integer id, Page<Tree> page) { List<Tree> list = baseMapper.selectChildrenById(id, page); page.setRecords(list); return page; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小大宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值