目录
1、分页插件
2、mybatis的代码生成器--generator
3、mybatis的缓存
3.1 什么是缓存
3.2 缓存的好处
3.3 什么样的数据适合放入缓存
3.4 什么样的数据不适合放入缓存
3.5 mybatis它也支持缓存
二级缓存测试:
二级缓存使用两种方式
1、分页插件
作用: 帮你完成分页的功能?
思考: 分页的sql语句。
select * from 表名 [where 条件] limit (page-1)*pageSize, pageSize; page: 当前页码 pageSize: 每页显示的条数。
如何使用:
(1)引入pageHelper的jar包
com.github.pagehelper
pagehelper
5.1.11
(2)mybatis中设置pageHelper的拦截器
(3)使用pageHelper
//设置分页属性pageNum:显示第几页 PageSize:每页显示的条数
PageHelper.startPage(2,5);
List list = userDao.findAll();
//把查询的结果封装到PageInfo类中。
PageInfo pageInfo=new PageInfo(list);
System.out.println("总条数:"+pageInfo.getTotal());
System.out.println("总页数:"+pageInfo.getPages());
System.out.println("当前页码对应的数据:"+pageInfo.getList());
原理
2、mybatis的代码生成器--generator
作用: 根据表帮你生成实体类,和dao和xml映射文件。就是简单的CRUD。
如何使用:
MyBatis Generator Core – Introduction to MyBatis Generator
(1)引入mybatis-generator的依赖jar包。
org.mybatis.generator mybatis-generator-core1.4.0
(2)generator的配置文件
(3)运行你的配置文件
Listwarnings = new ArrayList (); boolean overwrite = true; File configFile = new File("config.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null);
3、mybatis的缓存
3.1 什么是缓存
缓存是存在于==内存中==的临时数据。
3.2 缓存的好处
缓存是存在于==内存中==的临时数据。
3.2 缓存的好处
使用缓存减少和数据库的交互次数,提高执行效率。
3.3 什么样的数据适合放入缓存
经常查询并且不经常改变的; 数据的正确与否对最终结果影响不大的;
3.4 什么样的数据不适合放入缓存
经常改变的数据; 数据的正确与否对最终结果影响很大的;---数据安全性要求不高。 例如:商品的库存,银行的汇率,股市的牌价;
3.5 mybatis它也支持缓存
经常查询并且不经常改变的; 数据的正确与否对最终结果影响不大的;
经常改变的数据; 数据的正确与否对最终结果影响很大的;---数据安全性要求不高。 例如:商品的库存,银行的汇率,股市的牌价;
3.5 mybatis它也支持缓存
mybatis支持两种缓存
(1)一级缓存----基于SqlSession级别的缓存。默认一级缓存是开启的,不能关闭。
(2)二级缓存--基于SqlSessionFactory级别的缓存,它可以做到多个SqlSession共享数据。默认它是关闭。需要手动开启。
一级缓存测试:同一个sqlsession执行了两次相同的sql,从控制台的日志看出 sql只执行了一遍,且两个结果的地址值相同。
使用两个不同的sqlsession时,一级缓存失效,执行了两条sql语句
@Test
public void demo02() throws Exception{
Reader reader = Resources.getResourceAsReader("mybatis.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = factory.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectByPrimaryKey(1);
System.out.println(student);
sqlSession.close();
SqlSession sqlSession1 = factory.openSession();
StudentMapper mapper1 = sqlSession1.getMapper(StudentMapper.class);
Student student1 = mapper1.selectByPrimaryKey(1);
System.out.println(student1);
}
二级缓存测试:
(1)开启 二级缓存
(2)在映射文件中使用二级缓存
(3)实体一定要实现序列化接口
(4)测试二级缓存 (使用两个sqlsession时,sql只会执行一遍,二级缓存测试成功)
@Test
public void demo02() throws Exception{
Reader reader = Resources.getResourceAsReader("mybatis.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = factory.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectByPrimaryKey(1);
System.out.println(student);
sqlSession.close();
SqlSession sqlSession1 = factory.openSession();
StudentMapper mapper1 = sqlSession1.getMapper(StudentMapper.class);
Student student1 = mapper1.selectByPrimaryKey(1);
System.out.println(student1);
}
二级缓存使用两种方式
1、当使用
2、在sql语句中使用,设置userCache=true
useCache默认为true,表示会将本条语句的结果进行二级缓存。



