Skip to content

Commit b27806c

Browse files
zhuzhengping911zhuzhengping911
authored andcommitted
jdbc connect DB
1 parent 3cf95e7 commit b27806c

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@
7676
<groupId>org.springframework.boot</groupId>
7777
<artifactId>spring-boot-starter-amqp</artifactId>
7878
</dependency>
79+
<!--jdbc连接数据库-->
80+
<dependency>
81+
<groupId>org.springframework.boot</groupId>
82+
<artifactId>spring-boot-starter-jdbc</artifactId>
83+
</dependency>
84+
<dependency>
85+
<groupId>com.alibaba</groupId>
86+
<artifactId>druid</artifactId>
87+
<version>1.0.5</version>
88+
</dependency>
7989
</dependencies>
8090

8191
<build>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.common;
2+
3+
import com.alibaba.druid.pool.DruidDataSource;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.PropertySource;
8+
import org.springframework.core.env.Environment;
9+
import org.springframework.jdbc.core.JdbcTemplate;
10+
import org.springframework.transaction.annotation.EnableTransactionManagement;
11+
12+
import javax.sql.DataSource;
13+
14+
/**
15+
* Created by zhuzhengping on 2017/3/7.
16+
*/
17+
@Configuration
18+
@EnableTransactionManagement
19+
@PropertySource(value = {"classpath:config/DBsource.properties"})
20+
public class DBBeanConfig {
21+
22+
@Autowired
23+
private Environment env;
24+
25+
@Bean(destroyMethod = "close")
26+
public DataSource dataSource(){
27+
DruidDataSource dataSource = new DruidDataSource();
28+
dataSource.setDriverClassName(env.getProperty("source.driverClassName").trim());
29+
dataSource.setUrl(env.getProperty("source.url").trim());
30+
dataSource.setUsername(env.getProperty("source.username").trim());
31+
dataSource.setPassword(env.getProperty("source.password").trim());
32+
return dataSource;
33+
}
34+
35+
@Bean
36+
public JdbcTemplate jdbcTemplate(){
37+
JdbcTemplate jdbcTemplate = new JdbcTemplate();
38+
jdbcTemplate.setDataSource(dataSource());
39+
return jdbcTemplate;
40+
}
41+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source.driverClassName = com.mysql.jdbc.Driver
2+
source.url = jdbc:mysql://localhost:3306/blog
3+
source.username = root
4+
source.password = 123

0 commit comments

Comments
 (0)