使用动态sql

动态SQL的元素种类

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

if标签的使用

判断查询条件是否存在,决定是否拼接在where条件后.
    <!--1.if标签-->
    <select id="selectByItem" parameterType="User" resultType="User">
        select * from user
        where
        1=1
        <if test="id !=null">
        and id = #{id}
        </if>
        <if test="username !=null and username !=''">
            and username = #{username}
        </if>
        <if test="password !=null and password !=''">
            and password = #{password}
        </if>
        <if test="sex !=null and sex !=''">
            and sex = #{sex}
        </if>
        <if test="age !=null">
            and age = #{age}
        </if>

    </select>

where标签的使用

生成where关键字,并可以去除紧跟where后的sql的关键字.
<select id="selectByItem2" parameterType="User" resultType="User">
        select * from user
       <where>
            <if test="id !=null">
                and id = #{id}
            </if>
            <if test="username !=null and username !=''">
                and username = #{username}
            </if>
            <if test="password !=null and password !=''">
                and password = #{password}
            </if>
            <if test="sex !=null and sex !=''">
                and sex = #{sex}
            </if>
            <if test="age !=null">
                and age = #{age}
            </if>
       </where>

    </select>

choose (when, otherwise) 标签的用法

他的作用相当于java中Switch语句,只能拼接满足的一个条件.
    <select id="selectByItem3" parameterType="User" resultType="User">
        select * from user
        <where>
            <choose>
                <when test="id !=null">
                    and id = #{id}
                </when>
                <when test="username !=null and username !=''">
                    and username = #{username}
                </when>
                <when test="password !=null and password !=''">
                    and password = #{password}
                </when>
                <when test="sex !=null and sex !=''">
                    and sex = #{sex}
                </when>
                <when test="age !=null">
                    and age = #{age}
                </when>
                <otherwise>
                    and 1=1
                </otherwise>
            </choose>
        </where>
    </select>

trim标签的使用

用来去除指这的sql字符串.
常用属性:
1.prefix: 生成sql条件前拼接前缀
2.prefixOverrides: 去除前缀后面的紧跟前缀的指定的字符串
3.suffix:生成sql字符串后拼接后缀
4.suffixOverrides:去除后缀前面的紧跟后缀的指定的字符串
<select id="selectByItem4" parameterType="User" resultType="User">
        select * from user
        <trim prefix="where" prefixOverrides="and | or">
            <if test="id !=null">
                and id = #{id}
            </if>
            <if test="username !=null and username !=''">
                and username = #{username}
            </if>
            <if test="password !=null and password !=''">
                and password = #{password}
            </if>
            <if test="sex !=null and sex !=''">
                and sex = #{sex}
            </if>
            <if test="age !=null">
                and age = #{age}
            </if>
        </trim>
    </select>

set标签的使用

和where标签类似.在生成的Sql串中生成set关键字
<update id="update2" parameterType="User" >
        update user
        <set>
            <if test="username!=null and username!=''">
                username=#{username},
            </if>
            <if test="password !=null and password !=''">
                password = #{password},
            </if>
            <if test="sex !=null and sex !=''">
                sex = #{sex},
            </if>
            <if test="age !=null">
                age = #{age},  <!--会去除生成sql串最后的关键字 ,-->
            </if>
        </set>
        where id = #{id}
    </update>

foreach标签的使用

和java中for作用一样,用于迭代集合或数组数据.
	<!--6.foreach标签,
        collection:如果是数组使用array,如果是集合list,set
        item:每次迭代的数据存放的变量
        open:是以指定字符串开始
        close:以指定字符串结束
        separator:指定分隔符
    -->
    <select id="selectByItem5"  resultType="User">
        SELECT * FROM USER
             <foreach collection="array" item="id" open=" WHERE  id IN(" separator="," close=")">
                #{id}
            </foreach>
    </select>

    <!--如果数组或集合数据封装到POJO中,则传入参数为POJO对像,collection中该POJO的相应数组或集合属性-->
    <select id="selectByItem6"  resultType="User" parameterType="User">
        SELECT * FROM USER
        <foreach collection="ids" item="id" open=" WHERE  id IN(" separator="," close=")">
            #{id}
        </foreach>
    </select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值