首先数据准备,创建MySQL数据库mybatis,创建表并插入数据。
DROP TABLE IF EXISTS user_t;
CREATE TABLE user_t ( id INT PRIMARY KEY, username VARCHAR ( 128 ) );
INSERT INTO user_t VALUES(1,'Tom');
INSERT INTO user_t VALUES(2,'Jerry');
JDBC API允许应用程序访问任何形式的表格数据,特别是存储在关系数据库中的数据。
JDBC代码示例:
@Test
@DisplayName("JDBC模式访问数据库示例")
public void jdbc_test() {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 通过驱动管理类来获取数据库连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8", "root", "123456");
// 定义SQL语句 ?表示占位符
String sql = "select * from user_t where username = ?";
// 获取预处理Statement
preparedStatement = connection.prepareStatement(sql);
// 设置参数,第一个参数为SQL语句中参数的序号(从1开始),第二个参数为设置的参数值
preparedStatement.setString(1, "Tom");
// 向数据库发出SQL执行查询,查询出结果集
resultSet = preparedStatement.executeQuery();
// 遍历查询结果集
while (resultSet.next()) {
int id = resultSet.getInt("id");
String username = resultSet.getString("username");
// 封装User
User user = new User();
user.setId(id);
user.setUsername(username);
System.out.println(user);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
我们发现使用过程中存在以下问题:
手写持久层框架思路分析
创建一个maven项目mybatis-demo,作为使用端,引入自定义持久层框架jar包。
其中文件内容如下:
package com.mybatis.it.dao;
import com.mybatis.it.pojo.User;
import java.util.List;
public class UserDaoImpl implements IUserDao {
@Override
public List<User> findAll() {
return null;
}
@Override
public User findByCondition(User user) {
return null;
}
}
package com.mybatis.it.pojo;
public class User {
private int id;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
'}';
}
}
创建SqlMapConfig.xml配置文件:存放数据库配置信息、存放mapper.xml路径
<configuration>
<dataSource>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&serverTimezone=UTC">property>
<property name="username" value="root">property>
<property name="password" value="123456">property>
dataSource>
<mappers>
<mapper resource="mapper/UserMapper.xml">mapper>
mappers>
configuration>
创建mapper.xml配置文件:存放SQL信息、参数类型、返回值类型等
<mapper namespace="com.mybatis.it.dao.IUserDao">
<select id="findAll" resultType="com.mybatis.it.pojo.User">
select * from user_t
select>
<select id="findByCondition" resultType="com.mybatis.it.pojo.User" parameterType="com.mybatis.it.pojo.User">
select * from user_t where id = #{id} and username = #{username}
select>
mapper>
单元测试用例:
package com.mybatis.it;
import com.mybatis.it.dao.IUserDao;
import com.mybatis.it.pojo.User;
import com.mybatis.it.sdk.io.Resources;
import com.mybatis.it.sdk.session.SqlSession;
import com.mybatis.it.sdk.session.SqlSessionFactory;
import com.mybatis.it.sdk.session.SqlSessionFactoryBuilder;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.InputStream;
import java.util.List;
public class MyBatisSDKTest {
@Ignore
@DisplayName("测试手写版本1MyBatis使用")
public void test() {
// 1. 根据配置文件的路径,加载成字节输入流,存到内存中 注意:配置文件还未解析
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
// 2. 解析了配置文件,封装了Configuration对象 ; 创建sqlSessionFactory工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 3. 生产sqlSession 创建了执行器对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 4. 调用sqlSession方法
User user = new User();
user.setId(1);
user.setUsername("Tom");
User user2 = sqlSession.selectOne("user.selectOne", user);
System.out.println(user2);
List<Object> list = sqlSession.selectList("user.selectList", null);
System.out.println(list);
// 释放资源
sqlSession.close();
}
@Test
@DisplayName("测试手写版本2MyBatis使用")
public void test2() {
// 1. 根据配置文件的路径,加载成字节输入流,存到内存中 注意:配置文件还未解析
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
// 2. 解析了配置文件,封装了Configuration对象 ; 创建sqlSessionFactory工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 3. 生产sqlSession 创建了执行器对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 4. 调用sqlSession方法
User user = new User();
user.setId(1);
user.setUsername("Tom");
IUserDao userDao = sqlSession.getMapper(IUserDao.class);
User user2 = userDao.findByCondition(user);
System.out.println(user2);
List<User> list = userDao.findAll();
System.out.println(list);
// 释放资源
sqlSession.close();
}
}
项目pom.xml内容如下:
<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.0modelVersion>
<groupId>com.mybatis.itgroupId>
<artifactId>mybatis-demoartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>com.mybatis.it.sdkgroupId>
<artifactId>mybatis-sdkartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-engineartifactId>
<version>5.10.1version>
<scope>testscope>
dependency>
dependencies>
project>
同时创建一个maven模块:mybatis-sdk
引入依赖如下(pom.xml)
<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.0modelVersion>
<groupId>com.mybatis.it.sdkgroupId>
<artifactId>mybatis-sdkartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>dom4jgroupId>
<artifactId>dom4jartifactId>
<version>1.6.1version>
dependency>
<dependency>
<groupId>jaxengroupId>
<artifactId>jaxenartifactId>
<version>1.1.6version>
dependency>
<dependency>
<groupId>com.mysqlgroupId>
<artifactId>mysql-connector-jartifactId>
<version>8.2.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.20version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-slf4j-implartifactId>
<version>2.22.0version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-engineartifactId>
<version>5.10.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.30version>
<scope>providedscope>
dependency>
dependencies>
project>
创建Resources类:负责加载配置文件,加载成字节流,存到内存中
package com.mybatis.it.sdk.io;
import java.io.InputStream;
public class Resources {
/**
* 根据配置文件的路径,加载配置文件成字节输入流,存到内存中,注意配置文件还未解析
*
* @param path
* @return
*/
public static InputStream getResourceAsStream(String path) {
InputStream inputStream = Resources.class.getClassLoader().getResourceAsStream(path);
return inputStream;
}
}
Configuration:全局配置类:存储SqlMapConfig.xml配置文件解析出来的内容
package com.mybatis.it.sdk.pojo;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
* 全局配置类:存放核心配置文件解析出来的内容
*/
public class Configuration {
// 数据源对象
private DataSource dataSource;
/**
* 声明一个Map集合
* key:statementId:namespace.id
* MappedStatement: 封装好的MappedStatement对象
*/
private Map<String, MappedStatement> mappedStatementMap = new HashMap<>();
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public Map<String, MappedStatement> getMappedStatementMap() {
return mappedStatementMap;
}
public void setMappedStatementMap(Map<String, MappedStatement> mappedStatementMap) {
this.mappedStatementMap = mappedStatementMap;
}
}
MappedStatement:映射配置类:存储mapper.xml配置文件解析出来的内容
package com.mybatis.it.sdk.pojo;
/**
* 映射配置类:存放mapper.xml解析内容
*/
public class MappedStatement {
// 唯一标识:statementId:namespace.id
private String statementId;
// 返回值类型
private String resultType;
// 参数值类型
private String parameterType;
// SQL语句
private String sql;
// 判断当前是什么操作的一个属性
private String sqlCommandType;
public String getStatementId() {
return statementId;
}
public void setStatementId(String statementId) {
this.statementId = statementId;
}
public String getResultType() {
return resultType;
}
public void setResultType(String resultType) {
this.resultType = resultType;
}
public String getParameterType() {
return parameterType;
}
public void setParameterType(String parameterType) {
this.parameterType = parameterType;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public String getSqlCommandType() {
return sqlCommandType;
}
public void setSqlCommandType(String sqlCommandType) {
this.sqlCommandType = sqlCommandType;
}
}
解析配置文件,填充容器对象。创建SqlSessionFactoryBuilder类
提供方法:build(InputStream stream) 方法:
(1) 解析配置文件(dom4j + xpath),封装Configuration
(2) 创建SqlSessionFactory
package com.mybatis.it.sdk.session;
import com.mybatis.it.sdk.config.XMLConfigBuilder;
import com.mybatis.it.sdk.pojo.Configuration;
import java.io.InputStream;
public class SqlSessionFactoryBuilder {
/**
* 1.解析配置文件,封装容器对象
* 2.创建SqlSessionFactory工厂对象
*
* @param inputStream
* @return
*/
public SqlSessionFactory build(InputStream inputStream) {
// 1. 解析配置文件,封装容器对象 XMLConfigBuilder:专门解析核心配置文件的解析类
XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder();
Configuration configuration = xmlConfigBuilder.parse(inputStream);
// 2. 创建SqlSessionFactory工厂对象
DefaultSqlSessionFactory defaultSqlSessionFactory = new DefaultSqlSessionFactory(configuration);
return defaultSqlSessionFactory;
}
}
创建SqlSessionFactory接口及DefaultSqlSessionFactory实现类
提供方法:SqlSession openSession(); 工厂模式
提供接口类:
package com.mybatis.it.sdk.session;
public interface SqlSessionFactory {
/**
* 1.生产sqlSession对象
* 2. 创建执行器对象
*
* @return
*/
SqlSession openSession();
}
提供实现类:
package com.mybatis.it.sdk.session;
import com.mybatis.it.sdk.executor.Executor;
import com.mybatis.it.sdk.executor.SimpleExecutor;
import com.mybatis.it.sdk.pojo.Configuration;
public class DefaultSqlSessionFactory implements SqlSessionFactory {
private Configuration configuration;
public DefaultSqlSessionFactory(Configuration configuration) {
this.configuration = configuration;
}
@Override
public SqlSession openSession() {
// 1. 创建执行器对象
Executor executor = new SimpleExecutor();
// 2. 生产sqlSession对象
DefaultSqlSession defaultSqlSession = new DefaultSqlSession(configuration, executor);
return defaultSqlSession;
}
}
创建SqlSession接口和DefaultSqlSession实现类:
提供方法:
package com.mybatis.it.sdk.session;
import java.util.List;
public interface SqlSession {
/**
* 查询多个结果
*
* @param statementId
* @param param SQL参数
* @param 元素
* @return
*/
<E> List<E> selectList(String statementId, Object param);
/**
* 查询单个结果
*
* @param statementId
* @param param SQL参数
* @param 类型
* @return
*/
<T> T selectOne(String statementId, Object param);
/**
* 清除资源
*/
void close();
/**
* 生成代理对象
*/
<T> T getMapper(Class<?> mapperClass);
}
DefaultSqlSession实现类:
package com.mybatis.it.sdk.session;
import com.mybatis.it.sdk.executor.Executor;
import com.mybatis.it.sdk.pojo.Configuration;
import com.mybatis.it.sdk.pojo.MappedStatement;
import java.lang.reflect.*;
import java.util.List;
public class DefaultSqlSession implements SqlSession {
private Configuration configuration;
private Executor executor;
public DefaultSqlSession(Configuration configuration, Executor executor) {
this.configuration = configuration;
this.executor = executor;
}
@Override
public <E> List<E> selectList(String statementId, Object param) {
// 将查询操作委托给底层的执行器
// query():执行底层的JDBC操作:1.数据库配置信息 2.SQL配置信息
MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
List<E> list = executor.query(configuration, mappedStatement, param);
return list;
}
@Override
public <T> T selectOne(String statementId, Object param) {
// 去调用selectList方法
List<Object> list = this.selectList(statementId, param);
if (list.size() == 1) {
return (T) list.get(0);
} else if (list.size() > 1) {
throw new RuntimeException("返回结果大于预期");
} else {
return null;
}
}
@Override
public void close() {
executor.close();
}
@Override
public <T> T getMapper(Class<?> mapperClass) {
// 使用JDK动态代理生成基于接口的代理对象
Object proxy = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
/**
*
* @param proxy 代理对象的引用,很少用
* @param method 被调用的方法的字节码对象
* @param args 调用的方法参数
*
* @return
* @throws Throwable
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 具体的逻辑:执行底层的JDBC
// 通过调用sqlSession里面的方法来完成方法调用
// 参数准备:1.statementId 2.param
// 问题1:无法获取现有的statementId
// 规范:接口的全路径要和namespace的值保持一致;接口中的方法名要和id的值保持一致
String methodName = method.getName();
String className = method.getDeclaringClass().getName();
String statementId = className + "." + methodName;
// 方法调用:问题2:要调用sqlSession中增删改查的什么方法?
// 改造当前工程:sqlCommandType:判断当前是什么操作的一个属性
MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
// sqlCommandType取值范围(insert delete update select)
String sqlCommandType = mappedStatement.getSqlCommandType();
switch (sqlCommandType) {
case "select":
// 执行查询方法调用
// 问题3:该调用selectList还是selectOne?
Type genericReturnType = method.getGenericReturnType();
// 判断是否实现了 泛型类型参数化
if (genericReturnType instanceof ParameterizedType) {
// 表示返回结果是带泛型的
if (args != null) {
return selectList(statementId, args[0]);
}
return selectList(statementId, null);
}
if (args != null) {
return selectOne(statementId, args[0]);
}
return selectOne(statementId, null);
case "update":
// 执行更新方法调用
case "delete":
// 执行删除方法调用
case "insert":
// 执行插入方法调用
}
return null;
}
});
return (T) proxy;
}
}
创建Executor接口和实现类SimpleExecutor
提供方法:query(Configuration,MappedStatement,Object parameter); 执行的就是底层JDBC代码(数据库配置信息、SQL配置信息)
package com.mybatis.it.sdk.executor;
import com.mybatis.it.sdk.pojo.Configuration;
import com.mybatis.it.sdk.pojo.MappedStatement;
import java.sql.SQLException;
import java.util.List;
public interface Executor {
<E> List<E> query(Configuration configuration, MappedStatement mappedStatement, Object param);
void close();
}
SimpleExecutor实现类:
package com.mybatis.it.sdk.executor;
import com.mybatis.it.sdk.config.BoundSql;
import com.mybatis.it.sdk.pojo.Configuration;
import com.mybatis.it.sdk.pojo.MappedStatement;
import com.mybatis.it.sdk.utils.GenericTokenParser;
import com.mybatis.it.sdk.utils.ParameterMapping;
import com.mybatis.it.sdk.utils.ParameterMappingTokenHandler;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class SimpleExecutor implements Executor {
private Connection connection = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
@Override
public <E> List<E> query(Configuration configuration, MappedStatement mappedStatement, Object param) {
try {
// 1. 加载驱动,获取数据源连接
connection = configuration.getDataSource().getConnection();
// 2. 获取preparedStatement预编译对象
// 获取要执行的SQL语句
/**
* select * from user_t where id = #{id} and username = #{username}
* 替换成
* select * from user_t where id = ? and username = ?
* 解析替换过程中:自定义占位符#{id}里面的值保存下来
*/
String sql = mappedStatement.getSql();
BoundSql boundSql = getBoundSql(sql);
String finalSql = boundSql.getFinalSql();
preparedStatement = connection.prepareStatement(finalSql);
// 3.设置参数
String parameterType = mappedStatement.getParameterType();
if (parameterType != null) {
Class<?> parameterTypeClass = Class.forName(parameterType);
List<ParameterMapping> parameterMappingList = boundSql.getList();
for (int i = 0; i < parameterMappingList.size(); i++) {
ParameterMapping parameterMapping = parameterMappingList.get(i);
// 值为#{}里面的内容
String paramName = parameterMapping.getContent();
// 反射
Field declaredField = parameterTypeClass.getDeclaredField(paramName);
// 暴力访问
declaredField.setAccessible(true);
Object value = declaredField.get(param);
// 赋值占位符
preparedStatement.setObject(i + 1, value);
}
}
// 4. 执行SQL,发起查询
resultSet = preparedStatement.executeQuery();
// 5. 处理返回结果集
List<E> list = new ArrayList<>();
while (resultSet.next()) {
// 元数据信息 包含了:字段名以及字段的值
ResultSetMetaData metaData = resultSet.getMetaData();
String resultType = mappedStatement.getResultType();
Class<?> resultTypeClass = Class.forName(resultType);
Object object = resultTypeClass.newInstance();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
// 字段名
String columnName = metaData.getColumnName(i);
// 字段值
Object value = resultSet.getObject(columnName);
// 封装
// 属性描述器:通过API方法获取某个属性的读写方法
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(columnName, resultTypeClass);
Method writeMethod = propertyDescriptor.getWriteMethod();
// 参数1:实例对象 参数2:要设置的值
writeMethod.invoke(object, value);
}
list.add((E) object);
}
return list;
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
/**
* 1. 将#{}占位符替换成?
* 2. 解析替换的过程中 将#{}里面保存的值保存下来
*
* @param sql
* @return
*/
private BoundSql getBoundSql(String sql) {
// 1. 创建标记处理器:配合标记解析器完成标记的处理解析工作
ParameterMappingTokenHandler parameterMappingTokenHandler = new ParameterMappingTokenHandler();
// 2. 创建标记解析器
GenericTokenParser genericTokenParser = new GenericTokenParser("#{", "}", parameterMappingTokenHandler);
// #{}占位符替换成? 解析替换过程中 将#{}里面保存的值保存下来ParameterMapping集合中
String finalSql = genericTokenParser.parse(sql);
// #{}里面的值的一个集合
List<ParameterMapping> parameterMappings = parameterMappingTokenHandler.getParameterMappings();
BoundSql boundSql = new BoundSql(finalSql, parameterMappings);
return boundSql;
}
/**
* 释放资源
*/
@Override
public void close() {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
解析配置文件逻辑:
XMLConfigBuilder
package com.mybatis.it.sdk.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.mybatis.it.sdk.io.Resources;
import com.mybatis.it.sdk.pojo.Configuration;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
public class XMLConfigBuilder {
private Configuration configuration;
public XMLConfigBuilder() {
this.configuration = new Configuration();
}
/**
* 使用dom4j+xpath解析配置文件,封装Configuration对象
*
* @param inputStream
* @return
*/
public Configuration parse(InputStream inputStream) {
try {
Document document = new SAXReader().read(inputStream);
Element rootElement = document.getRootElement();
List<Element> list = rootElement.selectNodes("//property");
Properties properties = new Properties();
for (Element element : list) {
String name = element.attributeValue("name");
String value = element.attributeValue("value");
properties.setProperty(name, value);
}
// 创建数据源对象
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(properties.getProperty("driverClassName"));
druidDataSource.setUrl(properties.getProperty("url"));
druidDataSource.setUsername(properties.getProperty("username"));
druidDataSource.setPassword(properties.getProperty("password"));
// 创建好的数据源对象封装到Configuration对象中
configuration.setDataSource(druidDataSource);
/**
* 解析映射配置文件
* 1.获取映射配置文件的路径
* 2.根据路径进行映射配置文件的加载解析
* 3.封装到MappedStatement对象中 --> Configuration里面Map中
*/
// 1.获取映射配置文件的路径
List<Element> mapperList = rootElement.selectNodes("//mapper");
for (Element element : mapperList) {
String mapperPath = element.attributeValue("resource");
InputStream resourceAsStream = Resources.getResourceAsStream(mapperPath);
// 专门解析映射配置文件的对象
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(configuration);
// 2.根据路径进行映射配置文件的加载解析
// 3.封装到MappedStatement对象中 --> Configuration里面Map中
xmlMapperBuilder.parse(resourceAsStream);
}
return configuration;
} catch (DocumentException e) {
throw new RuntimeException(e);
}
}
}
XMLMapperBuilder
package com.mybatis.it.sdk.config;
import com.mybatis.it.sdk.pojo.Configuration;
import com.mybatis.it.sdk.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.InputStream;
import java.util.List;
/**
* parse:解析配置文件 --> mappedStatement --> Configuration里面Map中
*/
public class XMLMapperBuilder {
private Configuration configuration;
public XMLMapperBuilder(Configuration configuration) {
this.configuration = configuration;
}
public void parse(InputStream resourceAsStream) {
try {
Document document = new SAXReader().read(resourceAsStream);
Element rootElement = document.getRootElement();
String namespace = rootElement.attributeValue("namespace");
List<Element> selectList = rootElement.selectNodes("//select");
for (Element element : selectList) {
String id = element.attributeValue("id");
String resultType = element.attributeValue("resultType");
String parameterType = element.attributeValue("parameterType");
String sql = element.getTextTrim();
String statementId = namespace + "." + id;
// 封装MappedStatement对象
MappedStatement mappedStatement = new MappedStatement();
mappedStatement.setStatementId(statementId);
mappedStatement.setParameterType(parameterType);
mappedStatement.setResultType(resultType);
mappedStatement.setSql(sql);
mappedStatement.setSqlCommandType("select");
// 将封装好的MappedStatement封装到Configuration里面Map集合中
this.configuration.getMappedStatementMap().put(statementId, mappedStatement);
}
} catch (DocumentException e) {
throw new RuntimeException(e);
}
}
}
BoundSql
package com.mybatis.it.sdk.config;
import com.mybatis.it.sdk.utils.ParameterMapping;
import java.util.List;
public class BoundSql {
private String finalSql;
private List<ParameterMapping> list;
public BoundSql(String finalSql, List<ParameterMapping> list) {
this.finalSql = finalSql;
this.list = list;
}
public String getFinalSql() {
return finalSql;
}
public void setFinalSql(String finalSql) {
this.finalSql = finalSql;
}
public List<ParameterMapping> getList() {
return list;
}
public void setList(List<ParameterMapping> list) {
this.list = list;
}
}
解析参数工具类:
GenericTokenParser:来自mybatis源码
package com.mybatis.it.sdk.utils;
public class GenericTokenParser {
private final String openToken; //开始标记
private final String closeToken; //结束标记
private final TokenHandler handler; // 标记处理器
public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {
this.openToken = openToken;
this.closeToken = closeToken;
this.handler = handler;
}
/**
* 解析${}和#{}
* 该方法主要实现了配置文件、脚本等片段中占位符的解析、处理工作,并返回最终需要的数据。
* 其中,解析工作由该方法完成,处理工作由处理器handler的handleToken()方法来实现
*
* @param text
* @return
*/
public String parse(String text) {
// 验证参数问题,如果是null,就返回空字符串
if (text == null || text.isEmpty()) {
return "";
}
// search open token
// 下面继续验证是否包含开始标签,如果不包含,默认不是占位符,直接原样返回即可,否则继续执行
int start = text.indexOf(openToken, 0);
if (start == -1) {
return text;
}
// 把text转成字符数组src,并且定义默认偏移量offset=0、存储最终需要返回字符串的变量builder
// text变量中占位符对应的变量名是expression。判断start是否大于-1(即text中是否存在openToken),如果存在就执行
char[] src = text.toCharArray();
int offset = 0;
final StringBuilder builder = new StringBuilder();
StringBuilder expression = null;
while (start > -1) {
// 判断如果开始标记前如果有转义字符,就不作为openToken进行处理,否则继续处理
if (start > 0 && src[start - 1] == '\\') {
// this open token is escaped. remove the backslash and continue.
builder.append(src, offset, start - offset - 1).append(openToken);
offset = start + openToken.length();
} else {
// 重置expression变量,避免空指针或者老数据干扰。
// found open token. let's search close token.
if (expression == null) {
expression = new StringBuilder();
} else {
expression.setLength(0);
}
builder.append(src, offset, start - offset);
offset = start + openToken.length();
int end = text.indexOf(closeToken, offset);
while (end > -1) { // 存在结束标记时
if (end > offset && src[end - 1] == '\\') {// 如果结束标记前面有转义字符时
// this close token is escaped. remove the backslash and continue.
expression.append(src, offset, end - offset - 1).append(closeToken);
offset = end + closeToken.length();
end = text.indexOf(closeToken, offset);
} else {// 不存在转义字符,即需要作为参数进行处理
expression.append(src, offset, end - offset);
offset = end + closeToken.length();
break;
}
}
if (end == -1) {
// close token was not found.
builder.append(src, start, src.length - start);
offset = src.length;
} else {
// 首先根据参数的key(即expression)进行参数处理,返回?作为占位符
builder.append(handler.handleToken(expression.toString()));
offset = end + closeToken.length();
}
}
start = text.indexOf(openToken, offset);
}
if (offset < src.length) {
builder.append(src, offset, src.length - offset);
}
return builder.toString();
}
}
TokenHandler:来自mybatis源码
package com.mybatis.it.sdk.utils;
public interface TokenHandler {
String handleToken(String content);
}
ParameterMapping:
package com.mybatis.it.sdk.utils;
public class ParameterMapping {
// 值为#{}里面的内容:如:id、username
private String content;
public ParameterMapping(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
ParameterMappingTokenHandler:参考mybatis源码
package com.mybatis.it.sdk.utils;
import java.util.ArrayList;
import java.util.List;
public class ParameterMappingTokenHandler implements TokenHandler {
private List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
// content是参数名称 #{id} #{username}
@Override
public String handleToken(String content) {
parameterMappings.add(buildParameterMapping(content));
return "?";
}
private ParameterMapping buildParameterMapping(String content) {
ParameterMapping parameterMapping = new ParameterMapping(content);
return parameterMapping;
}
public List<ParameterMapping> getParameterMappings() {
return parameterMappings;
}
public void setParameterMappings(List<ParameterMapping> parameterMappings) {
this.parameterMappings = parameterMappings;
}
}
对应项目源码资源:https://download.csdn.net/download/liwenyang1992/88615616