Mybatis_动态SQL

  • where标签+if标签
<select id="select" parameterType="java.util.Map" resultType="com.test.model.User">
  	select
  		user_id userid,
  		username,
  		password,
  		sex,
  		createtime
  	from
  		user
  	<!-- 
  		1. where 标签用于动态生成查询条件 可以控制是否添加where关键字
  			如果有查询条件则添加where,如果没有则不添加
  	 -->
  	<where>
  		<!-- 
  			2. if标签 判断条件
  				test 判断条件 结果为true 则拼接标签内的字符串
  		 -->
  		 <if test="username != null and username != ''">
  		 	and username like concat('%',#{username},'%')
  		 </if>
  		 
  		 <if test="sex != null">
  		 	and sex = #{sex}
  		 </if>
  		 
  		 <if test="startDay != null and startDay != ''">
  		 	and createtime >= #{startDay}
  		 </if>
  		 
  		 <if test="endDay != null and endDay != ''">
  		 	<!-- and createtime &lt;= #{endDay} -->
  		 	<!-- and #{endDay} >= createtime -->
  		 	<![CDATA[
  		 		and createtime <= concat(#{endDay},' 23:59:59') 
  		 	]]>
  		 </if>
  	</where> 
  • trim标签+if标签
<select id="select" parameterType="java.util.Map" resultType="com.test.model.User">
  	select
  		user_id userid,
  		username,
  		password,
  		sex,
  		createtime
  	from
  		user
  	<!-- trim  标签 修剪标签   
  		prefix 前缀 字符串
	  		当trim标签内的字符串不为空时,
	  		则在字符串的前面添加前缀字符串,prefixOverrides属性表示 替换的字符串
	  		如果为空就不添加前缀字符串
  	-->
	<trim prefix="where" prefixOverrides="and" >
	 <if test="username != null and username != ''">
	 	and username like concat('%',#{username},'%')
	 </if>
	 
	 <if test="sex != null">
	 	and sex = #{sex}
	 </if>
	 
	 <if test="startDay != null and startDay != ''">
	 	and createtime >= #{startDay}
	 </if>
	 
	 <if test="endDay != null and endDay != ''">
	 	<!-- and createtime &lt;= #{endDay} -->
	 	<!-- and #{endDay} >= createtime -->
	 	<![CDATA[
	 		and createtime <= concat(#{endDay},' 23:59:59') 
	 	]]>
	 </if>
	</trim>
  • choose标签(不常用)
<choose>
	<when test="type == 1">
		and 1=1
	</when>
	<when test="type == 2">
		or 1=1
	</when>
	<otherwise>
		and 1!=1
	</otherwise>
</choose>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值