框架|MyBatis框架——快速入门第二篇

MyBatis框架入门第二篇

上一篇文章我们介绍了Mybatis的一些入门操作,包括配置文件的细节、CRUD操作,动态Sql以及基于Mapperd代理方式开发等内容,这篇文章开始着手MyBtis的缓存机制以及逆向工程的开发
引入log4j日志框架 在项目中加入log4j的配置文件,用于打印日志信息,便于开发调试
在src/main/resources目录下创建log4j.properties文件,配置信息如下:
# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

只要将该文件放在指定的位置,log4j工具会自动到指定位置加载上述文件,读取文件中的配置信息并使用!
框架|MyBatis框架——快速入门第二篇
文章图片

MyBatis缓存机制 缓存的意义 将用户经常查询的数据放在缓存(内存)中,这样用户去查询数据就不用频繁的从系型数据库进行查询,这样避免了频繁的与数据库进行交互,尤其在查询越多,缓存命中率越高的情况下,使用缓存对性能的提高更明显
MyBatis框架提供了对缓存的支持,分为一级缓存和二级缓存,默认情况下开启的是一级缓存(同一个SqlSession级别)
一级缓存 同一个SqlSession对象,在参数和Sql语句完全一样的情况下,只执行一次Sql语句进行查询(如果缓存没有过期)
修改 上一篇文章我们进行了代码优化,封装了一个MyBatisUtil工具类,引入ThreadLocal,现在方便我们测试,需要将ThreadLocal变量暂时屏蔽掉,这样保证每次创建的SqlSession都是不相同的。
/** * 获取SqlSession对象 * @return */ public static SqlSession getSqlSession(){ //SqlSession sqlSession = threadLocal.get(); //if (sqlSession == null){ //sqlSession = sqlSessionFactory.openSession(true); //threadLocal.set(sqlSession); //} SqlSession sqlSession = sqlSessionFactory.openSession(true); return sqlSession; }

相同SqlSession
public static void main(String[] args) throws IOException { System.out.println("第一次查询"); SqlSession sqlSession = MybatisUtil.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); System.out.println(mapper.getStudentById("09")); System.out.println("相同SqlSession的第二次查询"); StudentMapper mapper2 = sqlSession.getMapper(StudentMapper.class); System.out.println(mapper2.getStudentById("09")); }

输出结果如下:
框架|MyBatis框架——快速入门第二篇
文章图片

第一次查询发送了Sql语句后返回了结果
第二次查询没有发送Sql语句直接返回了结果,说明是从一级缓存中直接获取的
不同SqlSession
public static void main(String[] args) throws IOException { System.out.println("第一次查询"); SqlSession sqlSession = MybatisUtil.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); System.out.println(mapper.getStudentById("09")); System.out.println("不同SqlSession的第二次查询"); SqlSession sqlSession2 = MybatisUtil.getSqlSession(); StudentMapper mapper2 = sqlSession2.getMapper(StudentMapper.class); System.out.println(mapper2.getStudentById("09")); }

输出结果如下:
框架|MyBatis框架——快速入门第二篇
文章图片

第一次查询发送了Sql语句后返回了结果
第二次查询也同样发送了Sql语句后返回了结果,没有从缓存中获取
刷新缓存 刷新缓存是清空这个SqlSession的所有缓存,并不只是某一个
在xml文件中添加flushCache="true
="getStudentById" flushCache="true" parameterType="string" resultType="com.cn.pojo.Student" > SELECT * from Student where id= #{id};

相同SqlSession的情况下输出结果如下:
框架|MyBatis框架——快速入门第二篇
文章图片

图片因为加入了刷新缓存,所有前后两次查询都发送了Sql语句
总结:
  • 相同的SqlSession,MyBatis框架会把查询的结果写入SqlSession的一级缓存中,缓存使用的数据结构是Map,其中的key就是把执行的方法和参数通过算法生成缓存的key,如果后面查询的键值一样,就直接从map中获取。
  • 相同的SqlSession,在查询前可以通过配置flushCache="true清空缓存 不同的SqlSession之间缓存是隔离的
  • 任何UPDATE,INSERT,DELETE的提交事务的语句都会清空缓存,为的就是避免读取的时候有脏数据
二级缓存 二级缓存是用来解决一级缓存不能跨会话共享的问题,范围是mapper级别,可以被多个SqlSession共享(只要是同一个接口里的相同方法都可以共享),
查询顺序:二级缓存 —> 一级缓存 —> 数据库
配置二级缓存 在mybatis-config.xml文件中配置全局开关
>="cacheEnabled" value="https://www.it610.com/article/true"/>

在对应的映射文件mapper.xml中配置分开关

映射pojo的序列化
@Data public class Student implements Serializable { //代码省略 }

测试代码
public static void main(String[] args) throws IOException { System.out.println("第一次查询"); SqlSession sqlSession = MyBatisUtils.getSqlSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); System.out.println(studentMapper.getStudentById("08")); sqlSession.close(); System.out.println("第二次查询"); SqlSession sqlSession2 = MyBatisUtils.getSqlSession(); StudentMapper mapper2 = sqlSession2.getMapper(StudentMapper.class); System.out.println(mapper2.getStudentById("08")); }

输出结果如下:
框架|MyBatis框架——快速入门第二篇
文章图片

第一次查询发送了Sql语句后返回了结果
第二次查询没有发送Sql语句直接返回了结果,从二级缓存中获取,命中率为0.5
总结:
  • 调用sqlSession.close()或者sqlSession.commit()这两个方法后,此时才能将数据序列化并保存到二级缓存中
  • 刷新缓存时会清空所有缓存,所以对于变化频率高的Sql要禁用二级缓存
MyBatis逆向工程 什么是逆向工程
之前我们写代码都是自己手动创建实体类、映射文件和Sql语句,MyBatis框架提供逆向工程,可以针对单表自动生成MyBatis执行所需要的的代码(pojo,mapper,mapper.xml
等等),提高开发效率。
逆向工程开发 pom.xml中配置逆向工程插件
MyBatisorg.mybatis.generator mybatis-generator-maven-plugin 1.3.2 true true

创建generatorConfig.xml配置文件(默认放在resources包下)
="mapper" targetProject="src/main/resources">
引入的db.properties配置信息如下:
database.driver=com.mysql.jdbc.Driver database.url=jdbc:mysql://localhost:3306/db20?characterEncoding=utf-8 database.username=root database.password=rootjdbc.driverLocation=C:\\Users\\Administrator\\maven\\repository\\mysql\\mysql-connector-java\\5.1.47\\mysql-connector-java-5.1.47.jar

在IDEA添加一个"Run 运行选项",使用maven运行mybatis-generator-maven-plugin插件
点击 菜单run中Edit Configurations,会出现
框架|MyBatis框架——快速入门第二篇
文章图片

点击 + 号,选择maven
框架|MyBatis框架——快速入门第二篇
文章图片

在name和Commond line如上图所示分别填写,后apply和ok
【框架|MyBatis框架——快速入门第二篇】最后点击generator,生成model,mapper,dao
逆向工程生成如下图:
框架|MyBatis框架——快速入门第二篇
文章图片

注意:Mybatis中逆向工程生成的mapper所进行的操作都是单表的
到目前为止MyBatis框架入门第二篇就已经讲解完毕,下一篇文章我们开始学习Spring框架的快速入门。
因为本人还是一枚正在路上的菜鸟,难免会有错误之处还望大家及时指正,可以加我微信私聊,如果对您有所帮助,文末别忘了点赞,再看噢~~~

    推荐阅读