File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change 76
76
<groupId >org.springframework.boot</groupId >
77
77
<artifactId >spring-boot-starter-amqp</artifactId >
78
78
</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 >
79
89
</dependencies >
80
90
81
91
<build >
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ source.driverClassName = com.mysql.jdbc.Driver
2
+ source.url = jdbc:mysql://localhost:3306/blog
3
+ source.username = root
4
+ source.password = 123
You can’t perform that action at this time.
0 commit comments