官网描述:
MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。
虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形。
动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。
<setting name="mapUnderscoreToCamelCase" value="true"/>
@Data
public class Blog {
private String id;
private String title;
private String author;
private Date createTime;
private int views;
}
public interface BlogMapper {
//插入数据
int addBlog(Blog blog);
}
<insert id="addBlog" parameterType="blog">
insert into blog
(id,title,author,create_time,views)
value
(#{id},#{title},#{author},#{createTime},#{views})
insert>
@Test
public void testes01(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
Blog blog = new Blog();
blog.setId(IDUtils.getId());
blog.setTitle("Mybatis-01");
blog.setAuthor("xx");
blog.setCreateTime(new Date());
blog.setViews(999);
mapper.addBlog(blog);
blog.setId(IDUtils.getId());
blog.setTitle("Mybatis-02");
blog.setAuthor("xx");
blog.setCreateTime(new Date());
blog.setViews(888);
mapper.addBlog(blog);
blog.setId(IDUtils.getId());
blog.setTitle("Mybatis-03");
blog.setAuthor("zz");
blog.setCreateTime(new Date());
blog.setViews(777);
mapper.addBlog(blog);
sqlSession.close();
}
public interface BlogMapper {
List<Blog> queryBlogIf(Map map);
}
<select id="queryBlogIf" parameterType="map" resultType="blog">
select * from blog where state = ‘ACTIVE’
<if test="title != null ">
title = #{title}
if>
<if test="author != null">
and author = #{author}
if>
select>
@Test
public void test02(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map = new HashMap();
List<Blog> blogs = mapper.queryBlogIf(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
<select id="queryBlogIf" parameterType="map" resultType="blog">
select * from blog
<where>
<if test="title != null ">
title = #{title}
if>
<if test="author != null">
and author = #{author}
if>
where>
select>
<select id="queryBlogChoose" resultType="blog" parameterType="map">
select * from 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>
<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>
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
trim>
<trim prefix="SET" suffixOverrides=",">
...
trim>
所谓的动态SQL,本质还是SQL语句,只是我们可以在SQL层面,去执行一个逻辑代码
<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 blog
<where>
<include refid="if-title-author">include>
where>
select>
List<Blog> queryBlogForeach(Map map);
<select id="queryBlogForeach" resultType="blog" parameterType="map">
select * from blog
<where>
<foreach collection="ids" item="id" open=" (" close=")" separator="or">
id = #{id}
foreach>
where>
select>
@Test
public void test05(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map = new HashMap();
ArrayList<Integer> integers = new ArrayList<>();
integers.add(1);
integers.add(2);
map.put("ids",integers);
List<Blog> blogs = mapper.queryBlogForeach(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
动态SQL就是在拼接SQL语句,我们只要保证SQL的格式,去排列组合就可以了
建议: