Skip to content

Commit d12659b

Browse files
committed
使用Flyway来管理数据库版本
1 parent 6758d4d commit d12659b

File tree

7 files changed

+209
-0
lines changed

7 files changed

+209
-0
lines changed

Chapter3-4-1/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
<groupId>com.didispace</groupId>
7+
<artifactId>Chapter3-4-1</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter3-4-1</name>
12+
<description>Spring Boot with JDBCTemplate</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.9.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-test</artifactId>
30+
<scope>test</scope>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>mysql</groupId>
35+
<artifactId>mysql-connector-java</artifactId>
36+
<version>5.1.21</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-jdbc</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.flywaydb</groupId>
46+
<artifactId>flyway-core</artifactId>
47+
<version>5.0.3</version>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.didispace.service;
2+
3+
/**
4+
* @author 程序猿DD
5+
* @version 1.0.0
6+
* @date 16/3/17 下午7:04.
7+
* @blog http://blog.didispace.com
8+
*/
9+
public interface UserService {
10+
11+
/**
12+
* 新增一个用户
13+
* @param name
14+
* @param age
15+
*/
16+
void create(String name, Integer age);
17+
18+
/**
19+
* 根据name删除一个用户高
20+
* @param name
21+
*/
22+
void deleteByName(String name);
23+
24+
/**
25+
* 获取用户总量
26+
*/
27+
Integer getAllUsers();
28+
29+
/**
30+
* 删除所有用户
31+
*/
32+
void deleteAllUsers();
33+
34+
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.didispace.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.jdbc.core.JdbcTemplate;
5+
import org.springframework.stereotype.Service;
6+
7+
/**
8+
* @author 程序猿DD
9+
* @version 1.0.0
10+
* @date 16/3/17 下午6:44.
11+
* @blog http://blog.didispace.com
12+
*/
13+
@Service
14+
public class UserServiceImpl implements UserService {
15+
16+
@Autowired
17+
private JdbcTemplate jdbcTemplate;
18+
19+
@Override
20+
public void create(String name, Integer age) {
21+
jdbcTemplate.update("insert into t_user(NAME, AGE) values(?, ?)", name, age);
22+
}
23+
24+
@Override
25+
public void deleteByName(String name) {
26+
jdbcTemplate.update("delete from t_user where NAME = ?", name);
27+
}
28+
29+
@Override
30+
public Integer getAllUsers() {
31+
return jdbcTemplate.queryForObject("select count(1) from t_user", Integer.class);
32+
}
33+
34+
@Override
35+
public void deleteAllUsers() {
36+
jdbcTemplate.update("delete from t_user");
37+
}
38+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring.datasource.url=jdbc:mysql://localhost:3306/test
2+
spring.datasource.username=root
3+
spring.datasource.password=
4+
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
5+
6+
flyway.locations=classpath:/db
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DROP TABLE IF EXISTS t_user ;
2+
3+
4+
CREATE TABLE `t_user` (
5+
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
6+
`name` varchar(20) NOT NULL COMMENT '姓名',
7+
`age` int(5) DEFAULT NULL COMMENT '年龄',
8+
PRIMARY KEY (`id`)
9+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.didispace;
2+
3+
import com.didispace.service.UserService;
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.test.context.junit4.SpringJUnit4ClassRunner;
11+
12+
13+
@RunWith(SpringJUnit4ClassRunner.class)
14+
@SpringBootTest
15+
public class ApplicationTests {
16+
17+
@Autowired
18+
private UserService userSerivce;
19+
20+
@Before
21+
public void setUp() {
22+
// 准备,清空user表
23+
userSerivce.deleteAllUsers();
24+
}
25+
26+
@Test
27+
public void test() throws Exception {
28+
// 插入5个用户
29+
userSerivce.create("a", 1);
30+
userSerivce.create("b", 2);
31+
userSerivce.create("c", 3);
32+
userSerivce.create("d", 4);
33+
userSerivce.create("e", 5);
34+
35+
// 查数据库,应该有5个用户
36+
Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
37+
38+
// 删除两个用户
39+
userSerivce.deleteByName("a");
40+
userSerivce.deleteByName("e");
41+
42+
// 查数据库,应该有5个用户
43+
Assert.assertEquals(3, userSerivce.getAllUsers().intValue());
44+
45+
}
46+
47+
48+
}

0 commit comments

Comments
 (0)