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日常练习题,每天进步一点点(57)

    Java日常练习题,每天进步一点点(57)

    下面小编就为大家带来一篇Java基础的几道练习题(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望可以帮到你
    2021-08-08
  • java实现最短路径算法之Dijkstra算法

    java实现最短路径算法之Dijkstra算法

    这篇文章主要介绍了java实现最短路径算法之Dijkstra算法, Dijkstra算法是最短路径算法中为人熟知的一种,是单起点全路径算法,有兴趣的可以了解一下
    2017-10-10
  • Spring Boot 快速集成 Redis的方法

    Spring Boot 快速集成 Redis的方法

    这篇文章主要介绍了Spring Boot 如何快速集成 Redis,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • java多线程编程实现下雪效果

    java多线程编程实现下雪效果

    这篇文章主要介绍了java多线程编程实现下雪效果的相关资料,需要的朋友可以参考下
    2015-11-11
  • SpringBoot环境属性占位符解析和类型转换方式

    SpringBoot环境属性占位符解析和类型转换方式

    这篇文章主要介绍了SpringBoot环境属性占位符解析和类型转换方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • java中static关键字用法详解

    java中static关键字用法详解

    这篇文章主要为大家详细介绍了java中static关键字的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • java链表的常见简单面试算法题详解

    java链表的常见简单面试算法题详解

    文章总结:本文主要介绍了单链表的基本操作,包括头插法、尾插法、链表翻转、链表成环判断、成环位置判断、成环长度判断,以及有序链表的合并,通过实例和代码示例,详细讲解了每种操作的原理和实现方法
    2025-01-01
  • Java中@DateTimeFormat @JsonFormat失效原因及测试填坑

    Java中@DateTimeFormat @JsonFormat失效原因及测试填坑

    本文主要介绍了Java中@DateTimeFormat @JsonFormat失效原因及测试填坑,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • springboot @RequiredArgsConstructor的概念与使用方式

    springboot @RequiredArgsConstructor的概念与使用方式

    这篇文章主要介绍了springboot @RequiredArgsConstructor的概念与使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-09-09
  • Java9新特性对HTTP2协议支持与非阻塞HTTP API

    Java9新特性对HTTP2协议支持与非阻塞HTTP API

    这篇文章主要为大家介绍了Java9新特性对HTTP2协议的支持与非阻塞HTTP API,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-03-03

最新评论