查询缓存
一级缓存:
在同一个sqlSession中有效
MyBatis默认开启一级缓存,如果用同样的SqlSession对象查询相同的数据,
则只会在第一次 查询时 向数据库发送SQL语句,并将查询的结果 放入到SQLSESSION中(作为缓存);后续再次查询该同样的对象时,则直接从缓存中查询该对象即可(即省略了数据库的访问)
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1538399081.
DEBUG [main] - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@5bb21b69]
DEBUG [main] - ==> Preparing: select * from Student
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 5
1,zjj,21,true
2,zjj2,222,false
3,zjj3,23,false
5,zjj5,25,true
4,zjj4,24,true
1,zjj,21,true
2,zjj2,222,false
3,zjj3,23,false
5,zjj5,25,true
4,zjj4,24,true
DEBUG [main] - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@5bb21b69]
DEBUG [main] - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@5bb21b69]
DEBUG [main] - Returned connection 1538399081 to pool.
根据日志,两次查询,只执行一次sql操作
但是执行增删改并commit后,会清除缓存。
二级缓存
a.mybatis 自带的二级缓存:
在同一个namespace下有效
在增删改并commit后失效
在上次sqlsession close后(提交成功后)进行缓存
MyBatis默认情况没有开启二级缓存,需要手工打开。
1.conf.xml 开启二级缓存
<setting name="cacheEnabled" value="true"/>
2.在mapper.xml中声明
<cache/>
3.对象要支持序列化(Serializable)
日志:
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Cache Hit Ratio [StudentMapping]: 0.0
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1540270363.
DEBUG [main] - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@5bcea91b]
DEBUG [main] - ==> Preparing: select * from Student
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 5
1,zjj,21,true
2,zjj2,222,false
3,zjj3,23,false
5,zjj5,25,true
4,zjj4,24,true
DEBUG [main] - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@5bcea91b]
DEBUG [main] - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@5bcea91b]
DEBUG [main] - Returned connection 1540270363 to pool.
DEBUG [main] - Cache Hit Ratio [StudentMapping]: 0.5
1,zjj,21,true
2,zjj2,222,false
3,zjj3,23,false
5,zjj5,25,true
4,zjj4,24,true
Process finished with exit code 0
select 标签 可以设置是否启用缓存和是否刷新缓存
useCache="false"
flushCache="true"
逆向工程
通过数据库表生成类,mapper,接口
mybatis-generator-core.jar
generator.xml
<?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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!--
suppressAllComments属性值:
true:自动生成实体类、SQL映射文件时没有注释
true:自动生成实体类、SQL映射文件,并附有注释
-->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库连接信息 -->
<jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"
userId="scott" password="tiger">
</jdbcConnection>
<!--
forceBigDecimals属性值:
true:把数据表中的DECIMAL和NUMERIC类型,
解析为JAVA代码中的java.math.BigDecimal类型
false(默认):把数据表中的DECIMAL和NUMERIC类型,
解析为解析为JAVA代码中的Integer类型
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--
targetProject属性值:实体类的生成位置
targetPackage属性值:实体类所在包的路径
-->
<javaModelGenerator targetPackage="entity"
targetProject=".\src">
<!-- trimStrings属性值:
true:对数据库的查询结果进行trim操作
false(默认):不进行trim操作
-->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--
targetProject属性值:SQL映射文件的生成位置
targetPackage属性值:SQL映射文件所在包的路径
-->
<sqlMapGenerator targetPackage="mapper"
targetProject=".\src">
</sqlMapGenerator>
<!-- 生成动态代理的接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="mapper" targetProject=".\src">
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="Student"> </table>
<table tableName="Card"> </table>
<table tableName="Class"> </table>
</context>
</generatorConfiguration>
main
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class MybatisGenerator {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
File file = new File("src/main/resources/generator.xml") ;//配置文件
List<String> warnings = new ArrayList<String>();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(file);
DefaultShellCallback callBack = new DefaultShellCallback(true);
//逆向工程的核心类
MyBatisGenerator generator = new MyBatisGenerator(config, callBack,warnings );
generator.generate(null);
}
}