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语句内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java内存异常使用导致full gc频繁

    java内存异常使用导致full gc频繁

    Full GC是Java虚拟机中垃圾回收的一种方式,它会暂停应用程序所有的线程并清理整个堆内存。频繁的Full GC会导致应用程序的性能下降,甚至出现长时间的停顿。Java内存异常使用常常是Full GC频繁出现的原因之一,如使用大量的静态变量、内存泄漏等。
    2023-04-04
  • 基于@GetMapping注解携带参数的方式

    基于@GetMapping注解携带参数的方式

    这篇文章主要介绍了基于@GetMapping注解携带参数的方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • SpringCloud集成和使用OpenFeign的教程指南

    SpringCloud集成和使用OpenFeign的教程指南

    在微服务架构中,服务间的通信是至关重要的,SpringCloud作为一个功能强大的微服务框架,为我们提供了多种服务间通信的方式,其中,OpenFeign是一个声明式的Web服务客户端,它使得编写Web服务客户端变得更加简单,本文将详细介绍如何在SpringCloud项目中集成和使用OpenFeign
    2024-10-10
  • spring 自动装配和aop的使用

    spring 自动装配和aop的使用

    这篇文章主要介绍了spring 自动装配和aop的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Java带default方法接口的应用示例

    Java带default方法接口的应用示例

    这篇文章主要介绍了Java带default方法接口的应用,结合实例形式分析了java带default方法接口定义、用法及相关操作注意事项,需要的朋友可以参考下
    2019-08-08
  • Windows系统安装JDK小结

    Windows系统安装JDK小结

    这篇文章主要给大家详细介绍了Windows系统安装JDK的方法和步奏,十分的细致,有需要的小伙伴可以参考下
    2016-03-03
  • Java超详细整理讲解各种排序

    Java超详细整理讲解各种排序

    这篇文章主要介绍了Java常用的排序算法及代码实现,在Java开发中,对排序的应用需要熟练的掌握,这样才能够确保Java学习时候能够有扎实的基础能力。那Java有哪些排序算法呢?本文小编就来详细说说Java常见的排序算法,需要的朋友可以参考一下
    2022-07-07
  • 用SpringMVC编写一个HelloWorld的详细过程

    用SpringMVC编写一个HelloWorld的详细过程

    SpringMVC是Spring的一个后续产品,是Spring的一个子项目<BR>SpringMVC 是 Spring 为表述层开发提供的一整套完备的解决方案,本文我们将用SpringMVC编写一个HelloWorld,文中有详细的编写过程,需要的朋友可以参考下
    2023-08-08
  • IDEA在一个项目空间下管理多个项目的操作方法

    IDEA在一个项目空间下管理多个项目的操作方法

    这篇文章主要介绍了IDEA如何在一个项目空间下管理多个项目,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • Java数组操作经典例题大总结

    Java数组操作经典例题大总结

    数组是在内存中存储相同数据类型的连续的空间,声明一个数组就是在内存空间中划出一串连续的空间,下面这篇文章主要给大家介绍了关于Java数组操作经典例题的相关资料,需要的朋友可以参考下
    2022-03-03

最新评论