MyBatis中执行相关SQL语句的方法
作者:weixin_46949892
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语句内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!