目录
十三、MyBatis的缓存
13.1一级缓存
13.2二级缓存
13.3MyBatis集成EhCache
十四、MyBatis的逆向工程
14.1逆向工程配置与生成
14.2测试
十三、MyBatis的缓存

-  
缓存:cache
 -  
缓存的作用:通过减少IO的方式,来提高程序的执行效率。
 -  
mybatis的缓存:将select语句的查询结果放到缓存(内存)当中,下一次还是执行这条select语句的话,直接从缓存中取,不再查数据库。
-  
减少了IO
 -  
不再执行繁琐的查找算法,效率大大提升。
 
 -  
 -  
mybatis的缓存包括:
-  
一级缓存:将查询的数据存储到SqlSession中。
 -  
二级缓存:将查询到的数据存储到SqlSessionFactory中。
 -  
或者集成其它第三方的缓存:比如EhCache[Java语言开发的]、Memcache[C语言开发的]等。
 
 -  
 -  
缓存只针对DQL语句,也就是说缓存机制只对应select语句。
 
13.1一级缓存
-  
一级缓存是默认开启的,不需要做任何配置。
 -  
原理:只要使用同一个SqlSession对象执行同一条SQL语句,就会走缓存。
 -  
什么情况下不走缓存:
-  
第一种:不同的SqlSession对象。
 -  
第二种:查询条件变化了。
 
 -  
 -  
一级缓存失效情况包括两种:
-  
第一种:第一次查询和第二次查询之间,手动清空了一级缓存。
 
@Test public void testSelectById(){ SqlSession sqlSession = SqlSessionUtil.openSession();  CarMapper mapper1 = sqlSession.getMapper(CarMapper.class); Car car1 = mapper1.selectById(164L); System.out.println(car1);  // 手动清空一级缓存 sqlSession.clearCache();  CarMapper mapper2 = sqlSession.getMapper(CarMapper.class); Car car2 = mapper2.selectById(164L); System.out.println(car2);  sqlSession.commit(); sqlSession.close();-  
第二种:第一次查询和第二次查询之间,执行力增删改操作。【这个增删改和哪张表没有关系,只有insert delete update操作,一级缓存就失效】
 
@Test public void testSelectById(){ SqlSession sqlSession = SqlSessionUtil.openSession();  CarMapper mapper1 = sqlSession.getMapper(CarMapper.class); Car car1 = mapper1.selectById(164L); System.out.println(car1);  // 在这里执行了INSERT DELETE UPDATE中的任意一个语句。并且和表没有关系。 CarMapper mapper = sqlSession.getMapper(CarMapper.class); mapper.insertClazz(2000, "高三三班");  CarMapper mapper2 = sqlSession.getMapper(CarMapper.class); Car car2 = mapper2.selectById(164L); System.out.println(car2);  sqlSession.commit(); sqlSession.close(); -  
 
13.2二级缓存
-  
二级缓存的范围是SqlSessionFactory。
 -  
使用二级缓存需要具备以下几个条件:
-  
<setting name="cacheEnabled" value="true">在mybatis核心配置文件中全局性地开启或关闭所有映射器配置文件中已配置的任何缓存。默认是true,无需设置。
 

-  
在需要使用二级缓存的SqlMapper.xml文件中添加配置:<cache/>
 
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.hhb.mapper.CarMapper">  <cache/>  <select id="selectById" resultType="car"> select * from t_car where id = #{id}; </select>  </mapper>-  
使用二级缓存的实体类对象必须是可序列化的,也就是必须实现java.io.Serializable接口。
 

-  
SqlSession对象关闭或提交之后,一级缓存中的数据才会被写入到二级缓存当中,此时二级缓存才可用。
 
@Test public void testSelectById() throws IOException { //这里只有一个SqlSessionFactory对象,二级缓存对应的就是SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml")); SqlSession sqlSession1 = sqlSessionFactory.openSession(); SqlSession sqlSession2 = sqlSessionFactory.openSession(); CarMapper mapper1 = sqlSession1.getMapper(CarMapper.class); CarMapper mapper2 = sqlSession2.getMapper(CarMapper.class);  //这段代码执行之后,实际上数据是缓存到一级缓存当中了。(sqlSession1是一级缓存) Car car1 = mapper1.selectById(40L); System.out.println(car1); //如果这里不关闭SqlSession1对象的话,二级缓存中是没有数据的。 //程序执行到这里的时候,会将sqlSession1这个一级缓存中的数据写入到二级缓存中。 sqlSession1.close();  //这段代码执行之后,实际上数据是缓存到一级缓存当中了。(sqlSession2是一级缓存) Car car2 = mapper2.selectById(40L); System.out.println(car2);  sqlSession2.close(); } -  
 
-  
二级缓存的失效:只要查询两次之间出现了增删改操作,二级缓存就会失效。【一级缓存也会失效】
 -  
二级缓存的相关配置:
 

-  
eviction(驱逐):指定从缓存中移除某个对象的淘汰算法。默认使用LRU策略。
-  
LRU:Least Recently User。最近最少使用。优先淘汰在间隔时间内使用频率最低的对象。(其实还有一种淘汰算法LFU,最不常用。)
 -  
FIFO:First In First Out。一种先进先出的数据缓存器。先进入二级缓存的对象最先被淘汰。
 -  
SOFT:软引用。
 -  
WEAK:弱引用。
 
 -  
 -  
flushInterval:
-  
二级缓存的刷新时间间隔。单位毫秒。如果没有配置,就代表不刷新缓存,只要内存足够大,一直会向二级缓存中缓存数据,除非执行力增删改。
 
 -  
 -  
readOnly:
-  
true:多条相同的sql语句执⾏之后返回的对象是共享的同⼀个。性能好。但是多线程并发可能会存在安全问题。
 -  
false:多条相同的sql语句执⾏之后返回的对象是副本,调⽤了clone⽅法。性能⼀般。但安全。
 
 -  
 -  
size:
-  
设置二级缓存中最多可存储的java对象数量,默认值是1024.
 
 -  
 
13.3MyBatis集成EhCache
-  
集成EhCache是为了代替mybatis自带的二级缓存,一级缓存是无法替代的。
 -  
mybatis对外提供了接口,也可以集成第三方的缓存组件。比如EhCache、Memcache等都可以。
 -  
EhCache是Java写的,Memcache是C语言写的,所以mybatis集成EhCache较为常见。
 
第一步:在pom.xml文件中引入mybatis整合ehcache的依赖。
<!--mybatis集成ehcache的组件-->
<dependency>
 <groupId>org.mybatis.caches</groupId>
 <artifactId>mybatis-ehcache</artifactId>
 <version>1.2.2</version>
</dependency>
<!--ehcache需要slf4j的⽇志组件,log4j不好使-->
<dependency>
 <groupId>ch.qos.logback</groupId>
 <artifactId>logback-classic</artifactId>
 <version>1.2.11</version>
 <scope>test</scope>
</dependency> 
第二步:在类的根路径下新建echcache.xml⽂件,并提供以下配置信息。
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
 updateCheck="false">
 <!--磁盘存储:将缓存中暂时不使⽤的对象,转移到硬盘,类似于Windows系统的虚拟内存-->
 <diskStore path="e:/ehcache"/>
 
 <!--defaultCache:默认的管理策略-->
 <!--eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有
效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断-->
 <!--maxElementsInMemory:在内存中缓存的element的最⼤数⽬-->
 <!--overflowToDisk:如果内存中数据超过内存限制,是否要缓存到磁盘上-->
 <!--diskPersistent:是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false-
->
 <!--timeToIdleSeconds:对象空闲时间(单位:秒),指对象在多⻓时间没有被访问就会失
效。只对eternal为false的有效。默认值0,表示⼀直可以访问-->
 <!--timeToLiveSeconds:对象存活时间(单位:秒),指对象从创建到失效所需要的时间。
只对eternal为false的有效。默认值0,表示⼀直可以访问-->
 <!--memoryStoreEvictionPolicy:缓存的3 种清空策略-->
 <!--FIFO:first in first out (先进先出)-->
 <!--LFU:Less Frequently Used (最少使⽤).意思是⼀直以来最少被使⽤的。缓存的元
素有⼀个hit 属性,hit 值最⼩的将会被清出缓存-->
 <!--LRU:Least Recently Used(最近最少使⽤). (ehcache 默认值).缓存的元素有⼀
个时间戳,当缓存容量满了,⽽⼜需要腾出地⽅来缓存新的元素的时候,那么现有缓存元素中时间戳
离当前时间最远的元素将被清出缓存-->
 <defaultCache eternal="false" maxElementsInMemory="1000" overflowToDis
k="false" diskPersistent="false"
 timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStor
eEvictionPolicy="LRU"/>
</ehcache> 
第三步:修改CarMapper.xml文件中的<cache/>标签,添加type属性。
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> 
十四、MyBatis的逆向工程
-  
所谓的逆向工程是:根据数据库表逆向生成Java的pojo类,SqlMapper.xml文件,以及Mapper接口类等。
 -  
要想完成这个工作,需要借助别人写好的逆向工程插件。
 -  
使用这个插件,需要给这个插件配置哪些信息?
-  
pojo类名、包名以及生成位置。
 -  
SqlMapper.xml文件名以及生成位置。
 -  
Mapper接口名以及生成位置。
 -  
连接数据库的信息。
 -  
指定哪些表参与逆向工程。
 -  
...
 
 -  
 
14.1逆向工程配置与生成
第一步:基础环境准备
-  
新建模块:mybatis012-generator1
 -  
打包⽅式:jar
 
第二步:在pom.xml中添加逆向工程插件
<!--定制构建过程-->
<build>
 <!--可配置多个插件-->
 <plugins>
 <!--其中的⼀个插件:mybatis逆向⼯程插件-->
 <plugin>
 <!--插件的GAV坐标-->
 <groupId>org.mybatis.generator</groupId>
 <artifactId>mybatis-generator-maven-plugin</artifactId>
 <version>1.4.1</version>
 <!--允许覆盖-->
 <configuration>
 <overwrite>true</overwrite>
 </configuration>
 <!--插件的依赖-->
 <dependencies>
 <!--mysql驱动依赖-->
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>8.0.31</version>
 </dependency>
 </dependencies>
 </plugin>
 </plugins>
</build> 
第三步:配置generatorConfig.xml
-  
该文件名必须叫做:generatorConfig.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>
    <!--
        targetRuntime有两个值:
            MyBatis3Simple:生成的是基础版,只有基本的增删改查。
            MyBatis3:生成的是增强版,除了基本的增删改查之外还有复杂的增删改查。
    -->
    <context id="DB2Tables" targetRuntime="MyBatis3Simple">
        <!--防止生成重复代码-->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>
        <commentGenerator>
            <!--是否去掉生成日期-->
            <property name="suppressDate" value="true"/>
            <!--是否去除注释-->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--连接数据库信息-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatis"
                        userId="root"
                        password="030522">
        </jdbcConnection>
        <!-- 生成pojo包名和位置 -->
        <javaModelGenerator targetPackage="com.hhb.pojo" targetProject="src/main/java">
            <!--是否开启子包-->
            <property name="enableSubPackages" value="true"/>
            <!--是否去除字段名的前后空白-->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成SQL映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="com.hhb.mapper" targetProject="src/main/resources">
            <!--是否开启子包-->
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成Mapper接口的包名和位置 -->
        <javaClientGenerator
                type="xmlMapper"
                targetPackage="com.hhb.mapper"
                targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 表名和对应的实体类名-->
        <table tableName="t_car" domainObjectName="Car"/>
    </context>
</generatorConfiguration> 
-  
MyBatis3Simple:生成的是基础班,只有基本的增删改查。
 -  
MyBatis3:生成的是增强版,除了基本的增删改查之外还有复杂的增删改查。
 
第四步:运行插件

14.2测试
 //CarExample类负责封装查询条件
    @Test
    public void testSelect() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        //执行查询
        //1.查询一个
        Car car = mapper.selectByPrimaryKey(39L);
        System.out.println(car);
        //2.查询所有(selectByExample,根据条件查询,如果条件是null表示没有条件)
        List<Car> cars = mapper.selectByExample(null);
        cars.forEach(car1 -> System.out.println(car1));
        //3.按条件查询
        //QBC风格:Query By Criteria 一种查询方式,比较面向对象,看不到sql语句。
        //封装条件:通过CarExample对象来封装查询条件
        CarExample carExample = new CarExample();
        //调用carExample.createCriteria()方法来创建查询条件
        carExample.createCriteria().andBrandLike("比亚迪秦").andGuidePriceGreaterThan(new BigDecimal(20.00));
        //添加or
        carExample.or().andCarTypeEqualTo("燃油车");
        //执行查询
        List<Car> cars2 =mapper.selectByExample(carExample);
        cars2.forEach(car2 -> System.out.println(car2));
        sqlSession.close();
    } 
 
                


















