前言
推荐 B站 宝藏男孩 遇见狂神说
动态SQL
- 实体类Blog
public class Blog {
private String id;
private String title;
private String author;
// createTime属性和数据库字段不一致,在配置文件中设置开启驼峰命名自动映射
private Date createTime;
private int views;
}
- mybatis-config.xml主配置文件中加入设置
<settings>
<!-- 是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
- 数据库t_blog表
CREATE TABLE `t_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
INSERT INTO `t_blog` VALUES ('1', 'java的诞生', '狂神说', '2020-10-11 02:45:01', '4');
INSERT INTO `t_blog` VALUES ('2', 'mybatis和Spring的整合', '狂神说', '2020-10-11 02:47:15', '23');
INSERT INTO `t_blog` VALUES ('3', 'redis的底层实现原理', '狂神说', '2020-10-11 07:03:53', '9999');
INSERT INTO `t_blog` VALUES ('4', '453543', '54353', '2020-06-11 22:16:44', '64654');
准备工作完成,可以写SQL了~
1.BlogMapper接口
public interface BlogMapper {
// IF标签动态查询
List<Blog> selectBlogIF(Map map);
// choose标签查询
List<Blog> selectBlogChoose(Map map);
// set标签
int updateBlogSet(Map map);
// foreach标签查询
List<Blog> selectBlogForeach(Map map);
}
- if标签
<!-- if标签-->
<select id="selectBlogIF" parameterType="map" resultType="com.qst.pojo.Blog">
select id,title,author,create_time,views
from t_blog
<where>
<if test="title!=null">
and title=#{title}
</if>
<if test="author!=null">
and author=#{author}
</if>
</where>
</select>
测试
@Test
public void selectBlogIf(){
SqlSession session = MybatisUtil.getSqlSession();
BlogMapper mapper = session.getMapper(BlogMapper.class);
Map map = new HashMap();
map.put("title","mybatis的配置实现");
List<Blog> blogs = mapper.selectBlogIF(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
session.close();
}
- choose标签
<!-- choose标签-->
<select id="selectBlogChoose" parameterType="map" resultType="com.qst.pojo.Blog">
select id,title,author,create_time,views
from t_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 selectBlogChoose(){
SqlSession session = MybatisUtil.getSqlSession();
BlogMapper mapper = session.getMapper(BlogMapper.class);
Map map = new HashMap();
//map.put("title","mybatis的配置实现");
map.put("author","狂神说");
List<Blog> blogs = mapper.selectBlogChoose(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
session.close();
}
- 这里说一个sql标签,配合使用
sql片段
1.使用sql标签抽取公共部分
2.在需要使用的地方用include标签引入
注意:
1.最好基于单表查询
2.不要存在where标签
- update标签
- trim标签
有4个属性
prefix 前缀
prefixOverrides 前缀覆盖
suffix 后缀
suffixOverrides 后缀覆盖
前后缀这个很好理解,但是前缀覆盖,用以下例子说明:
prefix=“set” suffixOverrides=","
代码执行到trim时加一个set,如果在tSQL语句中多了一个逗号,trim标签会将其去掉
update t_blog
-- 这里末尾的逗号会去掉
set title=#{title},author=#{author},
where id=#{id}
<!-- sql片段,抽取公共部分 -->
<sql id="if-title-author">
<if test="title!=null">
title=#{title},
</if>
<if test="author!=null">
author=#{author},
</if>
</sql>
<!-- set标签-->
<update id="updateBlogSet" parameterType="map">
update t_blog
<trim prefix="set" suffixOverrides=",">
<!-- 引入sql片段-->
<include refid="if-title-author"/>
</trim>
where id=#{id}
</update>
测试
@Test
public void updateBlogSet(){
SqlSession session = MybatisUtil.getSqlSession();
BlogMapper mapper = session.getMapper(BlogMapper.class);
Map map = new HashMap();
//map.put("title","mybatis和Spring的整合");
map.put("author","狂神说");
// map.put("createTime",new Date());
map.put("id","fada1014abd54be590cf476d1a0a0116");
mapper.updateBlogSet(map);
session.commit();
session.close();
}
- foreach标签
<select id="selectBlogForeach" parameterType="map" resultType="com.qst.pojo.Blog">
select id,title,author,create_time,views
from t_blog
<where>
这里的collection是集合,item是变量 ,open是集合的开始
close是结尾,separator是分隔符
它拼凑的SQL : select * from t_blog where (id=1 or id=2);
<foreach collection="ids" item="id" open="(" close=")" separator="or">
id=#{id}
</foreach>
</where>
</select>
测试
@Test
public void selectBlogForeach(){
SqlSession session = MybatisUtil.getSqlSession();
BlogMapper mapper = session.getMapper(BlogMapper.class);
HashMap map = new HashMap();
ArrayList<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
传值给foreach中的collection属性,名字要一致
map.put("ids",ids);
List<Blog> blogs = mapper.selectBlogForeach(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
session.close();
}
小结
1.动态SQL和JSTL(标准标签语言)用法相似,还是很简单的
2.trim、foreach标签需要拼凑一些属性,需要注意
共同成长,共同进步~