缓存:原理图
查询语句在执行的时候会先去内存中去查找有没有该语句的缓存,有就直接拿来结果,没有再去硬盘(数据库)中去查找内容,把查到的内容放到缓存中。
对于缓存数据更新机制,当某一个作用域(一级缓存 Session/二级缓存 Namespaces)的进行
了 C(Create)/U(Update)/D(Delete) 操作后,默认该作用域下所有 select 中的缓存将被 clear。
Mybatis 一级缓存
基于 PerpetualCache 的 HashMap 本地缓存,其存储作用域为 Session,当Session flush 或 close 之后,该 Session 中的所有 Cache 就将清空。
Mybatis默认支持一级缓存,是SqlSession级别的,如果session关闭或者清空就没有缓存了
Mybatis 二级缓存
二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap 存储,不同在于其存储作用域为 Mapper(Namespace),并且可自定义存储源,如 Ehcache。
Mybatis二级缓存需要手动开启
- 在Mybatis配置文件中开启二级缓存
<settings>
<setting name = "cacheEnabled" value = "true" />
</settings>
- 在需要开启二级缓存的映射文件中使用二级缓存
添加一个标签,如果该文件中有不需要开启二级缓存的查询语句,就在该语句中添加usercCache=“false”
分页插件:pageHelper
使用分页插件:
1. 添加依赖
<!--pageHelper的依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
2. 在mybatis中配置xml中配置拦截器插件
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="param1" value="value1"/>
</plugin>
</plugins>
3. 开启分页
//开启分页,1代表页数,10代表每页元素个数
PageHelper.startPage(1,10);
//查询所有元素,实际查处的只有10条
List<Staff> staffs = staffMapper.selectAllStudent(staff1);
//可以存放在Page Info中,pageInfo内有所有的分页需要的数据
PageInfo<Staff> info = new PageInfo<>(staffs);
3.1 pageInfod的用法
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
List<User> list = userMapper.selectAll();
//用PageInfo对结果进行包装
PageInfo page = new PageInfo(list);
//测试PageInfo全部属性
//PageInfo包含了非常全面的分页属性
assertEquals(1, page.getPageNum());
assertEquals(10, page.getPageSize());
assertEquals(1, page.getStartRow());
assertEquals(10, page.getEndRow());
assertEquals(183, page.getTotal());
assertEquals(19, page.getPages());
assertEquals(1, page.getFirstPage());
assertEquals(8, page.getLastPage());
assertEquals(true, page.isFirstPage());
assertEquals(false, page.isLastPage());
assertEquals(false, page.isHasPreviousPage());
assertEquals(true, page.isHasNextPage());
逆向工程:generator
- 需要先加入依赖:mybatis-generator-core-x.x.x.jar
- Mybatis Generator官网:http://mybatis.org/generator/configreference/classPathEntry.html
- 创建Generator配置文件放在工程下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--mysql驱动jar所在的绝对路径 必须低于8.0以下 -->
<classPathEntry location="D:\\repMaven\\mysql\\mysql-connector-java\\5.1.47\\mysql-connector-java-5.1.47.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--连接数据库的信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql:///mybatis"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--实体类生成的路径-->
<javaModelGenerator targetPackage="com.ykq.entity" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--映射文件生成的路径-->
<sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--dao生成的路径-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.ykq.dao" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--数据库的表信息-->
<table schema="mybatis" tableName="account" domainObjectName="Account"
enableUpdateByExample="false" enableSelectByExample="false" enableDeleteByExample="false"
enableCountByExample="false"
>
<property name="useActualColumnNames" value="true"/>
<!--column:主键字段-->
<generatedKey column="ID" sqlStatement="mysql" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
<table schema="mybatis" tableName="orders" domainObjectName="Order"
enableUpdateByExample="false" enableSelectByExample="false" enableDeleteByExample="false"
enableCountByExample="false"
>
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="mysql" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>
</context>
</generatorConfiguration>
- 使用generator配置文件自动生成对应数据库表的实体类、映射文件、dao层
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generator.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);
总结:
缓存:
mybatis默认支持一级缓存------基于session 不需要配置。
mybatis也支持二级缓存----基于namespace 默认没有开启,必须在配置文件中开启二级缓存
分页:
导入依赖: pagehelper-x.x.x.jar
在 MyBatis 配置 xml 中配置拦截器插件
使用PageHelder的startPage方法分页
逆向工程:
加入依赖mybatis-generator-core-x.x.x.jar
创建配置文件
使用配置文件自动生成实体类、映射文件、dao层