Skip to content

Commit 9f71244

Browse files
committed
Spring Boot 2.x基础教程:使用国产数据库连接池Druid
1 parent e4ef2fe commit 9f71244

File tree

9 files changed

+374
-0
lines changed

9 files changed

+374
-0
lines changed

2.1.x/chapter3-3/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

2.1.x/chapter3-3/pom.xml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.3.RELEASE</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
13+
<groupId>com.didispace</groupId>
14+
<artifactId>chapter3-3</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-jdbc</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.alibaba</groupId>
34+
<artifactId>druid-spring-boot-starter</artifactId>
35+
<version>1.1.21</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-actuator</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>mysql</groupId>
45+
<artifactId>mysql-connector-java</artifactId>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.projectlombok</groupId>
50+
<artifactId>lombok</artifactId>
51+
<scope>provided</scope>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-test</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
61+
<build>
62+
<plugins>
63+
<plugin>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-maven-plugin</artifactId>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
70+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.didispace.chapter33;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@SpringBootApplication
9+
public class Chapter33Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Chapter33Application.class, args);
13+
}
14+
15+
@RestController
16+
static class TextController {
17+
18+
@GetMapping("/hello")
19+
public String hello() {
20+
return "hello world";
21+
}
22+
23+
}
24+
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.didispace.chapter33;
2+
3+
import lombok.Data;
4+
import lombok.NoArgsConstructor;
5+
6+
@Data
7+
@NoArgsConstructor
8+
public class User {
9+
10+
private String name;
11+
private Integer age;
12+
13+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.didispace.chapter33;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
import java.util.List;
8+
9+
/**
10+
* Created by 程序猿DD/翟永超 on 2020/2/8.
11+
* <p>
12+
* Blog: http://blog.didispace.com/
13+
* Github: https://github.com/dyc87112/
14+
*/
15+
@Data
16+
@AllArgsConstructor
17+
@RestController
18+
public class UserController {
19+
20+
private UserService userService;
21+
22+
@PostMapping("/user")
23+
public int create(@RequestBody User user) {
24+
return userService.create(user.getName(), user.getAge());
25+
}
26+
27+
@GetMapping("/user/{name}")
28+
public List<User> getByName(@PathVariable String name) {
29+
return userService.getByName(name);
30+
}
31+
32+
@DeleteMapping("/user/{name}")
33+
public int deleteByName(@PathVariable String name) {
34+
return userService.deleteByName(name);
35+
}
36+
37+
@GetMapping("/user/count")
38+
public int getAllUsers() {
39+
return userService.getAllUsers();
40+
}
41+
42+
@DeleteMapping("/user/all")
43+
public int deleteAllUsers() {
44+
return userService.deleteAllUsers();
45+
}
46+
47+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.didispace.chapter33;
2+
3+
import java.util.List;
4+
5+
public interface UserService {
6+
7+
/**
8+
* 新增一个用户
9+
*
10+
* @param name
11+
* @param age
12+
*/
13+
int create(String name, Integer age);
14+
15+
/**
16+
* 根据name查询用户
17+
*
18+
* @param name
19+
* @return
20+
*/
21+
List<User> getByName(String name);
22+
23+
/**
24+
* 根据name删除用户
25+
*
26+
* @param name
27+
*/
28+
int deleteByName(String name);
29+
30+
/**
31+
* 获取用户总量
32+
*/
33+
int getAllUsers();
34+
35+
/**
36+
* 删除所有用户
37+
*/
38+
int deleteAllUsers();
39+
40+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.didispace.chapter33;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.jdbc.core.JdbcTemplate;
5+
import org.springframework.jdbc.core.RowMapper;
6+
import org.springframework.stereotype.Service;
7+
8+
import java.sql.ResultSet;
9+
import java.sql.SQLException;
10+
import java.util.List;
11+
12+
@Service
13+
public class UserServiceImpl implements UserService {
14+
15+
private JdbcTemplate jdbcTemplate;
16+
17+
UserServiceImpl(JdbcTemplate jdbcTemplate) {
18+
this.jdbcTemplate = jdbcTemplate;
19+
}
20+
21+
@Override
22+
public int create(String name, Integer age) {
23+
return jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age);
24+
}
25+
26+
@Override
27+
public List<User> getByName(String name) {
28+
List<User> users = jdbcTemplate.query("select NAME, AGE from USER where NAME = ?", (resultSet, i) -> {
29+
User user = new User();
30+
user.setName(resultSet.getString("NAME"));
31+
user.setAge(resultSet.getInt("AGE"));
32+
return user;
33+
}, name);
34+
return users;
35+
}
36+
37+
@Override
38+
public int deleteByName(String name) {
39+
return jdbcTemplate.update("delete from USER where NAME = ?", name);
40+
}
41+
42+
@Override
43+
public int getAllUsers() {
44+
return jdbcTemplate.queryForObject("select count(1) from USER", Integer.class);
45+
}
46+
47+
@Override
48+
public int deleteAllUsers() {
49+
return jdbcTemplate.update("delete from USER");
50+
}
51+
52+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
# 基础配置
3+
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test
4+
spring.datasource.druid.username=root
5+
spring.datasource.druid.password=
6+
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
7+
8+
# 连接池配置
9+
spring.datasource.druid.initialSize=10
10+
spring.datasource.druid.maxActive=20
11+
spring.datasource.druid.maxWait=60000
12+
spring.datasource.druid.minIdle=1
13+
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
14+
spring.datasource.druid.minEvictableIdleTimeMillis=300000
15+
spring.datasource.druid.testWhileIdle=true
16+
spring.datasource.druid.testOnBorrow=true
17+
spring.datasource.druid.testOnReturn=false
18+
spring.datasource.druid.poolPreparedStatements=true
19+
spring.datasource.druid.maxOpenPreparedStatements=20
20+
spring.datasource.druid.validationQuery=SELECT 1
21+
spring.datasource.druid.validation-query-timeout=500
22+
spring.datasource.druid.filters=stat,wall
23+
24+
# 监控配置
25+
spring.datasource.druid.stat-view-servlet.enabled=true
26+
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
27+
spring.datasource.druid.stat-view-servlet.reset-enable=true
28+
spring.datasource.druid.stat-view-servlet.login-username=admin
29+
spring.datasource.druid.stat-view-servlet.login-password=admin
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.didispace.chapter33;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.http.MediaType;
11+
import org.springframework.test.annotation.Rollback;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
import org.springframework.test.web.servlet.MockMvc;
14+
import org.springframework.test.web.servlet.RequestBuilder;
15+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
16+
import org.springframework.transaction.annotation.Transactional;
17+
18+
import javax.sql.DataSource;
19+
import java.util.List;
20+
21+
import static org.hamcrest.Matchers.equalTo;
22+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
23+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
24+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
25+
26+
@Slf4j
27+
@RunWith(SpringRunner.class)
28+
@SpringBootTest
29+
public class Chapter33ApplicationTests {
30+
31+
@Autowired
32+
private UserService userSerivce;
33+
34+
@Autowired
35+
private DataSource dataSource;
36+
37+
@Before
38+
public void setUp() {
39+
// 准备,清空user表
40+
userSerivce.deleteAllUsers();
41+
42+
}
43+
44+
@Test
45+
public void test() throws Exception {
46+
// 插入5个用户
47+
userSerivce.create("Tom", 10);
48+
userSerivce.create("Mike", 11);
49+
userSerivce.create("Didispace", 30);
50+
userSerivce.create("Oscar", 21);
51+
userSerivce.create("Linda", 17);
52+
53+
// 查询名为Oscar的用户,判断年龄是否匹配
54+
List<User> userList = userSerivce.getByName("Oscar");
55+
Assert.assertEquals(21, userList.get(0).getAge().intValue());
56+
57+
// 查数据库,应该有5个用户
58+
Assert.assertEquals(5, userSerivce.getAllUsers());
59+
60+
// 删除两个用户
61+
userSerivce.deleteByName("Tom");
62+
userSerivce.deleteByName("Mike");
63+
64+
// 查数据库,应该有5个用户
65+
Assert.assertEquals(3, userSerivce.getAllUsers());
66+
67+
}
68+
69+
}

0 commit comments

Comments
 (0)