lambdaWrapper进行多条件查询时,如何使用
我们知道数据库语句比较常用条件查询,但是多条件怎么办?
形如
select *
from table1
where
a = '''
and
b = ''
and c='''
and
(d = 1 or d = 2 or = d = 3)
使用lambdaWrapper时候,要充分使用.and()和.or()
使用的代码如下
@Override
public List<MallShopApptOrderEntity> listByCustomer(String customerId, LocalDateTime firstDay, LocalDateTime lastDay) {
LambdaQueryWrapper<MallShopApptOrderEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(MallShopApptOrderEntity::getCustomerId, customerId)
.ge(MallShopApptOrderEntity::getPayTime, firstDay)
.le(MallShopApptOrderEntity::getPayTime, lastDay)
.and(m -> m.eq(MallShopApptOrderEntity::getOrderStatus, 1).
or().eq(MallShopApptOrderEntity::getOrderStatus, 2).
or().eq(MallShopApptOrderEntity::getOrderStatus, 3).
or().eq(MallShopApptOrderEntity::getOrderStatus, 4))
.and(ms -> ms.eq(MallShopApptOrderEntity::getPayMethod, 2).
or().eq(MallShopApptOrderEntity::getPayMethod, 3).
or().eq(MallShopApptOrderEntity::getPayMethod, 5).
or().eq(MallShopApptOrderEntity::getPayMethod, 6).
or().eq(MallShopApptOrderEntity::getPayMethod, 7));
return this.list(wrapper);
}
编译出来的sql语句如下
SELECT
*
FROM mall_shop_appt_order
WHERE
(
customer_id = '1559017565590265856' AND pay_time >= "2022-09-01" AND pay_time <= "2022-09-30"
AND ((order_status = 1 OR order_status = 2 OR order_status = 3 OR order_status = 4))
AND ((pay_type = 2 OR pay_type = 3 OR pay_type = 5 OR pay_type = 6 OR pay_type = 7))
)