欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

在mybatis 中使用if else 进行判断的操作

时间:2021-03-10 09:24:48|栏目:JAVA代码|点击:

我就废话不多说了,大家还是直接看代码吧~

<!-- 查询物品的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 &gt;= #{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 &gt;= #{conds.startPos,jdbcType=BIGINT}
  </if>
  <if test="conds.endPos!=null">
  and end_pos &lt;= #{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>

上一篇:SSM框架前后端信息交互实现流程详解

栏    目:JAVA代码

下一篇:java string 转date方法如何实现

本文标题:在mybatis 中使用if else 进行判断的操作

本文地址:http://www.codeinn.net/misctech/77807.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有