forked from dyc87112/SpringBoot-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
Chapter3-4-1/src/main/java/com/didispace/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
|
||
|
||
} |
38 changes: 38 additions & 0 deletions
38
Chapter3-4-1/src/main/java/com/didispace/service/UserServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
48 changes: 48 additions & 0 deletions
48
Chapter3-4-1/src/test/java/com/didispace/ApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
|
||
} | ||
|
||
|
||
} |