MyBatis底层机制示意图

mybatis-config.xml
- mybatis-config.xml 是MyBatis全局配置文件,在项目中只能有一份。
- 通过该配置文件可以得到SqlSessionFactory对象
SqlSessionFactory
- 通过SqlSessionFactory可以得到SqlSession,拿到SqlSession就可以操作数据库了。
SqlSession
SqlSession底层是Excutor执行器。
Excutor
- Excutor是接口,定义了很多方法。
- 有两个重要的实现类,
基本执行器 BaseExcutor和缓存执行器 CacheExcutor。

Mapped Statement
- 对sql语句的参数及执行结果进行封装。
手写MyBatis框架
1. 思路分析

- 传统方式,通过Connection连接获取PrepareStatement对象,就可以执行sql了
- 使用MyBatis框架思想,则在执行sql之前,从mapper.xml中拿到sql和参数,结果类型。然后封装sql语句。执行sql之后,把得到结果封装到对应的类型之中。(动态代理实现)
2. 搭建Maven项目
配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>mybatis_edu</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--解析xml-->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!--简化java bean开发-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
</dependencies>
</project>
阶段一 :读取配置文件,获取连接

(1)配置文件mybatis-config.xml简化版
<?xml version="1.0" encoding="UTF-8" ?>
<database>
<!--配置数据库连接信息-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root1234"/>
</database>
(2)ConfigurationEdu,模拟Configuration
package com.mybatis.sqlsession;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 读取xml,建立连接
*/
public class ConfigurationEdu {
// 类加载器 ,作用:可以通过类加载器加载配置文件得到对应的流
private ClassLoader classLoader = ClassLoader.getSystemClassLoader();
// 读取配置文件并处理
public Connection build(String resource) {
Connection connection = null;
try {
// 加载配置文件,获取对应inputStream流
InputStream inputStream = classLoader.getResourceAsStream("mybatis-config.xml");
// 解析xml dom4j
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputStream);
// 获取配置文件根元素
Element root = document.getRootElement();
connection = evalElement(root);
} catch (DocumentException e) {
throw new RuntimeException(e);
}
return connection;
}
private Connection evalElement(Element node){
String driver = "",url = "",username = "",password = "";
Connection

最低0.47元/天 解锁文章
1729

被折叠的 条评论
为什么被折叠?



