MyBatis超详细讲解动态SQL的实现


当前第2页 返回上一页

可以说功能很强大,可以利用它来替代where元素,并实现与where元素相同的效果。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mapper.BookMapper">
 
    <resultMap id="BookType" type="org.example.po.Book">
        <id property="id" column="id"></id>
        <result property="bookName" column="bookName"></result>
        <result property="price" column="price"></result>
        <result property="publisher" column="publisher"></result>
    </resultMap>
 
    <update id="bookUpdate" parameterType="book">
        UPDATE t_book
        <trim prefix="set" suffixOverrides=",">
            <if test="bookName!=null and bookName!=''">bookName=#{bookName},</if>
            <if test="price!=null and price!=''">price=#{price},</if>
            <if test="publisher!=null and publisher!=''">publisher=#{publisher}</if>
            WHERE id=#{id}
        </trim>
    </update>
 
</mapper>

<set>:

      set元素主要用于更新操作,它的主要功能和where元素差不多,主要是包含的语句前输入一个set,若包含的语句以逗号结束,则会自动把括号忽略掉,再配合if元素就可以动态地更新需要修改的字段;若不需要更改字段,则可以不再被更新。

把上面的代码修改一下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mapper.BookMapper">
 
    <resultMap id="BookType" type="org.example.po.Book">
        <id property="id" column="id"></id>
        <result property="bookName" column="bookName"></result>
        <result property="price" column="price"></result>
        <result property="publisher" column="publisher"></result>
    </resultMap>
 
    <update id="bookUpdate" parameterType="book">
        UPDATE t_book
<!--        <trim prefix="set" suffixOverrides=",">-->
<!--            <if test="bookName!=null and bookName!=''">bookName=#{bookName},</if>-->
<!--            <if test="price!=null and price!=''">price=#{price},</if>-->
<!--            <if test="publisher!=null and publisher!=''">publisher=#{publisher}</if>-->
<!--            WHERE id=#{id}-->
<!--        </trim>-->
        <set>
            <if test="bookName!=null and bookName!=''">bookName=#{bookName},</if>
            <if test="price!=null and price!=''">price=#{price},</if>
            <if test="publisher!=null and publisher!=''">publisher=#{publisher},</if>
        </set>
        WHERE id=#{id}
    </update>
 
 
</mapper>

<foreach>:

foreach元素通常在构建in条件语句时使用,其使用方式如下。

(1).  item: 表示每个元素迭代时的别名。

(2).  index: 指定一个名称,用于表示在迭代过程中每次迭代的位置

(3).  open: 表示该语句以什么开始(in语句以“( ”开始)

(4).  separator: 表示每次进行迭代时以上面符号作为分隔符(in语句以“ ,”作为分隔符)

(5).  close: 表示该语句以什么结束(in语句以“ )”结束)

(注意:  这个open,separator,close基本上就是固定格式了)

(6).  collection: 表示最关键且最容易出错的属性,需格外注意。该属性必须指定,不同情况下该属性的值是不一样的,主要有三种情况:

        1).  若入参为单参数且参数类型是一个list时,collection属性值为list

        2).  若入参为单参数且参数类型是一个数组时,collection属性值为array

        3).  若入参为多参数,需要将其封装为一个Map进行处理

格式:

<select id="selectPostIn" resultType="domain.blog.Post">
  SELECT *
  FROM POST P
  <where>
    <foreach item="item" index="id" collection="list"
        open="ID in (" separator="," close=")" nullable="true">
          #{item}
    </foreach>
  </where>
</select>

<bind>:

bind元素通常用于需要模糊查询的语句中,使用bind元素定义了一个name为pattern_username的变量,value的属性值就是拼接的查询字符串,其中_parameter.getTitle()表示传递进来的参数(也可以直接写成对应的参数变量名,如username)。

<select id="selectBlogsLike" resultType="Blog">
  <bind name="pattern_username" value="'%' + _parameter.getTitle() + '%'" />
  SELECT * FROM BLOG
  WHERE title LIKE #{pattern}
</select>

到此这篇关于MyBatis 超详细讲解动态SQL的实现的文章就介绍到这了,更多相关MyBatis 动态SQL内容请搜索


打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...