詳解mybatis中的if-else的嵌套使用
案例一:if-else
在mybatis的使用過(guò)程中,難免會(huì)存在使用if-else的邏輯,但是實(shí)際是沒(méi)有這種語(yǔ)法的,提供了choose標(biāo)簽來(lái)替代這種語(yǔ)法
<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í)際的編碼過(guò)程中會(huì)對(duì)一些條件進(jìn)行重復(fù)判斷,并對(duì)內(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)之后反思:要不我不對(duì)“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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA配置文件application.properties亂碼問(wèn)題及解決過(guò)程
在IntelliJ IDEA中打開(kāi)application.properties時(shí),中文注釋出現(xiàn)亂碼,原因多為文件編碼設(shè)置不正確,即使顯示UTF-8仍可能亂碼,解決方法是設(shè)置屬性文件默認(rèn)編碼為UTF-8,保存后注釋可正常顯示2025-09-09
java評(píng)論、回復(fù)功能設(shè)計(jì)與實(shí)現(xiàn)方法
很多項(xiàng)目或者系統(tǒng)都有評(píng)論或者回復(fù)的需求,但評(píng)論回復(fù)的實(shí)現(xiàn)往往都比較復(fù)雜,也不好實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于java評(píng)論、回復(fù)功能設(shè)計(jì)與實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-06-06
Java線程本地變量導(dǎo)致的緩存問(wèn)題解決方法
使用緩存可以緩解大流量壓力,顯著提高程序的性能,我們?cè)谑褂镁彺嫦到y(tǒng)時(shí),尤其是大并發(fā)情況下,經(jīng)常會(huì)遇到一些疑難雜癥,這篇文章主要給大家介紹了關(guān)于Java線程本地變量導(dǎo)致的緩存問(wèn)題的解決方法,需要的朋友可以參考下,2024-08-08
動(dòng)態(tài)配置Spring Boot日志級(jí)別的全步驟
這篇文章主要給大家介紹了關(guān)于動(dòng)態(tài)配置Spring Boot日志級(jí)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Springboot集成minio實(shí)現(xiàn)文件存儲(chǔ)的實(shí)現(xiàn)代碼
MinIO?是一款基于Go語(yǔ)言的高性能對(duì)象存儲(chǔ)服務(wù),本文主要介紹了Springboot集成minio實(shí)現(xiàn)文件存儲(chǔ)的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
SpringBoot開(kāi)發(fā)案例之配置Druid數(shù)據(jù)庫(kù)連接池的示例
本篇文章主要介紹了SpringBoot開(kāi)發(fā)案例之配置Druid數(shù)據(jù)庫(kù)連接池的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03

