詳解mybatis中的if-else的嵌套使用
案例一:if-else
在mybatis的使用過程中,難免會存在使用if-else的邏輯,但是實(shí)際是沒有這種語法的,提供了choose標(biāo)簽來替代這種語法
<select id="selectUserByState" resultType="com.bz.model.entity.User"> SELECT * FROM user WHERE 1=1 <choose> <when test="state == 1"> AND name = #{name1} </when> <when test="state == 2"> AND name = #{name2} </when> <otherwise> AND name = #{name3} </otherwise> </choose> </select>
案例二:if嵌套
在實(shí)際的編碼過程中會對一些條件進(jìn)行重復(fù)判斷,并對內(nèi)深入if判斷,這時(shí)就可以使用if嵌套
<select id="selectUserByState" resultType="com.bz.model.entity.User"> SELECT * FROM user WHERE <if test=" gender!=null and gender!='' "> <if test=" gender==male "> and name=#{name} </if> </if> </select>
MyBatis中if和choose的嵌套
<!-- public List<VadtaxShow> findList(VadtaxShow vadtaxShow); --> <select id="findList" parameterType="com.cdqyzj_WC.Backstage.vaddedtax.domain.VaddeTax" resultType="com.cdqyzj_WC.Backstage.vaddedtax.domain.VaddeTax"> SELECT t.addedId,t.taxType,t.totalSales,t.outputTax,t.inputTax,t.entryAmount, t.amountTax,t.retentionTax, t.createTime, t.taxTime,t.comId,c.comName,c.comType FROM t_g_vaddedtax AS t JOIN t_ucompany AS c ON c.comId = t.comId <where> 1=1 <if test="comType != '' and comType != null"> and c.comType = #{comType}</if> <if test="taxTime != null and taxTime != ''"> and t.taxTime =#{taxTime} </if> <if test="taxType != null and taxType != '' "> and t.taxType =#{taxType} </if> <if test="comId != null and comId != '' and comId != 0 "> and t.comId =#{comId} </if> <if test="start_times != null and end_times != null"> <choose> <when test="middle_times != null"> and t.createTime in ('${start_times}','${middle_times}', '${end_times}' ) </when> <otherwise> and t.createTime in ('${start_times}','${end_times}' ) </otherwise> </choose> </if> <if test="orderBy != null and orderType != '' "> order by ${orderBy} ${orderType} </if> <if test="pageSize != 0 "> limit ${startRows},${pageSize} </if> </where> </select>
功能實(shí)現(xiàn)之后反思:要不我不對“middle_times”判空?這樣不就不用嵌套了嗎?
<!-- public List<VadtaxShow> findList(VadtaxShow vadtaxShow); --> <select id="findList" parameterType="com.cdqyzj_WC.Backstage.vaddedtax.domain.VaddeTax" resultType="com.cdqyzj_WC.Backstage.vaddedtax.domain.VaddeTax"> SELECT t.addedId,t.taxType,t.totalSales,t.outputTax,t.inputTax,t.entryAmount, t.amountTax,t.retentionTax, t.createTime, t.taxTime,t.comId,c.comName,c.comType FROM t_g_vaddedtax AS t JOIN t_ucompany AS c ON c.comId = t.comId <where> 1=1 <if test="comType != '' and comType != null"> and c.comType = #{comType}</if> <if test="taxTime != null and taxTime != ''"> and t.taxTime =#{taxTime} </if> <if test="taxType != null and taxType != '' "> and t.taxType =#{taxType} </if> <if test="comId != null and comId != '' and comId != 0 "> and t.comId =#{comId} </if> <if test="start_times != null and end_times != null"> and t.createTime in ('${start_times}','${middle_times}', '${end_times}' ) </if> <if test="orderBy != null and orderType != '' "> order by ${orderBy} ${orderType} </if> <if test="pageSize != 0 "> limit ${startRows},${pageSize} </if> </where> </select>
到此這篇關(guān)于詳解mybatis中的if-else的嵌套使用的文章就介紹到這了,更多相關(guān)mybatis if-else嵌套內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)手寫線程池實(shí)例并測試詳解
這篇文章主要來模擬一下線程池和工作隊(duì)列的流程,以及編寫代碼和測試類進(jìn)行測試。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02java實(shí)現(xiàn)圖片滑動驗(yàn)證(包含前端代碼)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖片滑動驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12java 設(shè)計(jì)模式之依賴倒置實(shí)例詳解
這篇文章主要介紹了java 設(shè)計(jì)模式之依賴倒置,結(jié)合實(shí)例形式詳細(xì)分析了依賴倒置的相關(guān)概念、原理、使用技巧及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-11-11Springboot詳解RocketMQ實(shí)現(xiàn)消息發(fā)送與接收流程
這篇文章主要介紹了SpringBoot整合RocketMQ實(shí)現(xiàn)消息發(fā)送和接收功能,我們使用主流的SpringBoot框架整合RocketMQ來講解,使用方便快捷,本文分步驟給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06Java程序員必須知道的5個(gè)JVM命令行標(biāo)志
這篇文章主要介紹了每個(gè)Java程序員必須知道的5個(gè)JVM命令行標(biāo)志,需要的朋友可以參考下2015-03-03java 遍歷request中的所有表單數(shù)據(jù)的實(shí)例代碼
下面小編就為大家?guī)硪黄猨ava 遍歷request中的所有表單數(shù)據(jù)的實(shí)例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09javaweb中Filter(過濾器)的常見應(yīng)用
這篇文章主要介紹了javaweb中Filter的常見應(yīng)用,過濾器的使用方法,感興趣的小伙伴們可以參考一下2015-12-12