frame
Framework (Framework) is the reusable design of the whole or part of the system, expressed as a set of abstract components and methods of interaction between component instances; Another definition is that a framework is an application skeleton that can be customized by application developers. The former is from the application side and the latter is the definition given from the purpose side. Simply put, you use the stage set up by others and you do the performance. Moreover, the framework is generally mature and constantly upgraded software. |
mybatis framework
Mybatis is an excellent Java-based persistence layer framework, which encapsulates jdbc, so that developers only need to pay attention to the sql statement itself, instead of spending energy to deal with the complicated process of loading drivers, creating connections, and creating statements. Mybatis configures various statements to be executed through xml or annotations, and generates the final executed SQL statement through the mapping of java objects and the dynamic parameters of sql in the statement. Finally, the mybatis framework executes sql and maps the results to java objects. return. The ORM idea is adopted to solve the problem of entity and database mapping, encapsulate jdbc, shield the details of the underlying access of jdbc api, so that we can complete the persistence operation of the database without dealing with jdbc api. |
The difference between #{} and ${}
#{} represents a placeholder symbol. The #{}Can receive simple type values or pojo attribute values. If parameterType transmits a single simple type value, #{} brackets can be value or other names.
If parameterType transmits a single simple type value, only value can be enclosed in ${} brackets. |
Comparison of mybatis and jdbc programming
1. Frequent database link creation and release cause waste of system resources and thus affect system performance. This problem can be solved if the database link pool is used.
|
Delayed loading
Many times in the actual development process, we do not always have to load his account information when loading user information. This is what we call lazy loading. 延迟加载:就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据。延迟加载也称懒加载. 所以可能造成用户等待时间变长,造成用户体验下降。 |
一级缓存与二级缓存
一级缓存 是 SqlSession 范围的缓存,当调用 SqlSession 的修改,添加,删除, commit(), close()等方法时,就会清空一级缓存。 理解: 第一次发起查询用户 id 为 1 的用户信息,先去找缓存中是否有 id 为 1 的用户信息,如果没有,从数据库查询用户信息。得到用户信息,将用户信息存储到一级缓存中。如果 sqlSession 去执行 commit 操作(执行插入、更新、删除),清空 SqlSession 中的一级缓存,这样做的目的为了让缓存中存储的是最新的信息,避免脏读。第二次发起查询用户 id 为 1 的用户信息,先去找缓存中是否有 id 为 1 的用户信息,缓存中有,直接从缓存中获取用户信息。
二级缓存 是 mapper 映射级别的缓存,多个 SqlSession 去操作同一个 Mapper 映射的 sql 语句,多个SqlSession 可以共用二级缓存,二级缓存是跨 SqlSession 的。 理解: 首先开启 mybatis 的二级缓存。 如果 SqlSession3 去执行相同 mapper 映射下 sql,执行 commit 提交, 将会清空该 mapper 映射下的二级缓存区域的数据。 在 SqlMapConfig 中开启二级缓存支持: <!-- 配置二级缓存 --> 在持久层接口中使用注解配置二级缓存: @CacheNamespace(blocking=true) //mybatis 基于注解方式实现配置二级缓存 |
一. mybatis 框架中常用的设计模式
1. 工厂模式优势:解耦合,降低类之间的依赖关系。A a = new A() 根据传参,灵活创建不同的对象; 2. 构建者模式优势:把对象的创建细节隐藏,使用者直接调用方法即可拿到对象。把细节封装,给我不同的要求,创建不同的对象出来; ***构建者模式和工厂模式的区别: 构建者模式最主要的功能是基本方法的调用顺序安排,也就是这些基本方法已经实现了,通俗地说就是零件的装配,顺序不同产生的对象也不同;而工厂模式则重点是创建,创建对象是它的主要职责,组装顺序它不关心。 3.代理模式: 不修改源码的基础上对已有方法增强。只有接口,没有实现类,通过代理模式,也可以调用方法。 |
二. mybatis 框架的原理
1.mybatis配置 即 SqlMapConfig.xml,此文件作为 mybatis 的全局配置文件,配置了 mybatis 的运行环境等信息;
2.mapper.xml 即 sql 映射文件,文件中配置了操作数据库的sql语句。此文件需要在 SqlMapConfig.xml 中加载;
3.通过mybatis环境等配置信息构造 SqlSessionFactory, 即会话工厂;
4.由会话工厂创建 sqlSession, 即会话,操作数据库需要通过 sqlSession 进行;
5.mybatis 底层自定义了 Executor 执行器接口操作数据库,Executor 接口有两个实现,一个是基本执行器、一个是缓存执行器;
6.Mapped Statement 也是 mybatis 一个底层封装对象,它包装了 mybatis 配置信息及 sql 映射信息等。mapper.xml 文件中一个 sql对应一个 Mapped Statement 对象,sql 的 id 即是 Mapped statement 的 id;
7.Mapped Statement 对 sql 执行输入参数进行定义,包括 HashMap、基本类型、pojo,Executor 通过 Mapped Statement 在执 行 sql 前将输入的 java 对象映射至 sql 中,输入参数映射就是 jdbc 编程中对 preparedStatement 设置参数;
8.Mapped Statement 对 sql 执行输出结果进行定义,包括 HashMap、基本类型、pojo,Executor 通过 Mapped Statement 在执 行 sql 后将输出结果映射至 java 对象中,输出结果映射过程相当于jdbc编程中对结果的解析处理过程。