Mybatis学习总结(四)

mybatis动态sql

MyBatis 的一个强大的特性之一通常是它的动态 SQL 能力。如果你有使用 JDBC 或其他相似框架的经验,你就明白条件地串联 SQL 字符串在一起是多么的痛苦,确保不能忘了空格或在列表的最后省略逗号。动态 SQL 可以彻底处理这种痛苦。,MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。从而拼接成相应的sql语句。MyBatis中用于实现动态SQL的元素主要有:

  • if
  • choose
  • when
  • otherwise
  • trim
  • where
  • set
  • foreach

if

<select id="selectUser" parameterType="user" resultType="userResultMap">
        select * from t_user where 1=1
        <if test="name!=null">
            and name=#{name}
        </if>
        <if test="pwd!=null">
            and pwd=#{pwd}
        </if>
        <if test="id!=null">
                and id=#{id}
        </if>
    </select>

where

where元素的作用是会在写入where元素的地方输出一个where,另外一个好处是你不需要考虑where元素里面的条件输出是什么样子的,MyBatis会智能的帮你处理,如果所有的条件都不满足那么MyBatis就会查出所有的记录,如果输出后是and 开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在where元素中你不需要考虑空格的问题,MyBatis会智能的帮你加上

<select id="selectUser" parameterType="user" resultType="userResultMap">
        select * from t_user
        <where>
            <if test="name!=null">
                 name=#{name}
            </if>
            <if test="pwd!=null">
                and pwd=#{pwd}
            </if>
            <if test="id!=null">
                and id=#{id}
            </if>
        </where>
    </select>

choose

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

<select id="selectUser3" parameterType="user" resultType="userResultMap">
        select * from t_user where 1=1
        <choose>
            <when test="name!null">
                and name=#{name}
            </when>
            <when test="pwd!=null">
                and pwd=#{pwd}
            </when>
            <otherwise>
                and id=#{id}
            </otherwise>
        </choose>
    </select>

trim

trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;

<select id="selectUser2" parameterType="user" resultType="userResultMap">
        select * from t_user
        <trim prefix="where" prefixOverrides="and|or">
            <if test="name!=null">
                name=#{name}
            </if>
            <if test="pwd!=null">
                and pwd=#{pwd}
            </if>
            <if test="id!=null">
                or id=#{id}
            </if>
        </trim>
    </select>

set

set元素主要是用在更新操作的时候,它的主要功能和where元素其实是差不多的,主要是在包含的语句前输出一个set,然后如果包含的语句是以逗号结束或开始的话将会把该逗号忽略,如果set包含的内容为空的话则会出错。

<update id="updateUser" parameterType="user">
        update t_user
        <set>
            <if test="name!=null">
                name=#{name}
            </if>
            <if test="pwd!=null">
                ,pwd=#{pwd}
            </if>
        </set>
        where id=#{id}
    </update>

foreach

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的。

<select id="selectUser" resultType="user">
        select * from t_user where id in
        <foreach collection="list" item="item"  index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

测试代码

@TEST
public void selectUsers() {
    SqlSession sqlSession = MybatisUtil.getSqlSession();
    List<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(2);
    list.add(3);
    List<User> users=sqlSession.selectList("mapper.userMapper.selectUsers", list);
    for(User user:users)
    System.out.println(user);
    }

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值