Mybatis之动态sql标签的使用


当前第2页 返回上一页

2.5 choose

有时我们不想应用到所有的条件语句,而只想从中择其一项。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。
还是上面的例子,但是这次变为提供了“title”就按“title”查找,提供了“author”就按“author”查找的情形,若两者都没有提供,就返回所有符合条件的 BLOG(实际情况可能是由管理员按一定策略选出 BLOG 列表,而不是返回大量无意义的随机结果)。

<select id="findActiveBlogLike"
 resultType="Blog">
 SELECT * FROM BLOG WHERE state = ‘ACTIVE'
 <choose>
 <when test="title != null">
 AND title like #{title}
 </when>
 <when test="author != null and author.name != null">
 AND author_name like #{author.name}
 </when>
 <otherwise>
 AND featured = 1
 </otherwise>
 </choose>
</select>

2.6 foreach

常用的属性:
collection 要遍历的集合;
item 要遍历的元素;
index 元素在集合中的索引;
open 遍历以什么开头 比如 open=”and id in (“;
seprator 遍历出来的元素以什么分隔;
end 遍历以什么结束 end=”)”
动态 SQL 的另外一个常用的操作需求是对一个集合进行遍历,通常是在构建 IN 条件语句的时候。比如:

<select id="selectPostIn" resultType="domain.blog.Post">
 SELECT *
 FROM POST P
 WHERE ID in
 <foreach item="item" index="index" collection="list"
 open="(" separator="," close=")">
 #{item}
 </foreach>
</select>

你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象传递给 foreach 作为集合参数。当使用可迭代对象或者数组时,index 是当前迭代的次数,item 的值是本次迭代获取的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。

2.7 bind

bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文。这个动态标签可以完美解决#{}在某些时候不适用,而用美元{}又有sql注入的风险的情况(${}与#{}的区别)比如:

<select id="selectBlogsLike" resultType="Blog">
 <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
 SELECT * FROM BLOG
 WHERE title LIKE #{pattern}
</select>

2.8 insert

批量插入mysql 与oracle的区别:

2.8.1 mysql 批量插入

插入语句

<insert id="insertEmp">
 insert into t_emp (id,username)
 values 
 <foreach collection="userList" item="u" separator="," open="(" close=")">
 #{u.id},#{u.username}
 </foreach>
</insert>

预编译结果

insert into t_emp (id,username)
 values(?,?),(?,?),(?,?)

你可能会想把整个插入语句进行循环如下:
用;来分隔每一条插入语句

 <insert id="insertEmp">
 <foreach collection="userList" item="u" separator=";">
 insert into t_emp (id,username)
 values (#{u.id},#{u.username} )
 </foreach>
 </insert>

预编译结结果

insert into t_emp (id,username) values (?,?);
insert into t_emp (id,username) values (?,?);
insert into t_emp (id,username) values (?,?);

mysql默认是不支持这种语法,需要在url 后面的连接属性增加一个 allowMultiQueries=true; 该属性默认是关闭的。

2.8.2 oracle批量插入

oracle并不支持mysql这种语法

insert into t_emp (id,username) values(?,?),(?,?),(?,?) 

他只能通过如下来完成插入

begin 
insert into t_emp (id,username) values(?,?); 
insert into t_emp (id,username) values(?,?); 
insert into t_emp (id,username) values(?,?); 
end;

2.9 sql

这个元素可以被用来定义可重用的 SQL 代码段,可以包含在其他语句中。它可以被静态地(在加载参数) 参数化. 不同的属性值通过包含的实例变化. 比如:

<sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>
<select id="selectUsers" resultType="map">
 select
 <include refid="userColumns"><property name="alias" value="t1"/></include>,
 <include refid="userColumns"><property name="alias" value="t2"/></include>
 from some_table t1
 cross join some_table t2
</select>

到此这篇关于Mybatis之动态sql标签的使用的文章就介绍到这了,更多相关Mybatis 动态sql标签内容请搜索


打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...