mybatis-plus QueryWrapper条件子句多重与或

鄙人在项目中遇到一个业务要计算月同比环比,需要查询一个时间字段中多个时间范围的记录,实现的SQL示例如下:

select * from fault_happen where train_code = '710' and fault_level = '3' and 
(happen_time >= '2020-07-01 00:00:00' and happen_time < '2020-08-01 00:00:00'
OR happen_time >= '2020-06-01 00:00:00' and happen_time < '2020-07-01 00:00:00' 
OR happen_time >= '2019-07-01 00:00:00' and happen_time < '2019-08-01 00:00:00')
;

但是用QueryWrapper怎么拼接这个嵌套多个与或的条件呢?
如果无视条件子句中的圆括号()直接拼接逻辑是不对的,
需要用到.and(java.util.function.Function func)这个方法:
实现的Java代码如下:

wrapper.and(x ->
                x.ge("happen_time", currentMonthHead).lt("happen_time", currentMonthTail)
                        .or().ge("happen_time", lastMonthHead).lt("happen_time", lastMonthTail)
                        .or().ge("happen_time", lastYearHead).lt("happen_time", lastYearTail)
            );

你可能感兴趣的:(MySQL,mysql,mybatis)