MyBatis学习之动态SQL标签

来源:互联网 发布:js获取两位小数函数 编辑:程序博客网 时间:2024/06/05 12:48
  • 在实际开发中,往往有一些复杂的SQL语句,MyBatis中就需要写一些动态的SQL语句,这时就需要借助于MyBatis中的OGNL表达式,这样可以很方便的实现一些复杂的逻辑,MyBatis中的动态SQL标签主要有以下几种:

1.if标签(简单的条件判断)

<select id="dynamicIfTest" parameterType="Blog" resultType="Blog">        select * from t_blog where 1 = 1        <if test="title != null and title != ''">            and title = #{title}        </if>        <if test="content != null">            and content = #{content}        </if>        <if test="owner != null">            and owner = #{owner}        </if></select>

2.choose(when,otherwise)标签,相当于java中的Switch语句,和JSTL中的基本一样

<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">        select * from t_blog where 1 = 1         <choose>            <when test="title != null and title != ''">                and title = #{title}            </when>            <when test="content != null and content != ''">                and content = #{content}            </when>            <otherwise>                and owner = "owner1"            </otherwise>        </choose></select>

:when中有条件满足的时候,就会跳出choose,即所有的when和otherwise条件中,只有一个会输出
3.trim标签 (对包含的内容加上 prefix或者 suffix 等,即加上前缀和后缀)

<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">        select * from t_blog         <trim prefix="where" prefixOverrides="and |or">            <if test="title != null and title != ''">                and title = #{title}            </if>            <if test="content != null" content != ''>                and content = #{content}            </if>            <if test="owner != null and owner != ''">                or owner = #{owner}            </if>        </trim></select>

4.where标签(主要用于简化sql语句的where条件判断,智能的处理and|or, 不必担心多余的and或者or导致语法错误)

<select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">        select * from t_blog         <where>            <if test="title != null and title != ''">                title = #{title}            </if>            <if test="content != null and content != ''">                and content = #{content}            </if>            <if test="owner != null and owner != ''">                and owner = #{owner}            </if>        </where></select>

:where元素的作用是会在写入where元素的地方输出一个where,另外一个好处是你不需要考虑where元素里面的条件输出是什么样子的,MyBatis会智能的帮你处理,如果所有的条件都不满足那么MyBatis就会查出所有的记录,如果输出后是and 开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在where元素中你不需要考虑空格的问题,MyBatis会智能的帮你加上。像上述例子中,如果title=null, 而content != null,那么输出的整个语句会是select * from t_blog where content = #{content},而不是select * from t_blog where and content = #{content},因为MyBatis会智能的把首个and 或 or 给忽略。
5.set标签(主要用于更新语句时使用)

<update id="dynamicSetTest" parameterType="Blog">        update t_blog        <set>            <if test="title != null">                title = #{title},            </if>            <if test="content != null">                content = #{content},            </if>            <if test="owner != null">                owner = #{owner}            </if>        </set>        where id = #{id}</update>

:set标签主要是用在对数据库执行更新操作的时候,它的主要功能和where元素其实是差不多的,主要是在包含的语句前输出一个set,然后如果包含的语句是以逗号结束的话将会把该逗号忽略,如果set包含的内容为空的话则会出错。有了set标签我们就可以动态的更新那些修改了的字段,对于没有修改的则不会更新
6.foreach标签(在实现in的查询语句时特别有用)
foreach标签主要用在构建SQL语句中的in条件中,它可以在SQL语句中进行迭代一个集合。foreach标签的属性主要有item,index,collection,open,separator,close等一系列属性。item表示集合中每一个元素进行迭代时的别名;index表示迭代的索引,用于表示在迭代过程中,每次迭代到的位置,从0开始;open表示该语句以什么开始;separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束;在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有以下3种情况:
a、如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

<select id="dynamicForeachTest" parameterType="list" resultType="Blog">            select * from t_blog where id in            <foreach collection="list" index="index" item="item" open="(" separator="," close=")">                #{item}            </foreach></select>

b、如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

<select id="dynamicForeach2Test" parameterType="array" resultType="Blog">            select * from t_blog where id in            <foreach collection="array" index="index" item="item" open="(" separator="," close=")">                #{item}            </foreach></select>

c、如果传入的参数有多个的时候,即不仅仅只需要一个集合,还需要一些其他的参数时,我们就需要把它们封装成一个Map了,当然单参数也可以封装成Map,实际上在你传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,Map的key就是参数名,所以这个时候collection属性值就是传入的List或Array对象在自己封装的Map里面的key值,这时不单单forech中的collection属性是Map的key值,其它所有属性都是Map中的key值

<select id="dynamicForeach3Test" parameterType="map" resultMap="BaseResultMap">             select * from t_blog where 1=1             <if test="title != null and title != ''">                and title like concat('%',#{title},'%');             </if>            <if test="contentCategoryIds != null">                and id in                <foreach collection="map.key" index="index" item="item" open="(" separator="," close=")">                    #{item}                </foreach>            </if></select>

注:当为传进来的是一个Map类型的参数时,取值就是Map中的key值,像上面的title和ids就都是传进来的Map集合中的key值。而且以上的模糊查询是针对MySql数据库的,对于其它的数据库模糊查询的格式可能不一样

通过以上6种MyBatis中常用的标签,就能完成一般的动态SQL 语句,最常用的就是 if where foreach这几个标签,一定要重点掌握。当然MyBatis在Java代码中也可以动态生成SQL文。
当程序中存在复杂的SQL,无法采用动态标签来实现,那么就可以借助于MyBatis框架中的SelectBuilder和SqlBuilder来生成动态的SQL。
查询相关:

SelectBuilder.BEGIN();SelectBuilder.SELECT("*");SelectBuilder.FROM("tbl_user");SelectBuilder.WHERE("id=1");SelectBuilder.AND();SelectBuilder.WHERE("id=2");String sql = SelectBuilder.SQL();SqlRunner run = new SqlRunner(session.getConnection());List<Map<String Object>> maps=run.selectAll(sql);

增删改则借助SqlBuilder,生成方式和上面一样。
注意:以上的SelectBuilder类是过时的,我们可以试着只导入该类中的静态方法来避免警告。
import:该关键字表示导入指定包中的类,
import static:这是jdk1.5之后引入的特性,该关键字表示导入一个类中的静态方法

0 0