前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis:动态sql

Mybatis:动态sql

作者头像
冷环渊
发布2021-10-19 15:31:39
6190
发布2021-10-19 15:31:39
举报

mybatis

每日格言

无所畏惧,坚持到底,决不放弃。干兄弟们

动态sql

什么是动态sql,动态sql就是根据不同的条件产生不同的sql语句

动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

搭建环境

代码语言:javascript
复制
CREATE TABLE `mybatis`.`blog`  (
  `id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '博客id',
  `title` VARCHAR(30) NOT NULL COMMENT '博客标题',
  `author` VARCHAR(30) NOT NULL COMMENT '博客作者',
  `create_time` DATETIME NOT NULL COMMENT '创建时间',
  `views` INT(30) NOT NULL COMMENT '浏览量',
  PRIMARY KEY (`id`)
)

创建一个基础工程

if

代码语言:javascript
复制
        select * from blog where 1=1
         
            and title = #{title};
         
         
             and  author = #{author};

choose

代码语言:javascript
复制
    <select id="queryBlogChoose" resultType="blog" parameterType="map">

        select * from blog
      <where>
          <choose>
              <when test="title != null">
                  and title = #{title}
              </when>
              <when test="author != null">
                  and  author = #{author}
              </when>
        <otherwise>
            and  views = #{views}
        </otherwise>
          </choose>
      </where>

    </select>

trim(where,set)

代码语言:javascript
复制
        select * from blog
        
            
                and title = #{title};
            
            
                and  author = #{author};
代码语言:javascript
复制
    <update id="UpdateBlog" parameterType="map" >
        update blog
        <set>
            <if test="title != null">
             title = #{title},
             if>
             <if test="author != null">
                 author = #{author}
             if>
        set>
        where id = #{id}
    update>

所谓的动态sqL,本质上就是sql语句,只是我们可以在sql层面,去执行一个逻辑代码

sql片段

有的时候,我们可能会将一些公共的部分抽取出来,方便使用

通过include标签的refid属性来调用sql片段

代码语言:javascript
复制
    <select id="queryBlogif" parameterType="map" resultType="blog">
        select * from blog
        <where>
         <include refid="if-title-author">include>
        where>
    select>

设置id来让sql片段可以被调用,

代码语言:javascript
复制
    <sql id="if-title-author">
        <if test="title != null">
            and title = #{title};
        if>
        <if test="author != null">
            and  author = #{author};
        if>
    sql>

注意事项:

  • 最好基于单表来定义sql片段
  • 最好不在sql片段存在where

Foreach

代码语言:javascript
复制
select * from User where 1=1 and (id = 1 or id=2 or id=3)

<select id="selectPostIn" resultType="domain.blog.Post">
  SELECT *
  FROM POST P
  WHERE ID in
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
  foreach>
select>
代码语言:javascript
复制
    <select id="queryBlogForeach" resultType="blog" parameterType="map">
        select  * from blog
        <where>
            <foreach collection="ids" item="id" open="and (" close=")" separator="or">
                id=#{id}
            foreach>
        where>
    select>

建议:

  • 现在mysql写出完整的sql,再对应的修改我们的动态sql再使用,
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-08-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • mybatis
    • 每日格言
      • 动态sql
        • 搭建环境
        • if
        • choose
        • trim(where,set)
        • sql片段
        • Foreach
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档