Skip to content

Commit b61b8be

Browse files
author
曾梓健
committed
Druid配置
1 parent 940f1e2 commit b61b8be

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

springboot-dataaccess/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
<groupId>org.springframework.boot</groupId>
3737
<artifactId>spring-boot-starter-jdbc</artifactId>
3838
</dependency>
39+
<dependency>
40+
<groupId>com.alibaba</groupId>
41+
<artifactId>druid-spring-boot-starter</artifactId>
42+
<version>1.1.0</version>
43+
</dependency>
3944
</dependencies>
4045

4146
<build>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.hand.springbootdataaccess.config;
2+
3+
import javax.sql.DataSource;
4+
5+
import com.alibaba.druid.pool.DruidDataSource;
6+
import com.hand.springbootdataaccess.pojo.SimpleBean;
7+
import org.springframework.boot.context.properties.ConfigurationProperties;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
/**
12+
* @author zijian.zeng@hand-china.com
13+
* @since 2023-01-29
14+
*/
15+
@Configuration
16+
public class DruidConfig {
17+
18+
19+
@Bean
20+
@ConfigurationProperties(prefix = "spring.datasource")
21+
public DataSource dataSource() {
22+
return new DruidDataSource();
23+
}
24+
25+
@Bean
26+
@ConfigurationProperties(prefix = "spring.simple")
27+
public SimpleBean simpleBean() {
28+
return new SimpleBean();
29+
}
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.hand.springbootdataaccess.pojo;
2+
3+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4+
5+
/**
6+
* @author zijian.zeng@hand-china.com
7+
* @since 2023-01-29
8+
*/
9+
public class SimpleBean {
10+
11+
private String name;
12+
private String age;
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public String getAge() {
23+
return age;
24+
}
25+
26+
public void setAge(String age) {
27+
this.age = age;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "SimpleBean{" +
33+
"name='" + name + '\'' +
34+
", age='" + age + '\'' +
35+
'}';
36+
}
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
spring:
2+
datasource:
3+
username: root
4+
password: 123456
5+
url: jdbc:mysql://127.0.0.1:3306/sqlfather?useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true
6+
driver-class-name: com.mysql.cj.jdbc.Driver
7+
initialization-mode: always
8+
type: com.alibaba.druid.pool.DruidDataSource
9+
initialSize: 5
10+
minIdle: 5
11+
maxActive: 20
12+
maxWait: 60000
13+
timeBetweenEvictionRunsMillis: 60000
14+
simple:
15+
name: zzj
16+
age: 24

springboot-dataaccess/src/test/java/com/hand/springbootdataaccess/SpringbootDataaccessApplicationTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.hand.springbootdataaccess;
22

33
import java.sql.Connection;
4-
import java.sql.DatabaseMetaData;
54
import java.sql.ResultSet;
65
import java.sql.SQLException;
76
import java.sql.Statement;
87
import javax.sql.DataSource;
98

9+
import com.hand.springbootdataaccess.pojo.SimpleBean;
1010
import org.junit.jupiter.api.Test;
1111
import org.junit.runner.RunWith;
1212
import org.springframework.beans.factory.annotation.Autowired;
@@ -19,10 +19,15 @@ class SpringbootDataaccessApplicationTests {
1919

2020
@Autowired
2121
private DataSource dataSource;
22+
23+
@Autowired
24+
private SimpleBean simpleBean;
25+
2226
@Test
2327
void contextLoads() throws SQLException {
2428
Connection connection = dataSource.getConnection();
2529
System.out.println("connection:------" + connection);
30+
simpleBean.getAge();
2631
Statement statement = connection.createStatement();
2732
ResultSet resultSet = statement.executeQuery("select * from report");
2833
resultSet.next();

0 commit comments

Comments
 (0)