MyBatis Plus自定义sql结合多个wrapper的解决方案

service

// begin end
LambdaQueryWrapper<Folder> endWp = commonWp.clone();
LambdaQueryWrapper<Folder> beginWp = commonWp.clone();
endWp.eq(Folder::getName, folderNameArr.get(folderNameArr.size() - 1));
beginWp.eq(Folder::getName, folderNameArr.get(0));

// 多申明两个传参,替换掉默认的ew占位符
String endWpstr = endWp.getCustomSqlSegment().replaceAll("ew", "endWp");
String beginWpstr = beginWp.getCustomSqlSegment().replaceAll("ew", "beginWp");
List<Folder> result = folderMapper.queryTreeListByFolderNames(endWp, beginWp, endWpstr, beginWpstr);

dao

@Select(value = {
        "WITH RECURSIVE resEnd AS (\n" +
                "    SELECT * FROM public.technical_folder ${endWpstr}\n" +
                "    UNION ALL\n" +
                "    SELECT b.* FROM resEnd a INNER JOIN public.technical_folder b ON a.parent_id = b.id\n" +
                "),resBegin AS (\n" +
                "    SELECT * FROM public.technical_folder ${beginWpstr}\n" +
                "    UNION ALL\n" +
                "    SELECT b.* FROM resBegin a INNER JOIN resEnd b ON a.id = b.parent_id\n" +
                ")SELECT a.* from resEnd a INNER JOIN resBegin b ON a.id = b.id ORDER BY a.level"
})
List<Folder> queryTreeListByFolderNames(@Param("endWp") Wrapper<Folder> endWp,
                                        @Param("beginWp") Wrapper<Folder> beginWp,
                                        @Param("endWpstr") String endWpstr,
                                        @Param("beginWpstr") String beginWpstr);
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐