MyBatis-Plus 多个动态 where 条件

MyBatis-Plus About 1,098 words

场景

在编写自定义SQL时,可能会有子查询,所以出现多个where条件的情况。

限制

目前MyBatis-Plus只支持一个自定义参数ew

改造

假设2where条件的SQL

处理 Wrapper

QueryWrapper<Test> where1 = new QueryWrapper<>();
QueryWrapper<Test> where2 = new QueryWrapper<>();

String where1Str = where1.getCustomSqlSegment().replaceAll("ew", "where1");
String where2Str = where2.getCustomSqlSegment().replaceAll("ew", "where2");

List<Test> list = testMapper.stat(where1, where2, where1Str, where2Str);

Mapper

Mapper中传入4参数。分别是Wrapper对象和转换后的字符串对象。

@Mapper
public interface TestMapper extends BaseMapper<Test> {

    @Select("""
            select tmp.*, t1.name, t2.name from (
                select t1.name
                from table1 t1
                left join table2 t2 on t1.t_id = t2.id
                ${where1Str}
            ) tmp
            left join table3 t3 on t1.t_id = t3.id
            ${where2Str}
            """)
    List<Test> stat(@Param("where1") Wrapper<Test> where1,
                    @Param("where2") Wrapper<Test> where2,
                    @Param("where1Str") String where1Str,
                    @Param("where2Str") String where2Str);

}
Views: 72 · Posted: 2024-10-30

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh