在mybatis 中使用if else 進(jìn)行判斷的操作
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
<!-- 查詢物品的id -->
<select id="checkItemsId" parameterType="pd" resultType="java.lang.Integer">
SELECT
i.itemsid
FROM pq_goods_items i
<where>
<!--方式一使用choose的方式查詢-->
<!-- <choose>
<when test="parentId !=0 ">parentTypeId=#{parentId}</when>
<when test="parentId==0">parentTypeId is null</when>
</choose> -->
<!--方式二使用if的方式查詢-->
<if test="color!=null">
i.personone=#{personone}
AND i.persontwo=#{persontwo}
AND i.color=#{color}
</if>
<if test="color==null">
i.personone=#{personone}
AND i.persontwo=#{persontwo}
AND i.color is null
</if>
</where>
</select>
需要注意的是 使用了where標(biāo)簽以后,sql中不在使用where字段來(lái)限制條件
如果判斷條件有多個(gè) 中間用 and 表示并列
<if test="color!=null and personone!=null">
補(bǔ)充:mybaits中if 多個(gè)test 和 if else 分支支持
mybaits中if 多個(gè)test
<select id="selectByDynamicallyWithPage" parameterType="map" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from gene_polymorphism
<where>
diag_id = #{conds.diagId,jdbcType=INTEGER}
<if test="conds.chromesome!=null and conds.chromesome!=''">
and chromesome = #{conds.chromesome,jdbcType=VARCHAR}
</if>
<if test="conds.startPos!=null">
and start_pos >= #{conds.startPos,jdbcType=BIGINT}
</if>
</where>
</select>
if else分支:
<select id="selectByDynamicallyWithPage" parameterType="map" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from gene_polymorphism
<where>
diag_id = #{conds.diagId,jdbcType=INTEGER}
<if test="conds.chromesome!=null">
and chromesome = #{conds.chromesome,jdbcType=VARCHAR}
</if>
<if test="conds.startPos!=null">
and start_pos >= #{conds.startPos,jdbcType=BIGINT}
</if>
<if test="conds.endPos!=null">
and end_pos <= #{conds.endPos,jdbcType=BIGINT}
</if>
<if test="conds.geneTypes!=null">
<!--and gene_type in-->
<!--<foreach collection="conds.geneTypes" open="(" close=")" item="item" separator="," >-->
<!--#{item,jdbcType=VARCHAR}-->
<!--</foreach>-->
and (
<foreach collection="conds.geneTypes" item="item" separator="or">
gene_type like CONCAT('%',CONCAT(#{item,jdbcType=VARCHAR}, '%'))
</foreach>
)
</if>
<if test="conds.geneChange!=null">
and gene_change like CONCAT('%',CONCAT(#{conds.geneChange,jdbcType=VARCHAR}, '%'))
</if>
</where>
order by
<trim suffixOverrides=",">
<choose>
<when test="conds.chromesomeSort!=null and conds.chromesomeSort=='asc'">
chromesome asc ,
</when>
<when test="conds.chromesomeSort!=null and conds.chromesomeSort=='desc'">
chromesome desc ,
</when>
</choose>
<choose>
<when test="conds.startPosSort!=null and conds.startPosSort=='asc'">
start_pos asc ,
</when>
<when test="conds.startPosSort!=null and conds.startPosSort=='desc'">
start_pos desc ,
</when>
<otherwise>
id desc
</otherwise>
</choose>
</trim>
limit #{startRow,jdbcType=INTEGER} ,#{pageSize,jdbcType=INTEGER}
<!-- order by id desc limit #{startRow,jdbcType=INTEGER} ,#{pageSize,jdbcType=INTEGER} -->
</select>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Spring Boot2開發(fā)之Spring Boot整合Shiro兩種詳細(xì)方法
這篇文章主要介紹了Spring Boot2開發(fā)之Spring Boot整合Shiro詳細(xì)方法,需要的朋友可以參考下2020-03-03
java括號(hào)匹配算法求解(用棧實(shí)現(xiàn))
這篇文章主要介紹了java括號(hào)匹配算法求解(用棧實(shí)現(xiàn)),需要的朋友可以參考下2020-12-12
Spring MVC 自定義數(shù)據(jù)轉(zhuǎn)換器的思路案例詳解
本文通過(guò)兩個(gè)案例來(lái)介紹下Spring MVC 自定義數(shù)據(jù)轉(zhuǎn)換器的相關(guān)知識(shí),每種方法通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
Java基于中介者模式實(shí)現(xiàn)多人聊天室功能示例
這篇文章主要介紹了Java基于中介者模式實(shí)現(xiàn)多人聊天室功能,詳細(xì)分析了中介者模式的概念、原理以及使用中介模式實(shí)現(xiàn)多人聊天的步驟、操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-05-05
解決MyEclipse中Maven設(shè)置jdk版本jdk1.8報(bào)錯(cuò)問(wèn)題
今天安裝了jdk1.8、tomcat8、和maven3.5.2,弄好后在myeclipse新建了一個(gè)maven項(xiàng)目,項(xiàng)目默認(rèn)是jdk1.5,改成jdk1.8后項(xiàng)目報(bào)錯(cuò)2018-10-10
Java中的static關(guān)鍵字修飾屬性和方法(推薦)
這篇文章主要介紹了Java中的static關(guān)鍵字修飾屬性和方法,包括哪些成員屬性可以被static修飾,靜態(tài)屬性的訪問(wèn)方法示例詳解,需要的朋友可以參考下2022-04-04
Mybatis(ParameterType)傳遞多個(gè)不同類型的參數(shù)方式
這篇文章主要介紹了Mybatis(ParameterType)傳遞多個(gè)不同類型的參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
SpringBoot分布式WebSocket的實(shí)現(xiàn)指南
在現(xiàn)代Web應(yīng)用中,實(shí)時(shí)通信已成為基本需求,而WebSocket是實(shí)現(xiàn)這一功能的核心技術(shù),本文將詳細(xì)介紹如何在Spring Boot項(xiàng)目中實(shí)現(xiàn)分布式WebSocket,包括完整的技術(shù)方案、實(shí)現(xiàn)步驟和核心代碼,需要的朋友可以參考下2025-10-10

