From d12659b80b35bd7daec9fa230391ed472bdaa0a8 Mon Sep 17 00:00:00 2001 From: zhaiyongchao Date: Mon, 25 Dec 2017 18:11:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8Flyway=E6=9D=A5=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=95=B0=E6=8D=AE=E5=BA=93=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chapter3-4-1/pom.xml | 60 +++++++++++++++++++ .../main/java/com/didispace/Application.java | 13 ++++ .../com/didispace/service/UserService.java | 35 +++++++++++ .../didispace/service/UserServiceImpl.java | 38 ++++++++++++ .../src/main/resources/application.properties | 6 ++ .../main/resources/db/V1__Base_version.sql | 9 +++ .../java/com/didispace/ApplicationTests.java | 48 +++++++++++++++ 7 files changed, 209 insertions(+) create mode 100644 Chapter3-4-1/pom.xml create mode 100755 Chapter3-4-1/src/main/java/com/didispace/Application.java create mode 100644 Chapter3-4-1/src/main/java/com/didispace/service/UserService.java create mode 100644 Chapter3-4-1/src/main/java/com/didispace/service/UserServiceImpl.java create mode 100644 Chapter3-4-1/src/main/resources/application.properties create mode 100644 Chapter3-4-1/src/main/resources/db/V1__Base_version.sql create mode 100755 Chapter3-4-1/src/test/java/com/didispace/ApplicationTests.java diff --git a/Chapter3-4-1/pom.xml b/Chapter3-4-1/pom.xml new file mode 100644 index 0000000..61970b6 --- /dev/null +++ b/Chapter3-4-1/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + com.didispace + Chapter3-4-1 + 1.0.0 + jar + + Chapter3-4-1 + Spring Boot with JDBCTemplate + + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + mysql + mysql-connector-java + 5.1.21 + + + + org.springframework.boot + spring-boot-starter-jdbc + + + + org.flywaydb + flyway-core + 5.0.3 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter3-4-1/src/main/java/com/didispace/Application.java b/Chapter3-4-1/src/main/java/com/didispace/Application.java new file mode 100755 index 0000000..2a14b75 --- /dev/null +++ b/Chapter3-4-1/src/main/java/com/didispace/Application.java @@ -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); + } + +} diff --git a/Chapter3-4-1/src/main/java/com/didispace/service/UserService.java b/Chapter3-4-1/src/main/java/com/didispace/service/UserService.java new file mode 100644 index 0000000..620a443 --- /dev/null +++ b/Chapter3-4-1/src/main/java/com/didispace/service/UserService.java @@ -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(); + + +} diff --git a/Chapter3-4-1/src/main/java/com/didispace/service/UserServiceImpl.java b/Chapter3-4-1/src/main/java/com/didispace/service/UserServiceImpl.java new file mode 100644 index 0000000..0deeb24 --- /dev/null +++ b/Chapter3-4-1/src/main/java/com/didispace/service/UserServiceImpl.java @@ -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"); + } +} diff --git a/Chapter3-4-1/src/main/resources/application.properties b/Chapter3-4-1/src/main/resources/application.properties new file mode 100644 index 0000000..39713b5 --- /dev/null +++ b/Chapter3-4-1/src/main/resources/application.properties @@ -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 \ No newline at end of file diff --git a/Chapter3-4-1/src/main/resources/db/V1__Base_version.sql b/Chapter3-4-1/src/main/resources/db/V1__Base_version.sql new file mode 100644 index 0000000..939de12 --- /dev/null +++ b/Chapter3-4-1/src/main/resources/db/V1__Base_version.sql @@ -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; diff --git a/Chapter3-4-1/src/test/java/com/didispace/ApplicationTests.java b/Chapter3-4-1/src/test/java/com/didispace/ApplicationTests.java new file mode 100755 index 0000000..ec6f898 --- /dev/null +++ b/Chapter3-4-1/src/test/java/com/didispace/ApplicationTests.java @@ -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()); + + } + + +}