詳解mybatis中的if-else的嵌套使用
案例一:if-else
在mybatis的使用過程中,難免會存在使用if-else的邏輯,但是實際是沒有這種語法的,提供了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嵌套
在實際的編碼過程中會對一些條件進(jìn)行重復(fù)判斷,并對內(nèi)深入if判斷,這時就可以使用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>
功能實現(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)文章
Springboot詳解RocketMQ實現(xiàn)消息發(fā)送與接收流程
這篇文章主要介紹了SpringBoot整合RocketMQ實現(xiàn)消息發(fā)送和接收功能,我們使用主流的SpringBoot框架整合RocketMQ來講解,使用方便快捷,本文分步驟給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
java 遍歷request中的所有表單數(shù)據(jù)的實例代碼
下面小編就為大家?guī)硪黄猨ava 遍歷request中的所有表單數(shù)據(jù)的實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
javaweb中Filter(過濾器)的常見應(yīng)用
這篇文章主要介紹了javaweb中Filter的常見應(yīng)用,過濾器的使用方法,感興趣的小伙伴們可以參考一下2015-12-12

