目录
MyBatis的动态SQL
/  

MyBatis的动态SQL

动态SQL

< if >元素

  • 通过对姓名的模糊搜索,使用if判断姓名是否为空,如果为空,则是查询所有用户

<!--    使用if元素根据条件动态查询用户信息-->
    <select id="selectUserByIf" resultType="cn.test.beans.MyUser" parameterType="cn.test.beans.MyUser">
        select * from user where 1 = 1
        <if test="uname != null and uname != ''">
        <!-- 这里是根据姓名模糊查找 -->
            and uname like concat('%', #{uname}, '%')
        </if>
        <!-- concat()函数, 将多个字符串连接成一个字符串 -->
    </select>

< choose >、< when >、< otherwise>元素

有的时候不想用到所有的条件语句,而只想从中择取一二,针对这种情况,MyBatis提供了<choose>元素,它有点像java中的switch语句。

<!--    使用choose、when、otherwise元素根据条件动态查询用户信息-->
    <select id="selectUserByChoose" resultType="cn.test.beans.MyUser" parameterType="cn.test.beans.MyUser">
        select * from user where 1 = 1
        <choose>
            <when test="uname != null and uname != ''">
                and uname like concat('%', #{uname}, '%')
            </when>
            <when test="usex != null and usex != ''">
                and usex = #{usex}
            </when>
            <otherwise>
                and uid > 10
            </otherwise>
        </choose>
    </select>

< trim>、< where>、< set>元素

  • < trim>元素
    • < trim>元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以是在其后加上某些后缀,与之对应的属性是prefix和suffix; 可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides。正因为<trim>元素有这样的功能,所以也可以非常简单地利用< trim>来代替< where>元素的个功能。
<!--    使用trim元素根据条件动态查询用户信息-->

    <select id="selectUserByTrim" resultType="cn.test.beans.MyUser" parameterType="cn.test.beans.MyUser">
        select * from user
        <trim prefix="where" prefixOverrides="and | or">
            <if test="uname != null and uname != ''">
                and uname like concat('%', #{uname}, '%')
            </if>
            <if test="usex != null and usex != ''">
                and usex = #{usex}
            </if>
        </trim>
    </select>

  • < where>元素
    • < where>元素的作用是会在写入< where>元素的地方输出一个where语句,另一个好处是不需要考虑< where>元素里面的条件输出是什么样子的,MyBatis将智能处理。如果所有的条件都不满足,那么MyBatis就会查出所有的记录,如果输出是以and开头的,MyBatis会把地一个and忽略,当然如果是以or开头的,MyBatis也会把它忽略; 此外,在< where>元素中不需要考虑空格的问题,MyBatis将智能加上。
<!--    使用where元素根据条件动态查询用户信息-->

    <select id="selectUserByWhere" resultType="cn.test.beans.MyUser" parameterType="cn.test.beans.MyUser">
        select * from user
        <where>
            <if test="uname != null and uname != ''">
                and uname like concat('%', #{uname}, '%')
            </if>
            <if test="usex != null and usex != ''">
                and usex = #{usex}
            </if>
        </where>
    </select>

运行结果和sql语句同上

  • < set>元素
    • 在动态update语句中可以使用< set>元素动态更新序列。
<!--    使用set元素动态修改一个用户-->

    <update id="updateUserBySet" parameterType="cn.test.beans.MyUser">
        update user
        <set>
            <if test="uname != null">
                uname = #{uname}, <!--这里的逗号不能少-->
            </if>
            <if test="usex != null">
                usex = #{usex}
            </if>
        </set>
        where uid = #{uid}
    </update>

  • < foreach>元素
    • < foreach>元素主要用 在构建in条件中,它可以在SQL语句中迭代一个集合。< foreach>元素的属性有item、index、collection、open、separator、close、。item表示集合中每个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为间隔符,close表示以什么结束。在使用< foreach>元素时,最关键、最容易出错的是collection属性,该属性是必选的,但在不同情况下该属性的值是不一样的,主要有一下三种情况:
      • 如果传入的是单参数且参数类型是一个List,collection属性值为list
      • 如果传入的是单参数且参数类型是一个array数组,collection属性值为array
      • 如果传入的参数是多个,需要把它们封装成一个Map,当然单参数也可以封装成Map。Map的key是参数名, collection属性值是传入的List或Array对象在自己封装的Map中的key
<!--    使用foreach元素查询用户信息-->
    <select id="selectUserByForeach" resultType="cn.test.beans.MyUser" parameterType="List">

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

    </select>

        // 使用foreach元素查询用户信息
        List<Integer> listId = new ArrayList<Integer>();
        listId.add(34);
        listId.add(37);
        listId.add(39);
        List<MyUser> listByForeach = dao.selectUserByForeach(listId);
        System.out.println("foreach元素===============");
        for (MyUser myUser : listByForeach){
            System.out.println(myUser);
        }

< bind>元素

  • 在进行模糊查询时,如果使用"${}"拼接字符串,则无法防止SQL注入问题; 如果使用字符串拼接函数或连接符号,但不同数据库的拼接函数或连接符号不同,例如Mysql的concat()函数、Oracle的连接符号“||”, 这样SQL映射文件就需要根据不同的数据库提供不同的实现,显然比较麻烦,且不利于代码的移植。幸运的是,MyBatis提供了< bind>元素来解决这一问题。
<!--    使用bind元素进行模糊查询-->
    <select id="selectUserByBind" resultType="cn.test.beans.MyUser" parameterType="cn.test.beans.MyUser">
        <!--bind中的uname是cn.test.beans.MyUser的属性名 -->
        <bind name="paran_uname" value="'%' + uname + '%'"/>
        select * from user where uname like #{paran_uname}
    </select>


标题:MyBatis的动态SQL
作者:gitsilence
地址:https://blog.lacknb.cn/articles/2019/09/12/1577974155653.html