动态SQL
根据不同的条件生成不同的SQL语句
- if
- choose(when , otherwise)
- trim(where , set)
- foreach
环境搭建
偷懒包
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
- 编写配置文件(用之前的mabatis-config.xml 加这三条即可)
<setting name="mapUnderscoreToCamelCase" value="true"/>
<typeAlias type="net.cqwu.pojo.Blog" alias="blog"/>
<mapper class="net.cqwu.dao.BlogMapper"/>
import lombok.Data;
import java.util.Date;
@Data
public class Blog {
private int id;
private String title;
private String autor;
private Date dataTime;
private int views;
}
- 编写实体类对应的Mapper接口和Mapper.xml文件
<insert id="addBlog" parameterType="blog">
insert into mybatis.blog (id, title, author, create_time, views)
values (#{id}, #{title}, #{author}, #{createTime}, #{views});
</insert>
@Test
public void addBlog() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
Blog blog = new Blog();
blog.setId(IDutils.getId());
blog.setTitle("Mybatis");
blog.setAuthor("Feliks");
blog.setCreateTime(new Date());
blog.setViews(9999);
mapper.addBlog(blog);
blog.setId(IDutils.getId());
blog.setTitle("Java");
mapper.addBlog(blog);
blog.setId(IDutils.getId());
blog.setTitle("Spring");
mapper.addBlog(blog);
blog.setId(IDutils.getId());
blog.setTitle("SpringMVC");
mapper.addBlog(blog);
sqlSession.commit();
sqlSession.close();
}
IF
接口
List<Blog> queryBlogIF(Map map);
xml
<select id="queryBlogIF" parameterType="map" resultType="blog">
select * from mybatis.blog where 1=1
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</select>
Test
@Test
public void queryBlogIF(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map = new HashMap();
map.put("title","MyBatis");
map.put("author","Feliks");
List<Blog> blogs = mapper.queryBlogIF(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
success
[net.cqwu.dao.BlogMapper.queryBlogIF]-==> Preparing: select * from mybatis.blog where 1=1 and title = ? and author = ?
[net.cqwu.dao.BlogMapper.queryBlogIF]-==> Parameters: MyBatis(String), Feliks(String)
[net.cqwu.dao.BlogMapper.queryBlogIF]-<== Total: 1
Blog(id=ccbf414acc3a44d9b26cd368dee7b108, title=Mybatis, author=Feliks, createTime=Sun Dec 05 21:35:37 CST 2021, views=9999)
choose(when , otherwise)
List<Blog> queryBlogChoose(Map map);
<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>
@Test
public void queryBlogChoose(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map = new HashMap();
map.put("title","java针不戳");
map.put("author","Feliks陈");
map.put("views","9999");
List<Blog> blogs = mapper.queryBlogChoose(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
[net.cqwu.dao.BlogMapper.queryBlogChoose]-==> Preparing: select * from mybatis.blog WHERE title = ?
[net.cqwu.dao.BlogMapper.queryBlogChoose]-==> Parameters: java针不戳(String)
[net.cqwu.dao.BlogMapper.queryBlogChoose]-<== Total: 1
Blog(id=2a2da62e7d784489b644ca8d35a9564e, title=Java针不戳, author=Feliks陈, createTime=Sun Dec 05 21:35:37 CST 2021, views=10000)
trim(where , set)
<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>
@Test
public void updateBlog(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map = new HashMap();
map.put("author","slugger");
map.put("id","ccbf414acc3a44d9b26cd368dee7b108");
mapper.updateBlog(map);
sqlSession.commit();
sqlSession.close();
}
SQL片段
将一些功能的部分抽取出来,方便使用
- 使用sql标签抽取公共部分
<sql id="if-title-author">
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>
- 在需要使用的对方使用include标签引用
<select id="queryBlogIF" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<include refid="if-title-author"></include>
</where>
</select>
注意:
- 最好基于单表来定义SQL片段
- 不要存在where标签
Foreach
List<Blog> queryBlogForeach(Map map);
<select id="queryBlogForeach" parameterType="map" resultType="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<Integer>();
ids.add(1);
ids.add(2);
ids.add(3);
map.put("ids",ids);
List<Blog> blogs = mapper.queryBlogForeach(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
动态SQL就是在拼接SQL语句,我们只要保证SQL的正确性,按照SQL的格式,去排列组合即可
|