用where标签替代where 1=1,防止出错
db.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=&useUnicode=true&characterEncoding=utf-8
username=root
password=123456
mybatis-config.xml:
Blog:
package com.kuang.pojo;
import lombok.Data;
import java.util.Date;
@Data
public class Blog {
private String id;
private String title;
private String author;
private Date createTime;//属性名和字段名不一致
private int views;
}
MybatisUtils:
package com.kuang.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
//sqlSessionFactory --sqlSession
public class MybaitsUtils {
private static SqlSessionFactory sqlSessionFactory;//提升作用域
/*
所谓的代码块就是一段独立的代码空间,那什么是静态代码块呢?说白了,静态代码块就是用static修饰的代码块,关于静态代码块的概念不多说,不过你需要知道它的一个特点,那就是
随着类的加载而执行,而且只执行一次
*/
static{
try {
//使用MyBaits获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}catch(IOException e){
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
public static SqlSession getSqlSession(){
//获取SqlSession对象
SqlSession sqlSession=sqlSessionFactory.openSession(true);//true设置事务自动提交,执行增删改,sql不用手动提交
return sqlSession;
}
}
IDUtils:生成id
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});
update mybatis.blog
title=#{title},
author=#{author}
where id=#{id}
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}
update mybatis.blog
title=#{title},
author=#{author}
where id=#{id}
queryBlogForeach:
当不忘List集合中添加id
/*ids.add(1); ids.add(2); ids.add(3);*/
去掉注释: