使用Flyway来管理数据库版本

This commit is contained in:
zhaiyongchao
2017-12-25 18:11:31 +08:00
parent 6758d4d405
commit d12659b80b
7 changed files with 209 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.didispace</groupId>
<artifactId>Chapter3-4-1</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Chapter3-4-1</name>
<description>Spring Boot with JDBCTemplate</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
+13
View File
@@ -0,0 +1,13 @@
package com.didispace;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@@ -0,0 +1,35 @@
package com.didispace.service;
/**
* @author 程序猿DD
* @version 1.0.0
* @date 16/3/17 下午7:04.
* @blog http://blog.didispace.com
*/
public interface UserService {
/**
* 新增一个用户
* @param name
* @param age
*/
void create(String name, Integer age);
/**
* 根据name删除一个用户高
* @param name
*/
void deleteByName(String name);
/**
* 获取用户总量
*/
Integer getAllUsers();
/**
* 删除所有用户
*/
void deleteAllUsers();
}
@@ -0,0 +1,38 @@
package com.didispace.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
/**
* @author 程序猿DD
* @version 1.0.0
* @date 16/3/17 下午6:44.
* @blog http://blog.didispace.com
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void create(String name, Integer age) {
jdbcTemplate.update("insert into t_user(NAME, AGE) values(?, ?)", name, age);
}
@Override
public void deleteByName(String name) {
jdbcTemplate.update("delete from t_user where NAME = ?", name);
}
@Override
public Integer getAllUsers() {
return jdbcTemplate.queryForObject("select count(1) from t_user", Integer.class);
}
@Override
public void deleteAllUsers() {
jdbcTemplate.update("delete from t_user");
}
}
@@ -0,0 +1,6 @@
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
flyway.locations=classpath:/db
@@ -0,0 +1,9 @@
DROP TABLE IF EXISTS t_user ;
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(20) NOT NULL COMMENT '姓名',
`age` int(5) DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
@@ -0,0 +1,48 @@
package com.didispace;
import com.didispace.service.UserService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ApplicationTests {
@Autowired
private UserService userSerivce;
@Before
public void setUp() {
// 准备,清空user表
userSerivce.deleteAllUsers();
}
@Test
public void test() throws Exception {
// 插入5个用户
userSerivce.create("a", 1);
userSerivce.create("b", 2);
userSerivce.create("c", 3);
userSerivce.create("d", 4);
userSerivce.create("e", 5);
// 查数据库,应该有5个用户
Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
// 删除两个用户
userSerivce.deleteByName("a");
userSerivce.deleteByName("e");
// 查数据库,应该有5个用户
Assert.assertEquals(3, userSerivce.getAllUsers().intValue());
}
}