仿照MyBatis手寫一個持久層框架學習

首先數據準備,創建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 = ?";// 獲取預處理StatementpreparedStatement = 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");// 封裝UserUser 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);}}}}

我們發現使用過程中存在以下問題:

  • 數據庫配置信息硬編碼
  • 頻繁創建釋放數據庫連接,而數據庫連接是寶貴的資源
  • SQL語句、參數、返回結果集獲取 均存在硬編碼問題
  • 需要手動封裝返回結果集,較為繁瑣

手寫持久層框架思路分析

在這里插入圖片描述

創建一個maven項目mybatis-demo,作為使用端,引入自定義持久層框架jar包

在這里插入圖片描述

其中文件內容如下:

package com.mybatis.it.dao;import com.mybatis.it.pojo.User;import java.util.List;public class UserDaoImpl implements IUserDao {@Overridepublic List<User> findAll() {return null;}@Overridepublic 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;}@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +'}';}
}

創建SqlMapConfig.xml配置文件:存放數據庫配置信息、存放mapper.xml路徑

<?xml version="1.0" encoding="utf-8" ?>
<configuration><!-- 1. 配置數據庫信息--><dataSource><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&amp;serverTimezone=UTC"></property><property name="username" value="root"></property><property name="password" value="123456"></property></dataSource><!-- 2. 引入映射配置文件--><mappers><mapper resource="mapper/UserMapper.xml"></mapper></mappers></configuration>

創建mapper.xml配置文件:存放SQL信息、參數類型、返回值類型等

<?xml version="1.0" encoding="UTF-8" ?>
<!-- 唯一標識:namespace.id 取名字叫statementId -->
<mapper namespace="com.mybatis.it.dao.IUserDao"><!--規范:接口的全路徑要和namespace的值保持一致接口中的方法名要和id的值保持一致--><!-- 查詢所有--><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內容如下:

<?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.mybatis.it</groupId><artifactId>mybatis-demo</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><!-- 引入自定義持久層框架的jar包 --><dependency><groupId>com.mybatis.it.sdk</groupId><artifactId>mybatis-sdk</artifactId><version>1.0-SNAPSHOT</version></dependency><!-- 引入單元測試依賴 --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>5.10.1</version><scope>test</scope></dependency></dependencies></project>

同時創建一個maven模塊:mybatis-sdk

在這里插入圖片描述

引入依賴如下(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.mybatis.it.sdk</groupId><artifactId>mybatis-sdk</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><!-- xpath --><dependency><groupId>jaxen</groupId><artifactId>jaxen</artifactId><version>1.1.6</version></dependency><!-- mysql數據庫驅動 --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.2.0</version></dependency><!-- 數據庫連接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.20</version></dependency><!-- 日志 --><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-slf4j-impl</artifactId><version>2.22.0</version><scope>test</scope></dependency><!-- 單元測試 --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>5.10.1</version><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version><scope>provided</scope></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.idprivate 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;}@Overridepublic SqlSession openSession() {// 1. 創建執行器對象Executor executor = new SimpleExecutor();// 2. 生產sqlSession對象DefaultSqlSession defaultSqlSession = new DefaultSqlSession(configuration, executor);return defaultSqlSession;}
}

創建SqlSession接口和DefaultSqlSession實現類:

提供方法:

  • selectList(); 查詢所有
  • selectOne(); 查詢單個
  • update(); 更新
  • delete(); 刪除
  • insert(); 添加
  • close();
  • getMapper();
package com.mybatis.it.sdk.session;import java.util.List;public interface SqlSession {/*** 查詢多個結果** @param statementId* @param param       SQL參數* @param <E>         元素* @return*/<E> List<E> selectList(String statementId, Object param);/*** 查詢單個結果** @param statementId* @param param       SQL參數* @param <T>         類型* @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;}@Overridepublic <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;}@Overridepublic <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;}}@Overridepublic void close() {executor.close();}@Overridepublic <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*/@Overridepublic 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;@Overridepublic <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;}/*** 釋放資源*/@Overridepublic 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<String, MappedStatement>中*/// 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<String, MappedStatement>中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<String, MappedStatement>中*/
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<String, MappedStatement>集合中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、usernameprivate 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}@Overridepublic 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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/213159.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/213159.shtml
英文地址,請注明出處:http://en.pswp.cn/news/213159.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

深入理解Java虛擬機----內存區域的劃分

Java虛擬機在執行Java程序的過程時&#xff0c;會將它管理的內存劃分為若干個不同的數據區域。主要分為以下幾個區域&#xff1a; 程序計數器 當前線程所執行的字節碼的行號指示器。字節碼解釋器工作時通過改變程序計數器來選取下一條需要執行的字節碼指令&#xff0c;分支、循…

nginx中Include使用

1.include介紹 自己的理解&#xff1a;如果學過C語言的話&#xff0c;感覺和C語言中的Include引入是一樣的&#xff0c;引入的文件中可以寫任何東西&#xff0c;比如server相關信息&#xff0c;相當于替換的作用&#xff0c;一般情況下server是寫在nginx.conf配置文件中的&…

VR串流線方案:實現同時充電傳輸視頻信號

VR&#xff08;Virtual Reality&#xff09;&#xff0c;俗稱虛擬現實技術&#xff0c;是一項具有巨大潛力的技術創新&#xff0c;正在以驚人的速度改變我們的生活方式和體驗&#xff0c;利用專門設計的設備&#xff0c;如頭戴式顯示器&#xff08;VR頭盔&#xff09;、手柄、定…

idea 本身快捷鍵ctrl+d復制 無法像eclipse快捷鍵ctrl+alt+上下鍵,自動換行格式問題解決

問題 例如我使用ctrld 想復制如下內容 復制效果如下&#xff0c;沒有自動換行&#xff0c;還需要自己在進行調整 解決 讓如下快捷鍵第一個刪除 修改成如下&#xff0c;將第二個添加ctrld 提示&#xff1a;對應想要修改的item&#xff0c;直接右鍵&#xff0c;remove是刪…

分子生成領域的stable diffusion - GEOLDM

一、關于stable diffusion 很多人都知道stable diffusion&#xff0c;stable diffusion的出現改變了機器生成領域&#xff0c;讓AI技術第一次無比的接近正常人。大語言模型&#xff0c;AIGC概念于是興起。基于stable diffusion 大家開發了lora&#xff0c; hyperwork等微調技術…

[GWCTF 2019]我有一個數據庫1

提示 信息收集phpmyadmin的版本漏洞 這里看起來不像是加密應該是編碼錯誤 這里訪問robots.txt 直接把phpinfo.php放出來了 這里能看到它所有的信息 這里并沒有能找到可控點 用dirsearch掃了一遍 ####注意掃描buuctf的題需要控制掃描速度&#xff0c;每一秒只能掃10個多一個都…

聚類算法的性能度量

聚類算法的性能度量 聚類算法就是根據數據中樣本與樣本之間的距離或相似度&#xff0c;將樣本劃分為若干組&#xff0f;類&#xff0f;簇&#xff0c;其劃分的原則&#xff1a;簇內樣本相似、簇間樣本不相似&#xff0c;聚類的結果是產生一個簇的集合。 其劃分方式主要分為兩…

API接口并發測試:如何測試API接口的最大并發能力?

本文將深入探討API接口并發測試&#xff0c;介紹并比較不同的API并發測試工具&#xff0c;并分享如何有效測量和提高API接口在最大并發情況下的性能。了解如何應對高并發壓力是保證系統穩定性和用戶滿意度的關鍵&#xff0c;讓我們一起來探索這個重要的話題。 隨著互聯網的迅速…

float,flex和grid布局

頁面布局往往會影響著整體的結構與項目的樣式&#xff0c;通常我們用的布局方式有三種&#xff1a;float,flex,grid 1.float或position布局 1.1概念 首先對于一個頁面來說&#xff0c;有浮動流&#xff0c;文檔流&#xff0c;文本流這幾種模式&#xff0c;而float布局則是…

【EI會議征稿中】第六屆下一代數據驅動網絡國際學術會議(NGDN 2024)

第六屆下一代數據驅動網絡國際學術會議&#xff08;NGDN 2024&#xff09; The Sixth International Conference on Next Generation Data-driven Networks 基于前幾屆在英國埃克塞特 (ISPA 2020) 、中國沈陽 (TrustCom 2021) 和中國武漢 (IEEETrustCom-2022)成功舉辦的經驗&a…

若依vue-新建目錄及菜單

前面我們把標題和logo換成了自己系統的標題和logo了 接下來就是要建立自己需要的菜單和頁面 新建目錄解析 在拉下來的代碼跑起來后 有一個系統菜單--菜單管理(如圖) 在這個菜單的這個頁面內有對應的操作功能 修改功能 這個功能可以修改寫好了的菜單數據 例如:名稱/排序/路由…

python:五種算法(DBO、WOA、GWO、PSO、GA)求解23個測試函數(python代碼)

一、五種算法簡介 1、蜣螂優化算法DBO 2、鯨魚優化算法WOA 3、灰狼優化算法GWO 4、粒子群優化算法PSO 5、遺傳算法GA 二、5種算法求解23個函數 &#xff08;1&#xff09;23個函數簡介 參考文獻&#xff1a; [1] Yao X, Liu Y, Lin G M. Evolutionary programming made…

【小白專用】php執行sql腳本 更新23.12.10

可以使用 PHP 的 mysqli 擴展來執行 SQL 腳本。具體步驟如下&#xff1a; 連接到數據庫&#xff1b;打開 SQL 腳本文件并讀取其中的 SQL 語句&#xff1b;逐條執行 SQL 語句&#xff1b;關閉 SQL 腳本文件&#xff1b;關閉數據庫連接。 以下是通過 mysqli 執行 SQL 腳本的示例…

生產問題: 利用線程Thread預加載數據緩存,其它類全局變量獲取緩存偶發加載不到

生產問題: 利用線程Thread預加載數據緩存偶發加載不到 先上代碼 public class ThreadTest {//本地緩存Map<String, Object> map new HashMap<String, Object>();class ThreadA implements Runnable{Overridepublic void run() {System.out.println("Thread…

RT-Thread學習筆記(六):RT_Thread系統死機日志定位

RT_Thread系統死機日志定位 一、RT_Thread系統死機日志定位二、Cortex-M3 / M4架構知識2.1 Cortex-M3 / M4架構概述2.2 寄存器用途 三、排查步驟 一、RT_Thread系統死機日志定位 RT-Thread 系統發生hardfault死機時&#xff0c;系統默認會打印出一系列寄存器狀態幫助用戶定位死…

XML學習及應用

介紹XML語法及應用 1.XML基礎知識1.1什么是XML語言1.2 XML 和 HTML 之間的差異1.3 XML 用途 2.XML語法2.1基礎語法2.2XML元素2.3 XML屬性2.4XML命名空間 3.XML驗證3.1xml語法驗證3.2自定義驗證3.2.1 XML DTD3.2.2 XML Schema3.2.3PCDATA和CDATA區別3.2.4 參考 1.XML基礎知識 1…

AWR1642 boost開發板支持的TI參考設計

打開radar_toolbox_1_30_00_05\source\ti\examples\examples_overview,通過輸入“1642”查找AWR1642 BOOST支持的參考設計,通過篩選,支持AWR1642 BOOST的參考設計如下: 挑選出兩個參考設計上手,一個是“nonos_oob_16xx",不帶OS;另一個是”short range radar“,比較…

Sbatch, Salloc提交任務相關

salloc 申請計算節點&#xff0c;然后登錄到申請到的計算節點上運行指令&#xff1b; salloc的參數與sbatch相同&#xff0c;該部分先介紹一個簡單的使用案例&#xff1b;隨后介紹一個GPU的使用案例&#xff1b;最后介紹一個跨節點使用案例&#xff1b; 首先是一個簡單的例子&a…

基于Java健身房課程管理系統

基于Java健身房課程管理系統 功能需求 1、課程信息管理&#xff1a;系統需要能夠記錄和管理所有課程的詳細信息&#xff0c;包括課程名稱、教練信息、課程時間、課程地點、課程容量等。管理員和教練可以添加、編輯和刪除課程信息。 2、會員信息管理&#xff1a;系統需要能夠…

無服務器監控工具的演變:提高效率和性能

無服務器計算的興起改變了應用程序的構建和部署方式&#xff0c;提供了無與倫比的可擴展性&#xff0c;減少了基礎設施管理并提高了成本效率。隨著無服務器架構變得越來越流行&#xff0c;對有效監控工具的需求對于確保最佳性能和可靠性變得至關重要。 在本文中&#xff0c;我…