动态SQL

01、if标签

<!-- 演示动态sql-if标签的使用情景 -->
	<select id="getUserByWhere" parameterType="user" resultType="com.xu.mybatis.pojo.User">
		<!-- SELECT * FROM USER WHERE username LIKE '%${username}%' and id = #{id} -->
		<!-- 1=1 是为了保证sql语句在if判断为空时可以正常运行 -->
		SELECT * FROM USER where 1 = 1
		<!-- if标签的使用 -->
		<if test="id != null">
			and id = #{id}
		</if>
		<if test="username != null and username != ''">
			and username LIKE '%${username}%'
		</if>
	</select>

02、where标签

<!-- 演示动态sql-where标签的使用情景 -->
	<select id="getUserByWhere2" parameterType="user"
		resultType="com.xu.mybatis.pojo.User">
		<!-- include:引入sql片段,refid引入片段id -->
		SELECT
		*
		FROM USER
		<!-- where会自动加上where同处理多余的and -->
		<where>
			<!-- if标签的使用 -->
			<if test="id != null">
				and id = #{id}
			</if>
			<if test="username != null and username != ''">
				and username LIKE '%${username}%'
			</if>
		</where>
</select>

03、sql片段

<!-- sql片段 定义,id:片段唯一标识 -->
	<sql id="user_column">
		`id`,
		`username`,
		`birthday`,
		`sex`,
		`address`,
		`uuid2`
	</sql>


<!--sql片段的引入-->
<include refid="id">

04、Foreach标签

<!-- 演示动态sql-foreach标签的使用情景 -->
	<select id="getUserByIds" parameterType="queryvo"
		resultType="com.xu.mybatis.pojo.User">
		
		SELECT
		*
		FROM USER
		<!-- where会自动加上where同处理多余的and -->
		<where>
			<!-- id IN(1,10,25,30,34) -->
			<!-- foreach循环标签 
				 collection:要遍历的集合,来源入参 
				 open:循环开始前的sql 
				 separator:分隔符 
				 close:循环结束拼接的sql
			-->
			<foreach item="uid" collection="ids" open="id IN(" separator=","
				close=")">
				#{uid}
			</foreach>		
		</where>
	</select>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值