MyBatis連接數(shù)據(jù)庫配置的基本步驟和機(jī)制
前言
MyBatis 是一個(gè)流行的持久層框架,它通過使用XML或注解的方式將SQL語句、存儲過程和Java方法進(jìn)行綁定,從而避免了手寫大量的JDBC代碼和手動(dòng)設(shè)置參數(shù)與結(jié)果集。以下是 MyBatis 連接數(shù)據(jù)庫的基本步驟和機(jī)制:
MyBatis源碼版本:3.5.16
MyBatis-config.xml 示例
<environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${db.driver}"/> <property name="url" value="${db.url}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </dataSource> </environment> </environments>
1. 讀取配置文件
MyBatis 通過 Resources
類讀取配置文件。配置文件通常是 XML 格式,如 mybatis-config.xml
。
關(guān)鍵代碼
String resource = "mybatis-config.xml"; Reader reader = Resources.getResourceAsReader(resource)
2. 解析配置文件
SqlSessionFactoryBuilder
使用 XMLConfigBuilder
解析配置文件,將 XML 配置轉(zhuǎn)換為 Java 對象。
關(guān)鍵代碼
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
XMLConfigBuilder 源碼片段
private void parseConfiguration(XNode root) { try { // issue #117 先讀取屬性 propertiesElement(root.evalNode("properties")); Properties settings = settingsAsProperties(root.evalNode("settings")); loadCustomVfsImpl(settings); loadCustomLogImpl(settings); typeAliasesElement(root.evalNode("typeAliases")); pluginsElement(root.evalNode("plugins")); objectFactoryElement(root.evalNode("objectFactory")); objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); reflectorFactoryElement(root.evalNode("reflectorFactory")); settingsElement(settings); // read it after objectFactory and objectWrapperFactory issue #631 environmentsElement(root.evalNode("environments")); databaseIdProviderElement(root.evalNode("databaseIdProvider")); typeHandlersElement(root.evalNode("typeHandlers")); mappersElement(root.evalNode("mappers")); } catch (Exception e) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); } }
XMLConfigBuilder
解析XML配置,并生成Configuration
對象。
3. 創(chuàng)建 SqlSessionFactory
Configuration
對象包含了所有配置信息,包括環(huán)境配置、數(shù)據(jù)源配置等。SqlSessionFactoryBuilder
使用 Configuration
創(chuàng)建 DefaultSqlSessionFactory
。
關(guān)鍵代碼
public SqlSessionFactory build(Configuration config) { return new DefaultSqlSessionFactory(config); }
4. 建立數(shù)據(jù)庫連接
DefaultSqlSessionFactory
創(chuàng)建 SqlSession
時(shí),通過 Environment
配置和 DataSource
獲取數(shù)據(jù)庫連接。
DefaultSqlSessionFactory 源碼片段
public SqlSession openSession() { return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false); }
openSessionFromDataSource
方法中,DataSource
被用于獲取數(shù)據(jù)庫連接。
關(guān)鍵代碼
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { //環(huán)境配置 final Environment environment = configuration.getEnvironment(); //事務(wù)工廠 final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); //創(chuàng)建事務(wù) Transaction tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); //創(chuàng)建執(zhí)行器 final Executor executor = configuration.newExecutor(tx, execType); //返回SqlSession return new DefaultSqlSession(configuration, executor, autoCommit); }
5. 數(shù)據(jù)源配置
DataSource
配置在 mybatis-config.xml
文件中,通過 DataSourceFactory
創(chuàng)建具體的數(shù)據(jù)源實(shí)例。
關(guān)鍵代碼
// 獲取數(shù)據(jù)源工廠 String type = context.getStringAttribute("type"); Properties props = context.getChildrenAsProperties(); DataSourceFactory factory = (DataSourceFactory) resolveClass(type).getDeclaredConstructor().newInstance(); factory.setProperties(props); // 獲取數(shù)據(jù)源 DataSource dataSource = factory.getDataSource();
總結(jié)
MyBatis連接數(shù)據(jù)庫的步驟包括:
- 讀取配置文件:通過
Resources
類讀取 XML 文件。 - 解析配置文件:使用
XMLConfigBuilder
解析 XML,生成Configuration
對象。 - 創(chuàng)建 SqlSessionFactory:使用
SqlSessionFactoryBuilder
和Configuration
創(chuàng)建DefaultSqlSessionFactory
。 - 建立數(shù)據(jù)庫連接:
DefaultSqlSessionFactory
創(chuàng)建SqlSession
,使用DataSource
獲取數(shù)據(jù)庫連接。
以上就是MyBatis連接數(shù)據(jù)庫配置的基本步驟和機(jī)制的詳細(xì)內(nèi)容,更多關(guān)于MyBatis連接數(shù)據(jù)庫配置的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot Application注解原理及代碼詳解
這篇文章主要介紹了SpringBoot Application注解原理及代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Java 模擬數(shù)據(jù)庫連接池的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java 模擬數(shù)據(jù)庫連接池的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Java實(shí)現(xiàn)調(diào)用外部程序的示例代碼
本文主要介紹了Java實(shí)現(xiàn)調(diào)用外部程序的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Springboot如何基于assembly服務(wù)化實(shí)現(xiàn)打包
這篇文章主要介紹了Springboot如何基于assembly服務(wù)化實(shí)現(xiàn)打包,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Spring Batch讀取txt文件并寫入數(shù)據(jù)庫的方法教程
這篇文章主要給大家介紹了Spring Batch讀取txt文件并寫入數(shù)據(jù)庫的方法,SpringBatch 是一個(gè)輕量級、全面的批處理框架。這里我們用它來實(shí)現(xiàn)文件的讀取并將讀取的結(jié)果作處理,處理之后再寫入數(shù)據(jù)庫中的功能。需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04