在mybatis中使用mapper進(jìn)行if條件判斷
目的:
在使用mybatis框架中mapper文件有自動(dòng)生成,但有時(shí)需要自己添加sql語(yǔ)句進(jìn)行開(kāi)發(fā),當(dāng)遇到需要使用 if進(jìn)行條件判斷的時(shí)候該怎么寫(xiě)?
查詢sql語(yǔ)句如下:
<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>
語(yǔ)句解析:
1、if語(yǔ)句的格式 ;
2、test中的字段 為parameterType中 com.pojo.QueryDetailReq 的對(duì)象 (入?yún)ⅲ?/p>
3、resultType 為返回查詢數(shù)據(jù)對(duì)象 (結(jié)果集)
補(bǔ)充:mabatis mapper文件中 使用if條件插入字段和數(shù)據(jù)
有時(shí)候我們插入數(shù)據(jù)庫(kù)數(shù)據(jù)的時(shí)候,插入字段都是不確定的,那么我們也可以用if條件來(lái)過(guò)濾一些字段
廢話不多說(shuō),直接上代碼
<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)過(guò)測(cè)試,是可以實(shí)現(xiàn)的。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
微信開(kāi)發(fā)準(zhǔn)備第一步 Maven倉(cāng)庫(kù)管理新建WEB項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了微信開(kāi)發(fā)準(zhǔn)備第一步,Maven倉(cāng)庫(kù)管理新建WEB項(xiàng)目,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04JavaWeb response和request對(duì)象原理及實(shí)例解析
這篇文章主要介紹了JavaWeb response和request對(duì)象原理及實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Mybatis SQL日志如何轉(zhuǎn)換為可執(zhí)行sql
這篇文章主要介紹了Mybatis SQL日志如何轉(zhuǎn)換為可執(zhí)行sql問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09SpringBoot 中大文件(分片上傳)斷點(diǎn)續(xù)傳與極速秒傳功能的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot 中大文件(分片上傳)斷點(diǎn)續(xù)傳與極速秒傳功能的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Python安裝Jupyter Notebook配置使用教程詳解
這篇文章主要介紹了Python安裝Jupyter Notebook配置使用教程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Spring Boot ActiveMQ如何設(shè)置訪問(wèn)密碼
這篇文章主要介紹了Spring Boot ActiveMQ如何設(shè)置訪問(wèn)密碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Java代碼實(shí)現(xiàn)微信頁(yè)面滾動(dòng)防露底(核心代碼)
這篇文章主要介紹了Java代碼實(shí)現(xiàn)微信頁(yè)面滾動(dòng)防露底的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09java多線程中執(zhí)行多個(gè)程序的實(shí)例分析
在本篇文章里小編給大家整理的是一篇關(guān)于java多線程中執(zhí)行多個(gè)程序的實(shí)例分析內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2021-02-02