动态sql环境搭建和数据准备
工具类,获取UUID
package com.shan.utils;
import org.junit.Test;
import java.util.UUID;
public class IDUtils {
public static String getId(){
return UUID.randomUUID().toString().replace("-","");
}
@Test
public void idTest(){
System.out.println(IDUtils.getId());
System.out.println(IDUtils.getId());
System.out.println(IDUtils.getId());
}
}
创建一个博客表
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
以Java的方面给数据库插入数据
public void addBlogTest(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
Blog blog = new Blog();
blog.setId(IDutils.getId());
blog.setTitle("Mybatis如此简单!");
blog.setAuthor("小黑神");
blog.setCreateTime(new Date());
blog.setViews(9999);
mapper.addBook(blog);
blog.setId(IDutils.getId());
blog.setTitle("Java如此简单!");
mapper.addBook(blog);
blog.setId(IDutils.getId());
blog.setTitle("Spring如此简单!");
mapper.addBook(blog);
blog.setId(IDutils.getId());
blog.setTitle("微服务如此简单!");
mapper.addBook(blog);
sqlSession.close();
实体类
package com.shan.pojo;
import lombok.Data;
import java.util.Date;
@Data
public class Blog {
private String id;
private String title;
private String author;
private Date createTime;
private int views;
}
一、动态sql是什么?
动态sql可以这样理解:就是根据不同的条件生成不同的sql语句!
二、动态sql的使用
1.动态sql之if语句
<!--动态sql语句之If:满足什么条件就用那些条件-->
<!--sql片段:使用sql标签抽取功能重复的代码,方便复用,在需要使用的地方使用include标签引用即可-->
<sql id="if-title-author">
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>
<select id="queryBlogIf" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<include refid="if-title-author"></include>
</where>
</select>
2.动态sql之choose语句
<!--动态sql语句之Choose:当这个条件满足时就用这个条件-->
<select id="queryBlogChoose" parameterType="map" resultType="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>
3.动态sql之set语句
<!--动态sql语句之Set-->
<update id="updateBlog" parameterType="map">
update mybatis.blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="author != null">
author = #{author},
</if>
<if test="views != null">
views = #{views}
</if>
</set>
where id = #{id}
</update>
4.动态sql之Foreach语句
<!--动态sql语句之Foreach-->
<!--我想查询2,3,5,6号记录的博客
select * from mybatis.blog where (id=2 or id=3 or id=5 or id=6)
我们可以传入一个map,这个map里面可以是一个集合
-->
<select id="queryBlogForeach" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<foreach collection="ids" item="id" open="(" close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>
总结
动态sql的出现,打破以往原生态的sql拼接,让sql语句更加简单!
动态sql本质就是在拼接sql语句,我们只要保证sql的正确性,按照sql的格式,去排列组合即可!
作者有话说
博客创作不易,希望看到这里的读者动动你的小手点个赞,如果喜欢的小伙伴可以一键三连,作者大大在这里给大家谢谢了。