在mybatis中使用mapper進行if條件判斷
目的:
在使用mybatis框架中mapper文件有自動生成,但有時需要自己添加sql語句進行開發(fā),當遇到需要使用 if進行條件判斷的時候該怎么寫?
查詢sql語句如下:
<select id="queryData" parameterType="com.pojo.QueryDetailReq" resultType="com.pojo.MxDataInfo">
select * from db_trd.tb_trd_secu_order where order_status=6
<if test="channelNo!= null" >
and channel_no = #{channelNo,jdbcType=INTEGER}
</if>
<if test="reportNo!=null" >
and report_no = #{reportNo,jdbcType=INTEGER}
</if>
<if test="companyNo!= null" >
and company_no = #{companyNo,jdbcType=VARCHAR}
</if>
<if test="orderNo!=null" >
and order_no = #{orderNo,jdbcType=INTEGER}
</if>
<if test="stockCode!=null" >
and stock_code = #{stockCode,jdbcType=VARCHAR}
</if>
</select>
語句解析:
1、if語句的格式 ;
2、test中的字段 為parameterType中 com.pojo.QueryDetailReq 的對象 (入?yún)ⅲ?/p>
3、resultType 為返回查詢數(shù)據(jù)對象 (結(jié)果集)
補充:mabatis mapper文件中 使用if條件插入字段和數(shù)據(jù)
有時候我們插入數(shù)據(jù)庫數(shù)據(jù)的時候,插入字段都是不確定的,那么我們也可以用if條件來過濾一些字段
廢話不多說,直接上代碼
<insert id="ORDER_I" parameterType="hashmap">
insert into t_order
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="orderNo != null">
orderNo,
</if>
<if test="serviceName != null">
serviceName,
</if>
<if test="idcard != null">
idcard,
</if>
<if test="name != null">
name,
</if>
<if test="requestData != null">
requestData,
</if>
<if test="responseData != null">
responseData,
</if>
<if test="status != null">
status,
</if>
<if test="updatedTime != null">
updatedTime,
</if>
<if test="completionTime != null">
completionTime,
</if>
<if test="bae007 != null">
bae007,
</if>
<if test="operId != null">
operId,
</if>
<if test="operName != null">
operName,
</if>
<if test="remark != null">
remark,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="orderNo != null">
#{orderNo},
</if>
<if test="serviceName != null">
#{serviceName},
</if>
<if test="idcard != null">
#{idcard},
</if>
<if test="name != null">
#{name},
</if>
<if test="requestData != null">
#{requestData},
</if>
<if test="responseData != null">
#{responseData},
</if>
<if test="status != null">
#{status},
</if>
<if test="updatedTime != null">
#{updatedTime},
</if>
<if test="completionTime != null">
#{completionTime},
</if>
<if test="bae007 != null">
#{bae007},
</if>
<if test="operId != null">
#{operId},
</if>
<if test="operName != null">
#{operName},
</if>
<if test="remark != null">
#{remark},
</if>
</trim>
</insert>
經(jīng)過測試,是可以實現(xiàn)的。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
feign 調(diào)用第三方服務中部分特殊符號未轉(zhuǎn)義問題
這篇文章主要介紹了feign 調(diào)用第三方服務中部分特殊符號未轉(zhuǎn)義問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
springboot Controller直接返回String類型帶來的亂碼問題及解決
文章介紹了在Spring Boot中,當Controller直接返回String類型時可能出現(xiàn)的亂碼問題,并提供了解決辦法,通過在`application.yaml`中設置請求和響應的編碼格式,并在自定義配置類中進行配置,可以有效解決這一問題2024-11-11

