在mybatis 中使用if else 進(jìn)行判斷的操作
我就廢話不多說了,大家還是直接看代碼吧~
<!-- 查詢物品的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字段來限制條件
如果判斷條件有多個(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)文章
SpringBoot實(shí)現(xiàn)優(yōu)雅停機(jī)的正確方法
什么叫優(yōu)雅停機(jī)?就是向應(yīng)用進(jìn)程發(fā)出停止指令之后,能保證正在執(zhí)行的業(yè)務(wù)操作不受影響,直到操作運(yùn)行完畢之后再停止服務(wù)。本文就來和大家聊聊SpringBoot實(shí)現(xiàn)優(yōu)雅停機(jī)的正確姿勢,希望對大家有所幫助2023-01-01mybatis group by substr函數(shù)傳參報(bào)錯(cuò)的解決
這篇文章主要介紹了mybatis group by substr函數(shù)傳參報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java實(shí)現(xiàn)雪花算法的原理和實(shí)戰(zhàn)教程
這篇文章主要介紹了Java實(shí)現(xiàn)雪花算法的原理和實(shí)戰(zhàn)教程,本文通過語言表述和代碼的實(shí)現(xiàn)講解了該項(xiàng)算法,,需要的朋友可以參考下2021-06-06idea2020.1.3 手把手教你創(chuàng)建web項(xiàng)目的方法步驟
這篇文章主要介紹了idea 2020.1.3 手把手教你創(chuàng)建web項(xiàng)目的方法步驟,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08SpringMVC中RequestContextHolder獲取請求信息的方法
這篇文章主要介紹了SpringMVC中RequestContextHolder獲取請求信息的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04