package com.kuang.utils;
import org.junit.Test;
import java.util.UUID;
public class IDUtils {
public static String getId(){
return UUID.randomUUID().toString().replaceAll("-","");
}
@Test
public void test(){
System.out.println(IDUtils.getId());
System.out.println(IDUtils.getId());
System.out.println(IDUtils.getId());
}
}
BlogMapper:
package com.kuang.dao;
import com.kuang.pojo.Blog;
import java.util.List;
import java.util.Map;
public interface BlogMapper {
//插入数据
int addBlog(Blog blog);
//查询博客
List queryBlogIF(Map map);
}
BlogMapper.xml:动态sql if标签
insert into mybatis.blog (id, title, author, create_time, views)
values (#{id},#{title},#{author},#{createTime},#{views});
MyTest:
import com.kuang.dao.BlogMapper;
import com.kuang.pojo.Blog;
import com.kuang.utils.IDUtils;
import com.kuang.utils.MybaitsUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
public class MyTest {
@Test
public void addBlogTest(){
SqlSession sqlSession = MybaitsUtils.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.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("微服务如此简单");
mapper.addBlog(blog);
sqlSession.close();
}
@Test
public void queryBlogIF(){
SqlSession sqlSession=MybaitsUtils.getSqlSession();
BlogMapper mapper=sqlSession.getMapper(BlogMapper.class);
HashMap map=new HashMap();
// map.put("author","狂神说");
map.put("title","Java如此简单");
List blogs = mapper.queryBlogIF(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
}
运行addTestBlogTest:往表中插入数据
queryBlogIF:
切换map注释:
BlogMapper:
package com.kuang.dao;
import com.kuang.pojo.Blog;
import java.util.List;
import java.util.Map;
public interface BlogMapper {
//插入数据
int addBlog(Blog blog);
//查询博客
List queryBlogIF(Map map);
//
List queryBlogChoose(Map map);
//更新博客
int UpdateBlog(Map map);
//查询第1-3号记录的博客
List queryBlogForeach(Map map);
}
BlogMapper.xml:动态sql常用标签
insert into mybatis.blog (id, title, author, create_time, views)
values (#{id},#{title},#{author},#{createTime},#{views});
MyTest:
import com.kuang.dao.BlogMapper;
import com.kuang.pojo.Blog;
import com.kuang.utils.IDUtils;
import com.kuang.utils.MybaitsUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
public class MyTest {
//插入数据
@Test
public void addBlogTest(){
SqlSession sqlSession = MybaitsUtils.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.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("微服务如此简单");
mapper.addBlog(blog);
sqlSession.close();
}
//where标签查询
@Test
public void queryBlogIF(){
SqlSession sqlSession=MybaitsUtils.getSqlSession();
BlogMapper mapper=sqlSession.getMapper(BlogMapper.class);
HashMap map=new HashMap();
map.put("author","狂神说");
//map.put("title","Java如此简单");
List blogs = mapper.queryBlogIF(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
//Choose查询,相当于switch它会找满足条件的一个
@Test
public void queryBlogChoose(){
SqlSession sqlSession=MybaitsUtils.getSqlSession();
BlogMapper mapper=sqlSession.getMapper(BlogMapper.class);
HashMap map=new HashMap();
map.put("title","Java如此简单");
map.put("author","狂神说");
map.put("views",9999);
List blogs = mapper.queryBlogChoose(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
//Set更新
@Test
public void UpdateBlog(){
SqlSession sqlSession=MybaitsUtils.getSqlSession();
BlogMapper mapper=sqlSession.getMapper(BlogMapper.class);
HashMap map=new HashMap();
map.put("author","狂神说1");
map.put("title","Java如此简单");
map.put("id","311ace05e43747b49db2cc2ff61b687e");
int i = mapper.UpdateBlog(map);
if (i>0){
System.out.println("更新成功");
}
sqlSession.close();
}
//查询第1-3号记录的博客
@Test
public void queryBlogForeach(){
SqlSession sqlSession=MybaitsUtils.getSqlSession();
BlogMapper mapper=sqlSession.getMapper(BlogMapper.class);
HashMap map=new HashMap();
ArrayList ids=new ArrayList();
ids.add(1);
ids.add(2);
ids.add(3);
map.put("ids",ids);
List blogs = mapper.queryBlogForeach(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
}
queryBlogIF
queryBlogChoose:
choose相当于switch只会在查找满足条件的一个,如果把注释的内容打开,只会第一个条件生效
UpdateBlog:
BlogMapper.xml:
insert into mybatis.blog (id, title, author, create_time, views)
values (#{id},#{title},#{author},#{createTime},#{views});
title =#{title}
and author =#{author}
select * from mybatis.blog
select * from mybatis.blog
title =#{title}
and author =#{author}
and views=#{views}
update mybatis.blog
title=#{title},
author=#{author}
where id=#{id}
select * from mybatis.blog
id=#{id}