我记得自己第一次连接数据库的时候,接触到的是一个词语: 数据源。
到底什么是数据源呢? 看网络的解释:
从代码层面解释:
它是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();
}
}
运行程序: