在mybatis 中使用if else 进行判断的操作
作者:阿皓1024 发布时间:2021-11-10 23:17:11
标签:mybatis,if,else,判断
我就废话不多说了,大家还是直接看代码吧~
<!-- 查询物品的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标签以后,sql中不在使用where字段来限制条件
如果判断条件有多个 中间用 and 表示并列
<if test="color!=null and personone!=null">
补充:mybaits中if 多个test 和 if else 分支支持
mybaits中if 多个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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/pqsas_com/article/details/75337608


猜你喜欢
- 在后端数据接口项目开发中,经常遇到返回的数据中有null值,导致前端需要进行判断处理,否则容易出现undefined的情况,如何便捷的将nu
- 利用Java,在控制台操作下,编写的五子棋,作为复习二维数组,面向对象等基础知识。w表示白棋,b表示黑棋import java.util.S
- 结论先行Kotlin协程中的Channel用于处理多个数据组合的流,随用随取,时刻准备着,就像自来水一样,打开开关就有水了。Channel使
- 工具类-java精确到小数点后6位验证要求,必须精确到小数点后6位,但是后面都是0的话,double会省略0,正则验证不通过,所以有了下面解
- 上一篇文章里把SwipeRefreshLayout的原理简单过了一下,大致了解了其工作原理,不熟悉的可以去看一下:https://www.j
- 本文为大家分享了Android实现带动画效果的可点击展开TextView 制作代码,效果图: 收起(默认)效果:点击展开后的效果:源码: 布
- 画廊视图(Gallery)表示,能够按水平方向显示内容,并且可用手指直接拖动图片移动,一般用来浏览图片,被选中的选项位于中间,并且可以响应事
- 介绍Java中介者模式(Mediator Pattern)是一种行为设计模式,它可以降低多个对象之间的耦合性,通过一个中介者对象来协调这些对
- 现在Android智能手机的像素都会提供照相的功能,大部分的手机的摄像头的像素都在1000万以上的像素,有的甚至会更高。它们大多都会支持光学
- 引言:SpringBoot web项目开发中往往会涉及到一些静态资源的使用,比如说图片,css样式,js等等,今天我们来讲讲这些常见的静态资
- 本文实例为大家分享了Android画画板展示的具体代码,供大家参考,具体内容如下main.xml布局<RelativeLayout x
- springboot 启动排除某些bean的注入问题:最近做项目的时候,需要引入其他的jar。然后还需要扫描这些jar里的某些bean。于是
- 一、实体类转换成XML将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化public stat
- JSONArray删除元素的两种方式我自个磨出来的,难受JSONArray jsonarray = new JSONArray();Set&
- 新添加个发文类型insert into mis.zyb_sf_type values('121','榆财法字'
- 简介网上对于 Camera2 的介绍有很多,在 Github 上也有很多关于 Camera2 的封装库,但是对于那些库,封装性太强,有时候我
- 在使用C#进行相关编程的时候,有时候我们需要获取系统相关的进程信息。那么在C#中如何获取系统的所有进程那?下面请跟小编一起来操作。1、首先新
- 一.异步冷数据流在Kotlin协程:协程的基础与使用中,通过使用协程中提供的flow方法可以创建一个Flow对象。这种方法得到的Flow对象
- package org.load.u;import java.io.File;import java.util.LinkedHashMap;
- 本文实例讲述了C#控制台下多线程实现方法。分享给大家供大家参考。具体如下:class Program{ static void