Mybatis 动态sql
动态sql可以简单理解为根据不同的条件去生成不同的sql语句。
搭建环境
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的date和UUID插入数据
代码就不放了

动态sql语句之if

实现通过一个sql语句,在没有标题作者参数时就查询所有的信息。如果有标题作者参数就查询符合条件的信息
定义接口和mapper文件
package dao;
import pojo.Blog;
import java.util.List;
import java.util.Map;
public interface BlogMapper {
int addBlog(Blog blog);
List<Blog> getBlogIF(Map map);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace对应接口名字-->
<mapper namespace="dao.BlogMapper">
<insert id="addBlog" parameterType="Blog">
insert into mybatis.blog(id,title,author,create_time,views)
values (#{id},#{title},#{author},#{createTime},#{views});
</insert>
<resultMap id="BlogMap" type="Blog">
<result property="createTime" column="create_time"/>
</resultMap>
<select id="getBlogIF" parameterType="map" resultMap="BlogMap">
<!--通过where 1=1 让后面拼接更好写 当然建议使用下面说的where标签-->
select * from blog where 1=1
<if test="title!=null">
and title=#{title}
</if>
<if test="author!=null">
and author=#{author}
</if>
</select>
</mapper>
没有参数测试:

带作者,标题参数测试


功能实现成功!
where标签

通过where标签可以去掉上面的1=1
代码修改为
<select id="getBlogIF" parameterType="map" resultMap="BlogMap">
select * from blog
<where>
<if test="title!=null">
and title=#{title}
</if>
<if test="author!=null">
and author=#{author}
</if>
</where>
</select>
choose、when、otherwise
其实就是if else if的功能实现
官方文档说明

set
更新时的set语句逗号问题很麻烦,通过set标签解决
实现根据参数动态更新信息
定义接口 mapper文件
package dao;
import pojo.Blog;
import java.util.List;
import java.util.Map;
public interface BlogMapper {
int addBlog(Blog blog);
List<Blog> getBlogIF(Map map);
int updateBlog(Map map);
}
<update id="updateBlog" parameterType="map">
update blog
<!--通过set标签就可以直接全部加逗号 mybatis会帮你解决问题的 建议全部加逗号-->
<set>
<if test="title!=null">title=#{title},</if>
<if test="author!=null">author=#{author},</if>
</set>
where id=#{id}
</update>
主程序
import dao.BlogMapper;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.Blog;
import utiles.IDutiles;
import utiles.MybatisUtiles;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
public class Mytest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtiles.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map=new HashMap();
map.put("id","78e05a670d8e4be1a094d155623b8e25");
map.put("title","被修改过的博客1");
map.put("author","杰");
mapper.updateBlog(map);
sqlSession.commit();
sqlSession.close();
}
}

成功修改。
sql标签
有代码段
<select id="getBlogIF" parameterType="map" resultMap="BlogMap">
select * from blog
<where>
<if test="title!=null">
and title=#{title}
</if>
<if test="author!=null">
and author=#{author}
</if>
</where>
</select>
假设其中if的判断语段在后续很经常使用,可以使用Sql标签包装起来使用(大概就和函数一个功能)
<sql id="if-title-author">
<if test="title!=null">
and title=#{title}
</if>
<if test="author!=null">
and author=#{author}
</if>
</sql>
<select id="getBlogIF" parameterType="map" resultMap="BlogMap">
select * from blog
<where>
<include refid="if-title-author"></include>
</where>
</select>
foreach

假设我们要查询博客2到4的信息
有select语句
select *
from blog
where title in ('博客2','博客3','博客4')
通过foreach来实现循环生成语句
定义接口mapper文件
package dao;
import pojo.Blog;
import java.util.List;
import java.util.Map;
public interface BlogMapper {
int addBlog(Blog blog);
List<Blog> getBlogIF(Map map);
int updateBlog(Map map);
List<Blog> queryBlogForeach(Map map);
}
<select id="queryBlogForeach" parameterType="map" resultMap="BlogMap">
select * from blog
where title in
<foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
<!--open close 对应开头和结尾 separator为每一项的分割 item为list中的元素 index为当前下标-->
<!--假设list里有博客2博客3博客4 表达式就分割为(博客2,博客3,博客4)-->
</select>
主程序
import dao.BlogMapper;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.Blog;
import utiles.IDutiles;
import utiles.MybatisUtiles;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
public class Mytest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtiles.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map=new HashMap();
ArrayList<String> list=new ArrayList<String>();
list.add("博客2");
list.add("博客3");
list.add("博客4");
map.put("list",list);
List<Blog> blogs = mapper.queryBlogForeach(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.commit();
sqlSession.close();
}
}


浙公网安备 33010602011771号