秦悦明的运维笔记

spring jdbc数据源

0. 所有jdbc的先决条件

配置数据源,spring里面是配置一个datasource bean。

1. maven依赖

需要导入jdbc包,spring包,h2包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.175</version>
</dependency>

2. 运行h2数据库

可以网上找h2的二进制包运行,也可以直接在idea里面找到org.h2.tools.Console类运行。
用户名sa,密码为空。

3. 创建一个spring bean配置类,使用DriverManagerDataSource定义一个datasource bean

1
2
3
4
5
6
7
8
9
10
11
12
@Configuration
public class Ch4Configuration {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:tcp://localhost/~/test");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
}

DriverManagerDataSource还有一个子类SingleConnectionDataSource,该类重复使用相同的连接。

1
2
3
4
5
6
7
8
9
10
11
12
public class Ch4Configuration {
@Bean
public DataSource dataSource() {
SingleConnectionDataSource dataSource = new SingleConnectionDataSource();
dataSource.setSuppressClose(true);
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:tcp://localhost/~/test");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
}

4. 生产环境下使用连接池化的datasource

比如C3P0,或者DBCP,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
import org.apache.commons.dbcp.BasicDataSource;
@Configuration
public class Ch4ConfigurationForPooledDS1 {
@Bean(destroyMethod="close")
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:tcp://localhost/~/test");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
public class Ch4ConfigurationForPooledDS2 {
@Bean(destroyMethod="close")
public DataSource dataSource() throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("org.h2.Driver");
dataSource.setJdbcUrl("jdbc:h2:tcp://localhost/~/test");
dataSource.setUser("sa");
dataSource.setPassword("");
return dataSource;
}
}

5. 获取connection

1
2
3
4
5
6
7
8
9
10
11
public class Main {
public static void main(String[] args) throws SQLException {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Ch4Configuration.class);
DataSource dataSource = applicationContext.getBean("dataSource", DataSource.class);
Connection connection = dataSource.getConnection();
System.out.println(connection.isClosed());
connection.close();
System.out.println(connection.isClosed());
}
}

所有datasource 都实现了getConnection()接口,直接调用即可。这样我们就通过jdbc 数据源,获取了数据库连接。