File tree Expand file tree Collapse file tree 5 files changed +94
-1
lines changed
java/com/hand/springbootdataaccess
test/java/com/hand/springbootdataaccess Expand file tree Collapse file tree 5 files changed +94
-1
lines changed Original file line number Diff line number Diff line change 36
36
<groupId >org.springframework.boot</groupId >
37
37
<artifactId >spring-boot-starter-jdbc</artifactId >
38
38
</dependency >
39
+ <dependency >
40
+ <groupId >com.alibaba</groupId >
41
+ <artifactId >druid-spring-boot-starter</artifactId >
42
+ <version >1.1.0</version >
43
+ </dependency >
39
44
</dependencies >
40
45
41
46
<build >
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 1
1
package com .hand .springbootdataaccess ;
2
2
3
3
import java .sql .Connection ;
4
- import java .sql .DatabaseMetaData ;
5
4
import java .sql .ResultSet ;
6
5
import java .sql .SQLException ;
7
6
import java .sql .Statement ;
8
7
import javax .sql .DataSource ;
9
8
9
+ import com .hand .springbootdataaccess .pojo .SimpleBean ;
10
10
import org .junit .jupiter .api .Test ;
11
11
import org .junit .runner .RunWith ;
12
12
import org .springframework .beans .factory .annotation .Autowired ;
@@ -19,10 +19,15 @@ class SpringbootDataaccessApplicationTests {
19
19
20
20
@ Autowired
21
21
private DataSource dataSource ;
22
+
23
+ @ Autowired
24
+ private SimpleBean simpleBean ;
25
+
22
26
@ Test
23
27
void contextLoads () throws SQLException {
24
28
Connection connection = dataSource .getConnection ();
25
29
System .out .println ("connection:------" + connection );
30
+ simpleBean .getAge ();
26
31
Statement statement = connection .createStatement ();
27
32
ResultSet resultSet = statement .executeQuery ("select * from report" );
28
33
resultSet .next ();
You can’t perform that action at this time.
0 commit comments