java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > MyBatis中SQL写法

MyBatis中关于SQL的写法总结

作者:是Cc哈

这篇文章主要介绍了MyBatis中关于SQL的写法总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

最近MyBatis使用较多,在这里简单总结一下MyBatis的sql写法

说简单一点mybatis就是写原⽣sql,官方都说了 mybatis 的动态sql语句是基于OGNL表达式的。可以方便的在 sql 语句中实现某些逻辑.

总体说来mybatis 动态SQL 语句主要有以下几类:

一、MyBatis – if 语句

< select id="dynamicIfTest" 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>

if语句十分好理解,只有提供title,content,owener,这三个参数,才会返回满足这些条件的所有结果,这是一个非常有用的功能,如果使用JDBC就需要拼SQL语句,是非常麻烦的,相比较而言,MyBatis提供的这种动态Sql就非常的简单

二、MyBatis --choose 语句

相当于java 语⾔中的 switch ,与 jstl 中的choose 很类似.

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

when元素表⽰当when中的条件满⾜的时候就输出其中的内容,跟JAVA中的switch效果差不多的是按条件的顺序,当when中有条件满⾜的时候,就会跳出choose,即所有的when和otherwise条件中,只有个会输出,当所有的我很条件都不满⾜的时候就输出otherwise中的内容。

所以上述语句的意思⾮常简单,当title!=null的时候就输出and titlte = #{title},不再往下判断条件,当title为空且content!=null的时候就出and content = #{content},当所有条件都不满⾜的时候就输出otherwise中的内容。

三、MyBatis – trim

对包含的内容加上 prefix,或者 suffix 等,前缀,后缀

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

后缀,与之对应的属性是prefix和suffix;可以把包含内容的⾸部某些内容覆盖,即忽略,也可以把尾的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;正因为trim有这样的功能,所以我们可以⾮常简单的利⽤trim来代替where元素的功能。

四、MyBatis – where

主要是⽤来简化sql语句中where条件判断的,能智能的处理 and or 条件

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

where元素的作⽤是会在写⼊where元素的地⽅输出⼀个where,另外⼀个好处是你不需要考虑where素⾥⾯的条件输出是什么样⼦的,MyBatis会智能的帮你处理,如果所有的条件都不满⾜那么MyBatis就查出所有的记录,如果输出后是and 开头的,MyBatis会把第⼀个and忽略,当然如果是or开头的MyBatis也会把它忽略;此外,在where元素中你不需要考虑空格的问题,MyBatis会智能的帮你加上。

像上述⼦中,如果title=null, ⽽content != null,那么输出的整个语句会是select * fromt_blog where content = {content},⽽不是select * from t_blog where and content = #{content},因为MyBatis会智能的把⾸个and 或 or 给忽略。

五、MyBatis – 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元素我们就可以动态的更新那些修改了的字段。

六、MyBatis – foreach

在实现 mybatis in 语句查询时特别有⽤

<select id="queryById" resultMap="BaseReslutMap" >
    select * FROM entity
    where id  in
    <foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
        #{userid}
    </foreach>
</select>

foreach的主要⽤在构建in条件中,它可以在SQL语句中进⾏迭代⼀个集合。

foreach元素的属性主要有item,index,collection,open,separator,close。

item表⽰集合中每⼀个元素进⾏迭代时的别名index指定⼀个名字,⽤于表⽰在迭代过程中,每次迭代到的位置,open表⽰该语句以什么开始,separator⽰在每次进⾏迭代之间以什么符号作为分隔符,close表⽰以什么结束,在使⽤foreach的时候最关键的也最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不⼀样的,主要有⼀下3种情况:

七、MyBatis – concat

模糊查询

<select id="queryById" resultMap="BascResultMap" parameterType="entity">
    SELECT *  from entity
    <where>
        <if test="name!=null">
            name like  concat('%',concat(#{name},'%'))
        </if>
    </where>
</select>

八、MyBatis – sql片段

sql⽚段标签 :通过该标签可定义能复⽤的sql语句⽚段,在执⾏sql语句标签中直接引⽤即可。

这既可以提⾼编码效率,还能有效简化代码,提⾼可读性。

<!--定义sql⽚段-->
<sql id="orderAndItem">
    o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count
</sql>
<select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">
    select
    <!--引⽤sql⽚段-->
    <include refid="orderAndItem" />
    from ordertable o
    join orderitem i on o.orderitem_id = i.orderitem_id
    where o.order_id = #{orderId}
</select>

需要配置的属性:id="" >>>表⽰需要改sql语句⽚段的唯⼀标识

引用:通过 标签引⽤,refid="" 中的值指向需要引⽤的 中的id=“”属性

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

您可能感兴趣的文章:
阅读全文