MyBatis中執(zhí)行相關(guān)SQL語(yǔ)句的方法
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 ---兩種寫(xiě)法
<!--第一種寫(xiě)法--> <where> <if test="reportRule != null and reportRule != ''"> and report_rule like CONCAT('%',#{reportRule},'%') </if> </where> <!--第二種寫(xiě)法--> <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,后端實(shí)際查詢(xún)語(yǔ)句
[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
注意:由于一開(kāi)始where語(yǔ)句只寫(xiě)了 <if test="reportRule != null>沒(méi)有寫(xiě)reportRule != ''" ,導(dǎo)致查詢(xún)結(jié)果出錯(cuò),所以我們?nèi)绻玫絼?dòng)態(tài)查詢(xún)的話,一定要搞清楚前端傳的是空值還是null值,如果不確定的話,就都判斷一下<if test="reportRule != null and reportRule != ''">
到此這篇關(guān)于MyBatis中執(zhí)行相關(guān)SQL語(yǔ)句的方法的文章就介紹到這了,更多相關(guān)MyBatis 執(zhí)行SQL語(yǔ)句內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java內(nèi)存異常使用導(dǎo)致full?gc頻繁
Full?GC是Java虛擬機(jī)中垃圾回收的一種方式,它會(huì)暫停應(yīng)用程序所有的線程并清理整個(gè)堆內(nèi)存。頻繁的Full?GC會(huì)導(dǎo)致應(yīng)用程序的性能下降,甚至出現(xiàn)長(zhǎng)時(shí)間的停頓。Java內(nèi)存異常使用常常是Full?GC頻繁出現(xiàn)的原因之一,如使用大量的靜態(tài)變量、內(nèi)存泄漏等。2023-04-04SpringCloud集成和使用OpenFeign的教程指南
在微服務(wù)架構(gòu)中,服務(wù)間的通信是至關(guān)重要的,SpringCloud作為一個(gè)功能強(qiáng)大的微服務(wù)框架,為我們提供了多種服務(wù)間通信的方式,其中,OpenFeign是一個(gè)聲明式的Web服務(wù)客戶(hù)端,它使得編寫(xiě)Web服務(wù)客戶(hù)端變得更加簡(jiǎn)單,本文將詳細(xì)介紹如何在SpringCloud項(xiàng)目中集成和使用OpenFeign2024-10-10用SpringMVC編寫(xiě)一個(gè)HelloWorld的詳細(xì)過(guò)程
SpringMVC是Spring的一個(gè)后續(xù)產(chǎn)品,是Spring的一個(gè)子項(xiàng)目<BR>SpringMVC?是?Spring?為表述層開(kāi)發(fā)提供的一整套完備的解決方案,本文我們將用SpringMVC編寫(xiě)一個(gè)HelloWorld,文中有詳細(xì)的編寫(xiě)過(guò)程,需要的朋友可以參考下2023-08-08IDEA在一個(gè)項(xiàng)目空間下管理多個(gè)項(xiàng)目的操作方法
這篇文章主要介紹了IDEA如何在一個(gè)項(xiàng)目空間下管理多個(gè)項(xiàng)目,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Java數(shù)組操作經(jīng)典例題大總結(jié)
數(shù)組是在內(nèi)存中存儲(chǔ)相同數(shù)據(jù)類(lèi)型的連續(xù)的空間,聲明一個(gè)數(shù)組就是在內(nèi)存空間中劃出一串連續(xù)的空間,下面這篇文章主要給大家介紹了關(guān)于Java數(shù)組操作經(jīng)典例題的相關(guān)資料,需要的朋友可以參考下2022-03-03