MyBatis手寫框架(二)- 加載配置及創(chuàng)建數(shù)據(jù)源

mybatis

描述

在《MyBatis手寫框架(一)》中,我們提到原始 JDBC的幾個(gè)問題,現(xiàn)在我們先來解決硬編碼配置信息和打開單獨(dú)連接的問題。

   public void testSelectUser() {
       
       Connection connection = null;
       PreparedStatement preparedStatement = null;
       ResultSet rs = null;

       try {
           // 加載數(shù)據(jù)庫驅(qū)動(dòng)
           Class.forName("com.mysql.jdbc.Driver");

           // 通過驅(qū)動(dòng)管理類獲取數(shù)據(jù)庫鏈接connection = DriverManager
           connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tester?characterEncoding=utf-8",
                   "root", "123456");
                     ...
         }
 }
  • 配置信息硬編碼
  • 單獨(dú)打開 Connection,資源消耗較大

實(shí)現(xiàn)

對(duì)上面的問題進(jìn)行優(yōu)化,我們需要做以下幾步:

  1. 將配置信息移至配置文件
  2. 使用數(shù)據(jù)源,數(shù)據(jù)源有連接池功能,這里我們使用比較簡單的 BasicDataSource
  3. 重構(gòu)代碼

創(chuàng)建配置文件

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/tester?characterEncoding=utf-8
db.username=root
db.password=123456

添加 BasicDataSource 依賴

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.edugroup</groupId>
  <artifactId>mybatis-source-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
    <dependencies>
        <!-- junit單元測試依賴 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--<dependency>
            <groupId>com.kkb</groupId>
            <artifactId>mybatis-framework-16</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>-->
        <!-- mysql依賴 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>

        <!-- DOM4J依賴 -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <!-- dbcp連接池依賴 -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- OGNL表達(dá)式的依賴 -->
        <dependency>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
            <version>2.7.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>  
  
</project>

重構(gòu)代碼

package com.edugroup;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
import com.edugroup.pojo.UserInfo;

public class MyBatisV1 {
    
    Properties props = new Properties();
    BasicDataSource dataSource = new BasicDataSource();
    
    public MyBatisV1() throws IOException {
        //加載配置信息
        loadProperties("conf/db.properties");
        //創(chuàng)建數(shù)據(jù)源
        createDataSource();
    }
    
    /**
     * 創(chuàng)建數(shù)據(jù)源
     */
    private void createDataSource() {
        // 解決連接獲取時(shí)的硬編碼問題和頻繁連接的問題
        dataSource.setDriverClassName(props.getProperty("db.driver"));
        dataSource.setUrl(props.getProperty("db.url"));
        dataSource.setUsername(props.getProperty("db.username"));
        dataSource.setPassword(props.getProperty("db.password"));       
    }

    
    /**
       *   加載配置文件信息
     * @param location
     * @throws IOException
     */
    private void loadProperties(String location) throws IOException {
        InputStream inStream = null;
        try {
            inStream = this.getClass().getClassLoader().getResourceAsStream(location);
            props.load(inStream);
        } catch (IOException ex) {
            ex.printStackTrace();
            throw ex;
        } finally {
            if(inStream != null) {
                inStream.close();
            }
        }
    }

...

}
碼字不易,感謝點(diǎn)贊打賞
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容