mybatis总结(四)(mybatis的动态sql)

if判断

例子:对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。

原sql:

<!-- 查询 -->
<select id="findUserById" parameterType="com.tm.pojo.User" resultType="com.tm.pojo.User">
    select id,name from user where id=#{id} and username=#{username}
</select>

使用if和where组合后的:

<select id="findUserById" parameterType="com.tm.pojo.User" resultType="com.tm.pojo.User">
    <!-- where可以自动去掉条件中的第一个and -->
    <where>
        <!-- 如果是那种嵌套格式的pojo,还可以在两个if的外面再加上if -->
        <if test="id!=null">
            and id = #{id}
        </if>
        <if test="name!=null">
            and name = #{name}
        </if>
    </where>
</select>

 sql片段

 

在编写代码的时候,有些sql内容是通用的,为了简化代码,我们往往可以采用sql片段来处理。

原:

<!-- 查询 -->
<select id="findUserById" parameterType="String" resultType="com.tm.pojo.User">
    select id,name from user 
</select>

使用sql片段之后:

<!-- 定义要查询的列 -->
<sql id="base_column">
    id,name
</sql>
<!-- 定义要查询的表名 -->
<sql id="base_tablename">
    user
</sql>

<!-- 查询 -->
<select id="findUserById" parameterType="String" resultType="com.tm.pojo.User">
    select 
        <include refid="base_column"/>
    from 
        <include refid="base_tablename"/>
</select>

choose、when、otherwise元素用法

 

下面的逻辑可能不是太合理,仅作为学习的例子。

<!-- 查询 -->
<select id="findUserById" parameterType="String" resultType="com.tm.pojo.User">
    select id,name,sex,age from user where 1=1
    <choose>
        <when test="sex != null and sex != ''">
            and sex = #{sex}
        </when>
        <when test="name!=null and name!=''">
            and name like concat('%',#{name},'%')
        </when>
        <otherwise>
            and age is not null
        </otherwise>
    </choose>
</select>

where元素用法

 

where元素的用法很简单,比如一个sql中的查询条件是动态的,那么我们一般会使用where 1=1的技巧来实现,但是如果使用了where标签了,就需要where 1=1的形式了,具体例子可以参考 'if判断'的例子。

trim元素用法

 

trim元素意味着要去掉一些特殊的字符串,prefix代表的是语句的前缀,而prefixOverrides代表的是需要去掉哪种字符串。

<select id="findUserById" parameterType="com.tm.pojo.User" resultType="com.tm.pojo.User">
    select id,name from user 
    <trim prefix="where" prefixOverrides="and">
        <if test="name != null">
            and name = #{name}
        </if>
    </trim>
</select>

set用法

一般用于update sql,去掉逗号的操作。

<update id="updateUser" parameterType="com.tm.pojo.User">
    update user
    <set>
        <if test="name != null and name != ''">
            name = #{name},
        </if>
        <if test="sex != null and sex != ''">
            sex = #{sex}
        </if>
    </set>
    where age = #{age}
</update>

 foreach

 

foreach元素是一个循环语句,它的作用是循环遍历,它能够很好地支持数组和List、set接口的集合,对此提供遍历功能。它往往用于sql中的in关键字。

<select id="findUserById" parameterType="com.tm.pojo.User" resultType="com.tm.pojo.User">
    select * from user where name in 
        <foreach item="name" index="index" collection="nameList" open="(" separator="," close=")">
            #{name}
        </foreach>
</select>

这里解释一下:

1. collection:配置的nameList是传递进来的参数名称,它可以是一个数组、List、Set等集合。

2. item:配置的是循环中当前的元素。

3. index:配置的是当前元素在集合的位置下标。

4. open和close:配置的是以什么符号将这些集合元素包装起来。

5. separator:是各个元素的间隔符。

bind元素的用法

 

bind元素的作用是通过OGNL表达式去自定义一个上下文变量。

<select id="findUserById" parameterType="String" resultType="com.tm.pojo.User">
    <bind name="pattern" value="'%' + _parameter + '%'" />
    select id,name from user where id=#{pattern} 
</select>

这里解释一下:

这里的_parameter代表的是传递进来的参数,它和通配符(%)连接后赋给了pattern,然后就可以在select语句中使用这个变量了。

 

持续更新!!!

原文地址:https://www.cnblogs.com/flyinghome/p/12358669.html