动态SQL

动态SQL

什么是动态SQL:动态SQL就是 指根据不同的条件生成不同的SQL语句

利用动态SQL这一特性可以彻底摆脱这种痛苦。

搭建环境

CREATE TABLE `blog`(
`id` VARCHAR(50) NOT NULL COMMENT '博客id',
`title` VARCHAR(100) NOT NULL COMMENT '博客标题',
`author` VARCHAR(30) NOT NULL COMMENT '博客作者',
`create_time` DATETIME NOT NULL COMMENT '创建时间',
`views` INT(30) NOT NULL COMMENT '浏览量'
)ENGINE=INNODB DEFAULT CHARSET=utf8

创建一个基础工程

  1. 导包

  2. 编写配置文件

  3. 编写实体类

@Data
public class Blog {
   private String id;
   private String title;
   private String author;
   private Date createTime; //属性名和字段名不一致
   private int views;
}

4.编写实体类对应Mapper接口和Mapper.XML文件

IF

<select id="queryBlogIF" parameterType="map" resultType="com.lcbin.pojo.Blog">
       select * from mybatis.blog
       <if test="title!=null">
           and title=#{title}
       </if>
       <if test="author!=null">
           and author =#{author}
       </if>
   </select>

choose (when, otherwise)

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

trim (where, set)

<select id="queryBlogIF" parameterType="map" resultType="com.lcbin.pojo.Blog">
      select * from mybatis.blog
      <where>
          <if test="title!=null">
              and title=#{title}
          </if>
          <if test="author!=null">
              and author =#{author}
          </if>
      </where>
  </select>
<update id="updateBlog" parameterType="map">
       update mybatis.blog
       <set>
           <if test="title != null">
               title = #{title},
           </if>
           <if test="author != null">
               author = #{author}
           </if>
       </set>
       where id = #{id}
   </update>

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

Foreach

  • 动态 SQL 的另一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候)。

  • foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。这个元素也不会错误地添加多余的分隔符,看它多智能!

  • 提示你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象作为集合参数传递给 foreach。当使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。

<!--select * from blog where 1=1 and (id=1 or id=2 or id=3)
   我们现在传递一个万能的map,这map中可以存在一个集合!
   -->
   <select id="queryBlogForeach" parameterType="map" resultType="com.lcbin.pojo.Blog">
       select * from mybatis.blog
       <where>
           <foreach collection="ids" item="id" open="and (" close=")" separator="or">
               id = #{id}
           </foreach>
       </where>
   </select>
@Test
   public void queryBlogForeach(){
       SqlSession sqlSession = MybatisUtils.getSqlSession();
       BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

       HashMap map = new HashMap();
       ArrayList<Integer> ids = new ArrayList<>();//建一个集合
       ids.add(1);
       ids.add(2);
       map.put("ids", ids);//将集合导入map
       List<Blog> blogs = mapper.queryBlogForeach(map);
       for (Blog blog : blogs) {
           System.out.println(blog);
      }
       sqlSession.close();
  }

动态SQL就是在拼接SQL语句,我们只要保证SQL的正确性,按照SQL的格式,去排列组合就可以了。 建议:

  • 先在Mysql中写出完整的SQL,再对应的去修改成我们的动态SQL实现通用即可!

SQL片段

有的时候,我们可以能会将一些功能的部分抽取出来,方便复用!

  1. 使用SQL标签抽取公共的部分

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

2.在需要使用的地方使用Include标签引用即可

<select id="queryBlogIF" parameterType="map" resultType="com.lcbin.pojo.Blog">
       select * from mybatis.blog
       <where>
           <include refid="sql-title-if"></include>
       </where>
</select>

注意事项:

  • 最好基于单表来定义SQL片段!

  • 不要存在where标签

posted @ 2022-03-19 15:33  彬不冰  阅读(62)  评论(0)    收藏  举报