A main frame: Mybatis frame (2) to understand the underlying details of custom

A: Custom Mybatis analysis

1. Purpose significance:

Objective: To enable us to better understand mybatis internally how to perform in the future to better use mybatis development framework **, ** while its design (design patterns) ** have an understanding.

2.Mybatis work process

mybatis in the way of using a proxy dao realize additions and deletions to change search while doing it?
Only two things:
First: Create a proxy object
second: call the proxy object selectList **** execute sql statement
custom mybatis see through the entry-class case, we have to implement them ( using the principle underlying )

	class Resources
	class SqlSessionFactoryBuilder
	interface SqlSessionFactory
	interface SqlSession

3. The details contained

Factory pattern (Factory's factory mode), configured by mode (mode Builder), proxy mode, reflection, custom annotations, annotation reflection, XML parsing,
reflected database metadata, the metadata

Two: implementation flow analysis

Here Insert Picture Description

first step

The SqlSessionFactoryBuilder (constructors) accepts SqlMapConfig.xml file stream (profile information) to construct a SqlSessionFactory (plant)

The second step

1.SqlSessionFactory(工厂)读取SqlMapConfig.xml读取下面的连接数据库和mapper的映射信息。用来生产出真正操作数据库的SqlSession对象
Here Insert Picture Description
映射配置文件中的(dao接口中的)sql语句信息,以及信息是何种实体类(domain.User)
Here Insert Picture Description
2.读取xml文件均需要dom4j技术来读取,通过一个工具类XMLConfigBuilder来进行(内部底层是dom4j以及xpath),实际上是Resources的getResourceAsStream来返回一个IO流对象

然后返回一个Configuration对象,(自定义mybatis的配置类)里面含有driver和url以及Mapper对象等等信息。。

3.代理对象Mapper

Mapper对象中包含了获取的唯一标识(key是由dao的全限定类名和方法名组成)以及执行所需的必要信息(value是一个Mapper对象,里面存放的是执行的SQL语句和要封装的实体类全限定类名),是用于封装执行的SQL语句和结果类型的全限定类名

第三步

创建出SqlSession对象后,此对象有两个大的作用,从而引出实现两个分支,作用如下:

(1)生成dao接口的代理对象

(2)定义通用的增删改查的方法

无论是哪个分支作用,除了连接数据库信息,还需要得到Sql语句

第四步

作用(1)中:在SqlSessionImpl对象即Mapper对象中,执行getMapper方法分两步实现

1.先用SqlSessionFactory读取的数据库连接信息创建一个Connection对象 return DriverManager.getConnection(cfg.getUrl(), cfg.getUsername(), cfg.getPassword());

2.然后jdk代理模式创建出代理对象作为getMapper方法返回值,这里主要工作是在创建代理对象时第三个参数处理类里面得到Sql语句new MapperProxy(cfg.getMappers(),connection),执行增删改查操作 public List selectList(Mapper mapper, Connection conn)

作用(2)中:在SqlSessionImpl对象即Mapper对象中提供selectList方法

第一:用SqlSessionFactory读取的的数据库连接信息创建出jdbc的Connection对象。

第二:直接得到Sql语句,使用jdbc的Connection对象进行增删改查操作。

最后统统要对返回的数据库结果集进行封装,变成java对象返回给调用者。所以我们还必须要知道调用者所需要的返回类型。resultType="com.itheima.domain.User

三:自定义Mybatis的总结

Whether it is let Mybatis help us create a proxy object or direct use Mybatis provided the underlying is jdbc Connection object, perform the corresponding sql statement, the final package into the result set. Just two weeks annotations and xml configuration file transfer development model in the delivery sql statement and return value type of way vary it. Specifically as shown below:
Here Insert Picture Description

Published 47 original articles · won praise 18 · views 4880

Guess you like

Origin blog.csdn.net/qq_43605085/article/details/96488114