mybatis 九大动态标签详解

一、if:你们能判断,我也能判断!
<select id="count" resultType="java.lang.Integer">
	select count(*) from user where <if test="id != null">id = #{id}</if> and username = 'xiaoming'
</select>
  • 如果传入的 id 不为 null, 那么才会 SQL 才会拼接 id = #{id}
  • 如果传入的 id 为 null,那么最终的 SQL 语句就变成了 select count(*) from user where and username = ‘xiaoming’。这语句就会有问题,这时候 where 标签就该隆重登场了
二、where:有了我,SQL 语句拼接条件神马的都是浮云!
<select id="count" resultType="java.lang.Integer">
	select count(*) from user
	<where>
		<if test="id != null">id = #{id}</if>
		and username = 'xiaoming'
    </where>
 </select>
  • where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入 WHERE 子句
  • 若语句的开头为 AND、OR,where 元素也会将它们去除。还可以通过 trim 标签去自定义这种处理规则
三、trim:我的地盘,我做主!
trim 标签一般用于拼接、去除 SQL 的前缀、后缀
trim 标签中的属性
属性描述
prefix拼接前缀
suffix拼接后缀
prefixOverrides去除前缀
suffixOverrides去除后缀
<select id="count" result="java.lang.Integer">
	select count(*) from user
	<trim prefix ="where" prefixOverrides="and | or">
		<if test="id != null">id = #{id}</if>
		<if test="username != null"> and username = #{username}</if>
	</trim>
</select>
  • 如果 id 或者 username 有一个不为空,则在语句前加入 where。如果 where 后面紧随 and 或 or 就会自动会去除
  • 如果 id 或者 username 都为空,则不拼接任何东西
四、set: 信我,不出错!
<update id="UPDATE" parameterType="User">
	update user
    <set>
    	<if test="name != null">name = #{name},</if> 
        <if test="password != null">password = #{password},</if> 
        <if test="age != null">age = #{age},</if> 
   	</set>
</update>
  • 三个 if 至少有一个不为空。会在前面加上 set,自动去除尾部多余的逗号
五、foreach: 你有 for,我有 foreach
foreach 标签中的属性
属性描述
index下标
item每个元素名称
open该语句以什么开始
close该语句以什么结尾
separator在每次迭代之间以什么作为分隔符
collection参数类型
collection:
  • 如果参数类型为 List,则该值为 list
<select id="count" resultType="java.lang.Integer">
	select count(*) from user where id in
  	<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
        #{item}
  	</foreach>
</select>
  • 如果参数类型为数组,则该值为 array
<select id="count" resultType="java.lang.Integer">
	select * from user where id inarray
  	<foreach collection="array" item="item" index="index" open="(" separator="," close=")">
        #{item}
  	</foreach>
</select>
  • 如果参数类型为 Map,则参数类型为 Map 的 Key
六、choose: 我选择了你,你选择了我!
<select id="count" resultType="Blog">
	select count(*) from user
  	<choose>
    	<when test="id != null">
      		and id = #{id}
    	</when>
    	<when test="username != null">
      		and username = #{username}
    	</when>
    	<otherwise>
      		and age = 18
    	</otherwise>
  	</choose>
</select>
  • 当 id 和 username 都不为空的时候, 那么选择二选一(前者优先)
  • 如果都为空,那么就选择 otherwise 中的
  • 如果 id 和 username 只有一个不为空,那么就选择不为空的那个
七、sql:相当于 Java 中的代码提重,需要配合 include 使用
<sql id="table"> user </sql>
八、include:相当于 Java 中的方法调用
<select id="count" resultType="java.lang.Integer">
	select count(*) from <include refid=“table(sql 标签中的 id 值)” />
</select>
九、bind:对数据进行再加工
<select id="count" resultType="java.lang.Integer">
	select count(*) from user
	<where>
		<if test="name != null">
			<bind name="name" value="'%' + username + '%'"
			name = #{name}
		</if>
</select>
  • 20
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值