Mybatis中and和循环or混用操作(or转换成in)

Mybatis and和循环or混用 这次项目用到一个and和or混用的场景 , 因为用到多个or(循环), 没想到好的办法
最终转换成用 IN实现:
场景
用left join链接多个表, 多个条件and筛选, 其中状态(state)条件筛选出多个可选状态的条目,
【Mybatis中and和循环or混用操作(or转换成in)】本来想用and 和 or 但是 or的条件是个数组参数, 需要遍历states , 可能0个可能多个, 拼了半天没有成功 , 最后发现用 IN 和FOREACH就可以了
DAO层接口

List selectOrdersByStatesSelective(@Param(value="https://www.it610.com/article/order")Order order,@Param(value="https://www.it610.com/article/states")Integer[] states);

Mybatis实现
select from order_listLEFT JOIN product_method ON product_method.`code` = order_list.purchase_methodLEFT JOIN product_color ON product_color.`code` = order_list.colorLEFT JOIN product_guarantee ON product_guarantee.`code` = order_list.guaranteeLEFT JOIN product_info ON order_list.product_id = product_info.idLEFT JOIN product_model ON product_info.model = product_model.`code`LEFT JOIN product_standard ON product_info.standard = product_standard.`code`LEFT JOIN product_state ON product_state.`code` = order_list.order_stateLEFT JOIN product_apperance ON product_apperance.`code` = order_list.apperanceLEFT JOIN product_brand ON product_brand.`code` = product_info.brandorder_num like "%"#{order.orderNum,jdbcType=VARCHAR}"%"and operator like "%"#{order.operator,jdbcType=VARCHAR}"%"and purchase_time = #{order.purchaseTime,jdbcType=DATE}and order_state = #{order.orderState,jdbcType=VARCHAR}and serial_num like "%"#{order.serialNum,jdbcType=VARCHAR}"%"#{state,jdbcType=BIGINT}

这里的重点是:
#{state,jdbcType=BIGINT}

把多个state的or关系转化为 states in (state1,state2,state3...)
in中用foreach循环
mybatis plus and 和or合并写法 记录一下and 和 or 混合使用
sql 语句实现
SELECT* FROM somc_operation_plan WHERE ( title LIKE '%测试%' AND ( charge_user = 'xxx' OR execute_user = 'xxx' ) )

LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.like(StringUtils.isNotEmpty(operationPlan.getTitle()), SomcOperationPlan::getTitle, operationPlan.getTitle()).and(wrapper -> wrapper.eq(StringUtils.isNotEmpty(operationPlan.getChargeUser()), SomcOperationPlan::getChargeUser, operationPlan.getChargeUser()).or().eq(StringUtils.isNotEmpty(operationPlan.getExecuteUser()), SomcOperationPlan::getExecuteUser, operationPlan.getExecuteUser()));

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读