1. 程式人生 > >spring逐步整合orm例項--第二例:整合帶有資料來源的資料庫連線。

spring逐步整合orm例項--第二例:整合帶有資料來源的資料庫連線。

   我記得自己第一次連線資料庫的時候,接觸到的是一個詞語: 資料來源。

   到底什麼是資料來源呢? 看網路的解釋:

   從程式碼層面解釋:

   

   它是jdbc2.0 標準新增的。  位於jdbc官方包下。  

   因為它是一個標準,所以自然需要實現。

   除了spring外,比較知名的資料來源實現有:  dbcp資料來源,c3p0資料來源實現。  通過去meaven倉庫檢視,可以看到這樣的一些依賴關係:

  

   可見啊,阿里巴巴的druid資料來源是基於dbcp的資料來源實現,spring-jdbc預設是採用c3p0的資料來源實現。   值得一提的是,因為它們都是遵循jdbc標準的,所以可以無縫替換。

  如:dbcp的資料來源實現:

 

   要使用這個資料來源實現,需要匯入包: 由apache提供。  從它的依賴也可以看出。

   但是我們經常聽到的是 c3p0連線池。  那麼這個連線池到底是個什麼呢?  依然是看網上的解釋:

   通俗一點說來就是,它幫我們管理連線關係。  我們要注意到這點,資料來源是一個標準,它只提供連線相關的操作。它有兩個getConnection方法,引數分別是使用者名稱,密碼,與無參。  但是我們我們平常注入的資料來源都是具有四個屬性的。  正如上個例子的實現一樣。  只不過上一個例子是我們手工維護連線關係,這裡是它自動維護。 我想這也是連線池,的池字的原因吧。  上個例子中,我們並沒有用到資料來源,仍然是基於jdbc標準操作了資料庫。

   說了很多,看看這一節的例子吧:

package com.automannn.springZhenhe.template;


import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author [email protected]
 * @time 2018/10/16 11:28
 */
public class MyTemplate {
    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void insert(String sql) throws SQLException {
        Connection conn = this.dataSource.getConnection();
        Statement stmt = conn.createStatement();
        stmt.executeUpdate(sql);
        stmt.close();
        conn.close();
    }
}
package com.automannn.springZhenhe.dao;

import com.automannn.springZhenhe.template.MyTemplate;

/**
 * @author [email protected]
 * @time 2018/10/16 11:30
 */
public class MyTemplatePersonDao extends MyTemplate {

    public void savePerson() throws Exception{
        this.insert("insert into person(pid,pname) values(4,'bbb')");
    }
}

此時上下文的依賴關係:

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.4.0</version>
        </dependency>
</dependencies>

仍然是,我們將這個bean放到spring容器中:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--
        引入properties配置檔案
     -->

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>

    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="myTemplatePersonDao" class="com.automannn.springZhenhe.dao.MyTemplatePersonDao">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>
</beans>

   需要注意一點的是,我們注入資料來源的時候,不再是注入某個介面,而是資料來源的實現類,這裡使用的是dbcp的實現類。

  它的入口測試實現類:

package com.automannn.springZhenhe;

import com.automannn.springZhenhe.dao.MyBatisPersonDao;
import com.automannn.springZhenhe.dao.MyHibernatePersonDao;
import com.automannn.springZhenhe.dao.MyOwnPersonInfo;
import com.automannn.springZhenhe.dao.MyTemplatePersonDao;
import com.automannn.springZhenhe.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author [email protected]
 * @time 2018/10/16 11:31
 */
public class App {

    public static void main(String[] args) throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
        MyTemplatePersonDao myTemplatePersonDao = (MyTemplatePersonDao)context.getBean("myTemplatePersonDao");
        myTemplatePersonDao.savePerson();


    }



}

  執行程式: