游戏中MyBatis的动态SQL语句写法

来源:互联网 发布:windows软件功能 编辑:程序博客网 时间:2024/04/29 16:18

MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。

MyBatis中用于实现动态SQL的元素主要有:

1
2
3
4
5
6
if
choose(when,otherwise)
trim
where
set
foreach

if就是简单的条件判断,利用if语句我们可以实现某些简单的条件选择。先来看如下一个例子:

    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<select id="selectBlog" parameterType="Blog" resultType="Blog">
    select from t_blog where 1 = 1
    <if test="title != null">
  
        and title = #{title}
    </if>
    <if test="content != null">
        and content = #{content}
    </if>
    <if test="owner != null">
        and owner = #{owner}
    </if>
  
</select>

 


这条语句的意思非常简单,如果你提供了title参数,那么就要满足title=#{title},同样如果你提供了Content和Owner的时候,它们也需要满足相应的条件,之后就是返回满足这些条件的所有Blog,这是非常有用的一个功能,以往我们使用其他类型框架或者直接使用JDBC的时候, 如果我们要达到同样的选择效果的时候,我们就需要拼SQL语句,这是极其麻烦的,比起来,上述的动态SQL就要简单多了。

后续请参照原文:http://www.youxijishu.com/blogs/37.html

0 0