MyBatis中執(zhí)行相關(guān)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 != ''" ,導致查詢結(jié)果出錯,所以我們?nèi)绻玫絼討B(tài)查詢的話,一定要搞清楚前端傳的是空值還是null值,如果不確定的話,就都判斷一下<if test="reportRule != null and reportRule != ''">
到此這篇關(guān)于MyBatis中執(zhí)行相關(guān)SQL語句的方法的文章就介紹到這了,更多相關(guān)MyBatis 執(zhí)行SQL語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud集成和使用OpenFeign的教程指南
在微服務(wù)架構(gòu)中,服務(wù)間的通信是至關(guān)重要的,SpringCloud作為一個功能強大的微服務(wù)框架,為我們提供了多種服務(wù)間通信的方式,其中,OpenFeign是一個聲明式的Web服務(wù)客戶端,它使得編寫Web服務(wù)客戶端變得更加簡單,本文將詳細介紹如何在SpringCloud項目中集成和使用OpenFeign2024-10-10Java數(shù)組操作經(jīng)典例題大總結(jié)
數(shù)組是在內(nèi)存中存儲相同數(shù)據(jù)類型的連續(xù)的空間,聲明一個數(shù)組就是在內(nèi)存空間中劃出一串連續(xù)的空間,下面這篇文章主要給大家介紹了關(guān)于Java數(shù)組操作經(jīng)典例題的相關(guān)資料,需要的朋友可以參考下2022-03-03