MyBatis中执行相关SQL语句的方法

 更新时间:2023年08月16日 15:53:42   作者:weixin_46949892  
本文主要介绍了MyBatis中执行相关SQL语句的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.between... and...

<if test="(reportStartDate != null and reportStartDate != '') || (reportEndDate != null and reportEndDate != '')">
    and report_date between #{reportStartDate} AND #{reportEndDate}
</if>

2.and   or

<if test="method != null">
    and ( Method like CONCAT('%', #{key ,jdbcType=VARCHAR}, '%')
    or EventCode like CONCAT('%', #{key ,jdbcType=VARCHAR}, '%')
    or EventName like CONCAT('%', #{key ,jdbcType=VARCHAR}, '%')
    )
</if>

3.like ---两种写法

<!--第一种写法-->
<where>          
  <if test="reportRule != null and reportRule != ''">
      and report_rule like CONCAT('%',#{reportRule},'%')
  </if>           
</where>
<!--第二种写法-->         
<where>
  <if test="custName != null and custName != ''">
      and cust_name like '%' #{custName} '%'
  </if>
  <if test="creater != null and creater != ''">
       and creater like '%' #{creater} '%'
  </if>
</where>

完整示例: 

<select id="findByQueryIds"  parameterType="string" resultType="java.lang.Integer">
    select id from t_monitor_suspicion_custom
    <where>
      <if test="(reportStartDate != null and reportStartDate != '') || (reportEndDate != null and reportEndDate != '')">
         and report_date between #{reportStartDate} AND #{reportEndDate}
      </if>
      <if test="reportRule != null and reportRule != ''">
         and report_rule like CONCAT('%',#{reportRule},'%')
      </if>
      <if test="custNo != null and custNo != ''">
         and cust_no like CONCAT('%',#{custNo},'%')
      </if>
      <if test="custName != null and custName != ''">
          and cust_name like CONCAT('%',#{custName},'%')
      </if>
      <if test="customType != null and customType != ''">
          and custom_type = #{customType}
      </if>
   </where>
   and (suspicion_status = #{value} or suspicion_status = #{value1})
</select>

 前端传入cust_no为1019,后端实际查询语句

[zl-aml-admin] DEBUG 2023-08-15 10:44:14.514 [http-nio-8081-exec-20] com.zlpay.modules.monitor.dao.SuspicionCustomDao.findByQueryIds [BaseJdbcLogger.java:137] - ==>  Preparing: select id from t_monitor_suspicion_custom WHERE cust_no like CONCAT('%',?,'%') and (suspicion_status = ? or suspicion_status = ?)
[zl-aml-admin] DEBUG 2023-08-15 10:44:14.516 [http-nio-8081-exec-20] com.zlpay.modules.monitor.dao.SuspicionCustomDao.findByQueryIds [BaseJdbcLogger.java:137] - ==> Parameters: 1019(String), 0(String), 3(String)
[zl-aml-admin] DEBUG 2023-08-15 10:44:14.519 [http-nio-8081-exec-20] com.zlpay.modules.monitor.dao.SuspicionCustomDao.findByQueryIds [BaseJdbcLogger.java:137] - <==      Total: 1

注意:由于一开始where语句只写了 <if test="reportRule != null>没有写reportRule != ''" ,导致查询结果出错,所以我们如果用到动态查询的话,一定要搞清楚前端传的是空值还是null值,如果不确定的话,就都判断一下<if test="reportRule != null and reportRule != ''">

到此这篇关于MyBatis中执行相关SQL语句的方法的文章就介绍到这了,更多相关MyBatis 执行SQL语句内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 批量将现有Jar包上传到Maven私服

    批量将现有Jar包上传到Maven私服

    今天小编就为大家分享一篇关于批量将现有Jar包上传到Maven私服,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • SpringBoot中获取profile的方法详解

    SpringBoot中获取profile的方法详解

    这篇文章主要介绍了springboot获取profile的操作,文中的示例代码讲解详细,具有很好的参考价值,希望对大家有所帮助
    2022-04-04
  • java控制台版实现五子棋游戏

    java控制台版实现五子棋游戏

    这篇文章主要为大家详细介绍了java控制台版实现五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • SpringBoot中 Jackson 日期的时区和日期格式问题解决

    SpringBoot中 Jackson 日期的时区和日期格式问题解决

    因为最近项目需要国际化,需要能够支持多种国际化语言,目前需要支持三种(法语、英语、简体中文),这篇文章主要介绍了SpringBoot中 Jackson 日期的时区和日期格式问题,需要的朋友可以参考下
    2022-12-12
  • Java Socket聊天室编程(二)之利用socket实现单聊聊天室

    Java Socket聊天室编程(二)之利用socket实现单聊聊天室

    这篇文章主要介绍了Java Socket聊天室编程(二)之利用socket实现单聊聊天室的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • SpringBoot+SpringCache实现两级缓存(Redis+Caffeine)

    SpringBoot+SpringCache实现两级缓存(Redis+Caffeine)

    这篇文章主要介绍了SpringBoot+SpringCache实现两级缓存(Redis+Caffeine),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 一篇文章教你如何在SpringCloud项目中使用OpenFeign

    一篇文章教你如何在SpringCloud项目中使用OpenFeign

    这篇文章主要介绍了SpringCloud 使用Open feign 优化详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-08-08
  • Java线程安全之volatile详解

    Java线程安全之volatile详解

    这篇文章主要介绍了Java线程安全之volatile详解,volatile 的存在,解决了不同内存间拷贝的同步问题,在每一次使用或者修改时候,都去原持有内存中去拿最新的状态,需要的朋友可以参考下
    2023-08-08
  • 把Java程序转换成exe,可直接运行的实现

    把Java程序转换成exe,可直接运行的实现

    这篇文章主要介绍了把Java程序转换成exe,可直接运行的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • SpringBoot 如何从容器中获取对象

    SpringBoot 如何从容器中获取对象

    这篇文章主要介绍了SpringBoot 如何从容器中获取对象,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08

最新评论