MyBatis学习笔记(3)------动态SQL语句

if标签

在使用动态SQL标签时要注意SQL语句的规范,如果有条件判定要确定无论是否执行符合if条件,SQL语句都能通过编译。
例如:

<update id="updateByIdSelective">
		update sys_user
		<set>
			<if test="userName != null and userName != ''">
				user_name = #{userName},
			</if>
			<if test="userPassword != null and userPassword != ''">
				user_password = #{userPassword},
			</if>
			<if test="userInfo != null and userInfo != ''">
				user_info = #{userInfo},
			</if>
			<if test="createTime != null">
				create_time = #{createTime, jdbcType=TIMESTAMP},
			</if>
			id = #{id}
		</set>
		where id = #{id}
</update>

其中</ set>结束标签之前的 id = #{id} 是为了防止出现update sys_user set where id = ?
在 insert 中使用要注意,如果在列的部分增加 if 条件,则 values 部分也要增加相同的 if 条件,保证上下对应匹配。

choose标签

choose标签下要有when和otherwise两种逻辑结果,否则筛选不起作用。

where标签

where 标签作用:如果该标签包含的元素中有返回值,就插入一个where;如果 where 后面的字符是以 AND 和 OR 开头的,就将它们剔除。

<select id="selectByUser" parameterType="tk.mybatis.simple.model.SysUser" resultType="tk.mybatis.simple.model.SysUser">
		select id, user_name, user_info, create_time
		from sys_user
		<where>
			<if test="userName != null and userName != ''">
				and user_name = #{userName}
			</if>
			<if test="userInfo != null and userInfo != ''">
				<bind name="userInfoLike" value="'%' + userInfo + '%'"/>
				and user_info like #{userInfoLike}
			</if>
		</where>
</select>

set标签

set 标签的作用:如果该标签包含的元素中有返回值,就插入一个 set;如果 set 后面的字符串是以逗号结尾的,就将这个逗号剔除。

<update id="updateByIdSelective">
		update sys_user
		<set>
			<if test="userName != null and userName != ''">
				user_name = #{userName},
			</if>
			<if test="userPassword != null and userPassword != ''">
				user_password = #{userPassword},
			</if>
			<if test="userInfo != null and userInfo != ''">
				user_info = #{userInfo},
			</if>
			<if test="createTime != null">
				create_time = #{createTime, jdbcType=TIMESTAMP},
			</if>
			id = #{id},
		</set>
		where id = #{id}
</update>

foreach标签

foreach 可以对数组、map或者实现了iterable接口(List、Set)的对象进行遍历。数组会转换为List对象,因此 foreach 遍历的对象可以分为两大类:Iterable类型和 map 类型。

标签下属性 作用
collection 必填,值为要循环的属性名,可以用@Param注解指定的名称
item 变量名,相当于map中的value
index 索引属性名,相当于map中的key
open 指定循环内容的开头,比如添加(
close 指定循环内容的结尾,比如添加)
separator 每次循环的分隔符,比如”,”

参数为List时(List默认collection为 list;数组默认collection为 array)

<select id="selectByIdList" resultType="tk.mybatis.simple.model.SysUser">
		select id, user_name, user_password, user_info, create_time
		from sys_user
		where id in
		<foreach collection="list" open="(" close=")" separator="," item="id" index="i">
			#{id}
		</foreach>
</select>

参数类型是map时(Map类型默认collection为collection)

<update id="updateByMap">
		update sys_user
		set
		<foreach collection="user" index="key" item="val" separator=",">
			${key} = #{val}
		</foreach>
		where id = #{user.id}//因为这句在foreach循环外,需要用点属性名指定
</update>

foreach实现批量插入示例

<insert id="insertList" useGeneratedKeys="true" keyProperty="id">
		insert into sys_user(user_name, user_password, user_info, create_time)
		values
		<foreach collection="users" item="user" separator="," >
			(#{user.userName}, #{user.userPassword}, #{user.userInfo},
			 #{user.createTime, jdbcType=TIMESTAMP})
		</foreach>
</insert>

Mybatis3.3.1 开始支持批量新增回写主键值的功能(仅有MySQL数据库完美支持),只需要在XML insert 标签中添加 userGeneratedKeys=”true” 和 keyProperty=”id” 即可。

bind标签

可以用来连接字符串,比如模糊查询的%%

<if test="userInfo != null and userInfo != ''">
	<bind name="userInfoLike" value="'%' + userInfo + '%'"/>
		and user_info like #{userInfoLike}
</if>

OGNL用法

  1. e1 or e2
  2. e1 and e2
  3. e1 == e2 或 e1 eq e2
  4. e1 != e2 或 e1 neq e2
  5. e1 lt e2 小于
  6. e1 lte e2 小于等于(gt 大于 gte大于等于)
  7. e1 + e2、e1 – e2、e1 * e2、e1 / e2
  8. !e 或 not e
  9. e.method(args):调用对象方法
  10. e.property:对象属性
  11. e1[e2] 按索引取值(List、数组或Map)
  12. @class@method(args):调用类的静态方法
  13. @class@field:调用类的静态字段值

@class@method(args) 可用来进行特殊的校验,例如if中的判断:

<if test="@tk.mybatis.util.StringUtil@isEmpty(userName)">
			and user_name = #{userName}
</if>

StringUtil类如下:

public class StringUtil {
	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}
}

如果想知道映射XML中方法执行的参数也可以通过调用方法的方式:

public static void print(Object parameter) {
		System.out.println(parameter);
}

猜你喜欢

转载自blog.csdn.net/BZeHong/article/details/86770691
今日推荐