SSM-mybatis persistence layer framework

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.
In short, a framework is actually a semi-finished product of a certain application, a set of components for you to choose to complete your own system.

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
preparedStatement can be used to set a value in the placeholder through #{}, and the java type and jdbc type are automatically converted. #{} can effectively prevent sql injection.

#{}Can receive simple type values ​​or pojo attribute values. If parameterType transmits a single simple type value, #{} brackets can be value or other names.


${} means splicing sql string
through ${}, the content passed in parameterType can be spliced ​​in sql without jdbc type conversion, ${} can receive simple type values ​​or pojo attribute values,

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.
   Configure the data link pool in SqlMapConfig.xml, and use the connection pool to manage database links.


2. Sql statement written in the code makes the code difficult to maintain, and the actual application of sql may change greatly, and the change of sql needs to change the java code.
   Separate the Sql statement configuration from the java code in the XXXXmapper.xml file.


3. It is troublesome to pass parameters to the sql statement, because the where condition of the sql statement is not necessarily, there may be more or less, and the placeholders need to correspond to the parameters.
   Mybatis automatically maps java objects to sql statements, and defines the type of input parameters through the parameterType in the statement.


4. It is troublesome to parse the result set. SQL changes cause the parsing code to change, and it needs to be traversed before parsing. It is more convenient if the database records can be encapsulated into pojo objects for parsing.
   Mybatis automatically maps the sql execution result to the java object, and defines the type of the output result through the resultType in the statement.

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 的二级缓存。
sqlSession1 去查询用户信息,查询到用户信息会将查询数据存储到二级缓存中。

如果 SqlSession3 去执行相同 mapper 映射下 sql,执行 commit 提交, 将会清空该 mapper 映射下的二级缓存区域的数据。
sqlSession2 去查询与 sqlSession1 相同的用户信息, 首先会去缓存中找是否存在数据,如果存在直接从缓存中取出数据。

在 SqlMapConfig  中开启二级缓存支持:

<!-- 配置二级缓存 -->
<settings>
     <!-- 开启二级缓存的支持 -->
     <setting name="cacheEnabled" value="true"/>
</settings>

在持久层接口中使用注解配置二级缓存:

@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编程中对结果的解析处理过程。

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_42629433/article/details/83449627