当前位置:Gxlcms > 数据库问题 > MyBatis——动态SQL

MyBatis——动态SQL

时间:2021-07-01 10:21:17 帮助过:18人阅读

耳中所听恍若你呢喃,心之所向是指你为南,目之所及除你之外尽是荒野。

动态SQL

什么是动态SQL:

? 动态SQL就是根据不同的条件生成不同的SQL语句

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

1、搭建环境

建表

  1. <code>CREATE TABLE `bolg`(
  2. `id` VARCHAR(50) NOT NULL COMMENT '博客id',
  3. `title` VARCHAR(100) not null comment '博客标题',
  4. `author` VARCHAR(30) not null comment '博客作者',
  5. `creat_time` datetime not null comment '创建时间',
  6. `views` int(30) not null comment '浏览量'
  7. )ENGINE=InnoDB DEFAULT CHARSET=utf8</code>

创建一个基础工程

  1. 导包

  2. 编写配置文件

  3. 编写实体类

    1. <code>@Data
    2. public class Blog {
    3. private int id;
    4. private String title;
    5. private String author;
    6. private Date creatTime;
    7. private int views;
    8. }</code>
  4. 编写实体类对应的Mapper接口和Mapper.xm

2、IF

  1. <code><select id="queryBlogIF" parameterType="map" resultType="com.rui.pojo.Blog">
  2. select * from mybatis.bolg where 1=1
  3. <if test="title != null">
  4. and title = #{title}
  5. </if>
  6. <if test="author != null">
  7. and author = #{author}
  8. </if>
  9. </select></code>
  1. <code>@Test
  2. public void queryBlogIF(){
  3. SqlSession sqlSession = MyBatisUtils.getSqlSession();
  4. BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
  5. HashMap map = new HashMap();
  6. map.put("author","尹锐");
  7. List<Blog> blogs = mapper.queryBlogIF(map);
  8. for (Blog blog : blogs) {
  9. System.out.println(blog);
  10. }
  11. sqlSession.close();
  12. }</code>

3、choose(when,otherwise)

  1. <code><select id="queryBlogChoose" parameterType="map" resultType="com.rui.pojo.Blog">
  2. select * from mybatis.bolg
  3. <where>
  4. <choose>
  5. <when test="title != null">
  6. title=#{title}
  7. </when>
  8. <when test="author!=null">
  9. and author = #{author}
  10. </when>
  11. <otherwise>
  12. and views = #{views}
  13. </otherwise>
  14. </choose>
  15. </where>
  16. </select></code>

4、trim(where,set)

  1. <code>select * from mybatis.bolg
  2. <where>
  3. <if test="title != null">
  4. title = #{title}
  5. </if>
  6. <if test="author != null">
  7. and author = #{author}
  8. </if>
  9. </where></code>
  1. <code><update id="updateBlog" parameterType="map">
  2. update mybatis.bolg
  3. <set>
  4. <if test="title != null">
  5. title = #{title},
  6. </if>
  7. <if test="author != null">
  8. author = #{author},
  9. </if>
  10. </set>
  11. where id = #{id}
  12. </update></code>

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

5、Foreach

  1. <code>select * from user where 1=1 and
  2. <foreach item="id" index="index" collection="ids"
  3. open="(" separator="or" close=")">
  4. #{id}
  5. </foreach>
  6. (id=1 or id=2 or id=3)</code>

技术图片

  1. <code><!--
  2. select * from mybatis.bolg where 1=1 and (id=1 or id=2 or id=3)
  3. 我们现在传递一个万能的map,这个map中可以存在一个map
  4. -->
  5. <select id="queryBlogForeach" parameterType="map" resultType="com.rui.pojo.Blog">
  6. select * from mybatis.bolg
  7. <where>
  8. <foreach collection="ids" item="id" open="(" close=")" separator="or">
  9. id = #{id}
  10. </foreach>
  11. </where>
  12. </select></code>

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

建议:

  • 先在MySQL中写出完整的SQL,再对应的去修改成为我们的动态SQL

SQL片段

有的时候,我们可能会将一些公共的部分抽取处理,方便复用

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

    1. <code><sql id="if-title-author">
    2. <if test="title != null">
    3. title = #{title}
    4. </if>
    5. <if test="author != null">
    6. and author = #{author}
    7. </if>
    8. </sql></code>
  2. 在需要使用的地方使用Include标签引用即可

    1. <code><select id="queryBlogIF" parameterType="map" resultType="com.rui.pojo.Blog">
    2. select * from mybatis.bolg
    3. <where>
    4. <include refid="if-title-author"></include>
    5. </where>
    6. </select></code>

    注意事项:

    • 最好基于单表来定义SQL片段
    • 不要存在where或者set标签,片段里尽量只有if就好了

MyBatis——动态SQL

标签:ash   sep   hash   ati   基于   时间   ids   语句   配置   

人气教程排行