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

 更新时间:2021年07月13日 09:15:30   作者:NO0b  
这篇文章主要介绍了Mybatis中and和循环or混用操作(or转换成in),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Mybatis and和循环or混用

这次项目用到一个and和or混用的场景 , 因为用到多个or(循环), 没想到好的办法

最终转换成用 IN实现:

场景

用left join链接多个表, 多个条件and筛选, 其中状态(state)条件筛选出多个可选状态的条目,

本来想用and 和 or 但是 or的条件是个数组参数, 需要遍历states , 可能0个可能多个, 拼了半天没有成功 , 最后发现用 IN 和FOREACH就可以了

DAO层接口

List<OrderInfoForm> selectOrdersByStatesSelective(
            @Param(value="order")Order order,
            @Param(value="states")Integer[] states);

Mybatis实现

<select id="selectOrdersByStatesSelective" resultMap="AllResultMap" >
    select 
    <include refid="All_Column_List" />
    from order_list
    LEFT JOIN product_method ON product_method.`code` = order_list.purchase_method
    LEFT JOIN product_color ON product_color.`code` = order_list.color
    LEFT JOIN product_guarantee ON product_guarantee.`code` = order_list.guarantee
    LEFT JOIN product_info ON order_list.product_id = product_info.id
    LEFT 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_state
    LEFT JOIN product_apperance ON product_apperance.`code` = order_list.apperance
    LEFT JOIN product_brand ON product_brand.`code` = product_info.brand
    <where>
        <if test="order.orderNum != null " >
            order_num like "%"#{order.orderNum,jdbcType=VARCHAR}"%"
        </if>
        <if test="order.operator != null " >
            and operator like "%"#{order.operator,jdbcType=VARCHAR}"%"
        </if>
        <if test="order.purchaseTime != null" >
            and purchase_time = #{order.purchaseTime,jdbcType=DATE}
        </if>
        <if test="order.orderState != null" >
            and order_state = #{order.orderState,jdbcType=VARCHAR}
        </if>
        <if test="order.serialNum != null" >
            and serial_num like "%"#{order.serialNum,jdbcType=VARCHAR}"%"
        </if>
        
        <if test="states != null and states.length >0">
            <foreach collection="states" item="state" separator="," open=" and order_state in (" close=")">
                #{state,jdbcType=BIGINT}
            </foreach>
        </if>
    </where>
  </select>

这里的重点是:

 <if test="states != null and states.length >0">
            <foreach collection="states" item="state" separator="," open=" and order_state in (" close=")">
                #{state,jdbcType=BIGINT}
            </foreach>
</if>

把多个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<SomcOperationPlan> 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()));

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

相关文章

  • Spring实现Aware接口自定义获取bean的两种方式

    Spring实现Aware接口自定义获取bean的两种方式

    这篇文章主要介绍了Java编程实现Aware接口自定义获取bean的两种方式,通过BeanFactoryAware和ApplicationContextAware,具有一定参考价值,需要的朋友可以了解下。
    2017-09-09
  • 使用Java实现将ppt转换为文本

    使用Java实现将ppt转换为文本

    这篇文章主要为大家详细介绍了如何使用Java实现将ppt转换为文本,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以参考下
    2024-01-01
  • Java使用Preference类保存上一次记录的方法

    Java使用Preference类保存上一次记录的方法

    这篇文章主要介绍了Java使用Preference类保存上一次记录的方法,较为详细的分析了Preference类的使用技巧,需要的朋友可以参考下
    2015-05-05
  • JAVA验证码工具实例代码

    JAVA验证码工具实例代码

    这篇文章主要介绍了JAVA验证码工具实例代码,需要的朋友可以参考下
    2018-03-03
  • java按指定编码写入和读取文件内容的类分享

    java按指定编码写入和读取文件内容的类分享

    这篇文章主要介绍了java按指定编码写入和读取文件内容的类,需要的朋友可以参考下
    2014-02-02
  • Java NIO实战之聊天室功能详解

    Java NIO实战之聊天室功能详解

    这篇文章主要介绍了Java NIO实战之聊天室功能,结合实例形式详细分析了java NIO聊天室具体的服务端、客户端相关实现方法与操作注意事项,需要的朋友可以参考下
    2019-11-11
  • Java中AIO、BIO、NIO应用场景及区别

    Java中AIO、BIO、NIO应用场景及区别

    本文主要介绍了Java中AIO、BIO、NIO应用场景及区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • MyBatis执行动态SQL的方法

    MyBatis执行动态SQL的方法

    今天小编就为大家分享一篇关于MyBatis执行动态SQL的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Java接收text/event-stream格式数据的详细代码

    Java接收text/event-stream格式数据的详细代码

    这篇文章主要介绍了java接收text/event-stream格式数据,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • Spring实现动态修改时间参数并手动开启关停操作

    Spring实现动态修改时间参数并手动开启关停操作

    spring实现定时任务的方式有三种,分别是java自带的timer类、spring task和quartz三种。本文只介绍spring自带的task和第三方quartz,感兴趣的朋友参考下吧
    2017-09-09

最新评论