Mybaits第二轮认知

来源:互联网 发布:好玩照片app软件 编辑:程序博客网 时间:2024/06/10 17:36

Mybatis动态语句:

将SQL语句条件连接在一起

If;

choose(when,otherwise);

trim(where,set);

foreach;

一:If

动态 SQL 中最常做的事情就是用条件地包含一个 where 子句。比如:

<select id=”findActiveBlogWithTitleLike”parameterType=”Blog” resultType=”Blog”>SELECT * FROM BLOGWHERE state = ‘ACTIVE’<if test=”title != null”>AND title like #{title}</if></select>
如果有title就返回对应的Blog,如果没有就返回所有的Blog.

如果我们想要可选地使用标题或者作者查询怎么办?简单地加上另外一个条件。

<select id=”findActiveBlogLike”parameterType=”Blog” resultType=”Blog”>SELECT * FROM BLOG WHERE state = ‘ACTIVE’<if test=”title != null”>AND title like #{title}</if><if test=”author != null and author.name != null”>AND title like #{author.name}</if></select>

二、choose,when,otherwise

有时候我们并不想应用所有条件,而只是想从多个选项中选择一个。类似于Java的Switch语句,Mybatis提供了choose元素。

只搜寻有查询标题的,或者只搜索有查询作者的。如果都没提交,则只选择有特性的(比如说管理员加精的或者是置顶的)

<select id=”findActiveBlogLike”parameterType=”Blog” 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 title like #{author.name}</when><otherwise>AND featured = 1</otherwise></choose></select>

Choose元素在多个选项中选择一个。

三、Trim,Where,set

如果现在把“Active=1”也作为条件,会发生什么情况

<select id=”findActiveBlogLike”parameterType=”Blog” resultType=”Blog”>SELECT * FROM BLOGWHERE<if test=”state != null”>state = #{state}
</if><if test=”title != null”>AND title like #{title}</if><if test=”author != null and author.name != null”>AND title like #{author.name}</if></select>
如果一个条件都不给出,语句是这样:

SELECT * FROM BLOGWHERE
如果仅仅要第二个条件,语句是这样:

SELECT * FROM BLOGWHEREAND title like ‘someTitle’
这样很糟

Mybatis有个简单的解决方案,解决这里的问题,Where:

<select id=”findActiveBlogLike”parameterType=”Blog” resultType=”Blog”>SELECT * FROM BLOG<where><if test=”state != null”>state = #{state}</if><if test=”title != null”>AND title like #{title}</if><if test=”author != null and author.name != null”>AND title like #{author.name}</if></where></select>
这个Where元素会知道如果他包含的标签中有返回值的话,就插入一个"where".此外,如果标签返回的内容为AND或OR开头则它会剔除。

如果where 没有完全按照想象那样,则可以用trim元素定义它,下面的trim相同与where:

<trim prefix="WHERE" prefixOverrides="AND |OR ">…</trim>

Set元素被用来动态囊括列名来更新,而忽略其他的。比如

<update id="updateAuthorIfNecessary"parameterType="domain.blog.Author">update Author<set><if test="username != null">username=#{username},</if><if test="password != null">password=#{password},</if><if test="email != null">email=#{email},</if><if test="bio != null">bio=#{bio}</if></set>where id=#{id}</update>

set元素将动态的配置SET关键字,也用来剔除追加到条件末尾的任何不相关的逗号。

等同的trim是这样写:

<trim prefix="SET" suffixOverrides=",">…</trim>

注意:我们只写了一个前缀,同样我们可以加一个后缀。

Foreach

另外一个对于SQL非常重要,主要是迭代一个集合,通常是用于IN条件,比如说:

<select id="selectPostIn" resultType="domain.blog.Post">SELECT *FROM POST PWHERE ID in<foreach item="item" index="index" collection="list"open="(" separator="," close=")">#{item}</foreach></select>
oreach 元素非常强大,允许你指定一个集合,申明一个项和一个索引变量,用在这个元素的方法体内 。
也允许你指定开始和结束的字符,也可以在两个迭代器之间加入一个分隔符。它的智能之处在于它不会
偶尔追加额外的分隔符。
注意:你可以传入一个 List 实例或者一个数组给 MyBatis 作为一个参数对象。如果你这么做,MyBatis
会自动将它包装成一个 Map, 使用它的名称做为 key。 List 实例将使用 “list” 做为键, 数组实例以 “array ”

作为键。











0 0
原创粉丝点击