Mybatis 动态 SQL 语句大全

1、if语句
需求:根据作者名字和博客名字来查询博客,如果作者名字为空,那么只根据博客名字查询,反之,则根据作者名来查询:
2、where语句:
3、set语句:
4、choose语句
5、foreach语句
1、新增:
批量新增博客信息到数据库:
mapper接口中这样写:
2、删除:
mapperj接口中这样写:
3、更新
对所有表都适用,公用的写法:
mapper中这样写:
mapper中如果是这样写的:
那xml中就这样写:
4、查询:
批量查询博客信息:
批量查询博客信息:
6、SQL片段
有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。
提取SQL片段:
引用SQL片段:
注意事项:
最好基于 单表来定义Sql 片段,提高片段的可重用性
在 sql 片段中不要包括 Where
7、bind元素:
bind 元素允许你在 OGNL 表达式以外创建一个变量,并将其绑定到当前的上下文。比如:
1、if语句
需求:根据作者名字和博客名字来查询博客,如果作者名字为空,那么只根据博客名字查询,反之,则根据作者名来查

<select id="queryBlogIf" parameterType="map" resultType="blog">
 select * from blog where
 <if test="title != null and title != '' ">
  title = #{title}
 </if>
 <if test="author != null and author != '' ">
  and author = #{author}
 </if>
</select>

  

这样写我们可以看到,如果 author 等于 null,那么查询语句为 select * from user where title=#{title} ,但是如果title为空呢?那么查询语句为 select * from user where and author=#{author} ,这是错误的SQL 语句,如何解决呢?请看下面的 where 语句!

2、where语句:
修改上面的SQL语句:

 

<select id="queryBlogIf" parameterType="map" resultType="blog">
 select * from blog
 <where>
  <if test="title != null and title != ''">
   title = #{title}
  </if>
  <if test="author != null and author != ''">
   and author = #{author}
  </if>
 </where>
</select>

  where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND”或 “OR”,where 元素也会将它们去除
如果 where 元素与你期望的不太一样,你也可以通过自定义 trim 元素来定制 where 元素的功能,如下:

<trim prefix="WHERE" prefixOverrides="AND | OR ">
  ...
</trim>

  

prefixOverrides 属性会忽略通过管道分隔的文本序列**(注意此例中的空格也是必要的)它的作用是移除所有指定在 prefixOverrides 属性中的内容,并且插入 prefix 属性中指定的内容(注意prefixOverrides 单词不能写错,包括大小写,否则会报错)**

3、set语句:
同理,上面的对于查询 SQL 语句包含 where 关键字,如果在进行更新操作的时候,含有 set 关键词,我们怎么处理呢?

<!--注意set是用的逗号隔开-->
<update id="updateBlog" parameterType="map">
 update blog
 <set>
  <if test="title != null and title != '' ">
   title = #{title},
  </if>
  <if test="author != null and author != '' ">
   author = #{author}
  </if>
 </set>
 where id = #{id}
</update>

  这个例子中,set 元素会动态的在行首插入 SET 关键字,并会删掉额外的逗号**(这些逗号是在使用条件语句给列赋值时引入的)**
也是可以和 trim 搭配使用的,如下:

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

  

4、choose语句

有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose标签可以解决此类问题,类似于 Java 的 switch 语句

<select id="queryBlogChoose" parameterType="map" resultType="blog">
 select * from blog
 <where>
  <choose>
   <when test="title != null and title != '' ">
    title = #{title}
   </when>
   <when test="author != null and author != '' ">
    and author = #{author}
   </when>
   <otherwise>
    and views = #{views}
   </otherwise>
  </choose>
 </where>
</select>

  

5、foreach语句

注意:如果需要用SQL进行批量操作的话,数据库连接参数需要设置下面参数:allowMultiQueries=true&rewriteBatchedStatements=true
一般按照下面方式配置即可:

jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&rewriteBatchedStatements=true&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8

  

1、新增:

批量新增博客信息到数据库:

<insert id="batchInsertBlog" useGeneratedKeys="true" keyProperty="id">
    insert into blog (title, author, views, status) values
    <foreach collection="blogList" item="blog" separator=",">
        (#{blog.title}, #{blog.author}, #{blog.views}, #{blog.status})
    </foreach>
</insert>

  

mapper接口中这样写:

int batchInsertBlog(List<Blog> blogList);

  

2、删除:

<delete id="batchDeleteByIds">
    DELETE FROM blog WHERE id in
    <foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
        #{item}
    </foreach>
</delete>

  

mapperj接口中这样写:

int batchDeleteByIds(@Param("ids") List<String> ids);

  

3、更新

对所有表都适用,公用的写法:

<update id="updateTable">
    UPDATE ${tableName}
    SET
    <foreach collection="dataMap" index="key" item="value"  separator="," >
        ${key} = #{value}
    </foreach>
    WHERE
    id = #{id}
</update>

  

mapper中这样写:

 void updateTable(@Param("tableName") String tableName, Param("id") String id, @Param("dataMap") HashMap dataMap);

  

mapper中如果是这样写的:

void updateBatchCourse(@Param("list") List<Course> updateCourseList);

  

那xml中就这样写:

<!--批量更新-->
<update id="updateBatchCourse">
    <foreach collection="list" item="item"  separator=";" >
        update t_course set
        status = #{item.status}, name = #{item.name}
        where id = #{item.id}
    </foreach>
</update>

  

4、查询:

批量查询博客信息:

<select id="getAllBlog"  resultType="blog">
    SELECT title, author, views, status
    FROM blog
    <if test="blogList != null and blogList.size() > 0">
        WHERE id in
        <foreach collection="blogList" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </if>
</select>

  

批量查询博客信息:

 void getAllBlog(@Param("blogList") List<Long> blogList);

  

6、SQL片段

有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。

提取SQL片段:

<sql id="if-title-author">
 <if test="title != null and title != '' ">
  title = #{title}
 </if>
 <if test="author != null and author != '' ">
  and author = #{author}
 </if>
</sql>

  

引用SQL片段:

<select id="queryBlogIf" parameterType="map" resultType="blog">
 select * from blog
 <where>
  <!-- 引用 sql 片段,如果refid 指定的不在本文件中,那么需要在前面加上 namespace-->
  <include refid="if-title-author"></include>
  <!-- 在这里还可以引用其他的 sql 片段 -->
 </where>
</select>

  

注意事项:

  • 最好基于 单表来定义Sql 片段,提高片段的可重用性
  • 在 sql 片段中不要包括 Where

7、bind元素:

bind 元素允许你在 OGNL 表达式以外创建一个变量,并将其绑定到当前的上下文。比如:

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

  

 

原文链接:https://blog.csdn.net/qq_45525848/article/details/127951999

posted @ 2025-06-05 13:53  红尘沙漏  阅读(34)  评论(0)    收藏  举报