mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 06:03:52 +08:00
文档
This commit is contained in:
76
README.md
76
README.md
@@ -2,20 +2,27 @@
|
|||||||
spring boot 实践学习案例,是 spring boot 初学者及核心技术巩固的最佳实践。
|
spring boot 实践学习案例,是 spring boot 初学者及核心技术巩固的最佳实践。
|
||||||
|
|
||||||
# 推荐
|
# 推荐
|
||||||
- [高性能 TCP/UDP 通信框架 HP-Socket](https://github.com/ldcsaa/HP-Socket)
|
由于工作原因,我发表原创博客也需要编写的时间。期间很多人私我问题,没有及时得到回复,这里说声抱歉。因此,建一个星球
|
||||||
- springcloud-learning-example
|
|
||||||
[spring cloud 实践学习案例](https://github.com/JeffLi1993/springcloud-learning-example)<br>
|
|
||||||
https://github.com/JeffLi1993/springcloud-learning-example
|
|
||||||
|
|
||||||
|
「泥瓦匠BYSocket」和朋友们讨论技术相关的话题,你一起来吧?
|
||||||
|
|
||||||
|
[泥瓦匠BYSocket 星球](https://t.zsxq.com/nMzjqbe "泥瓦匠BYSocket")
|
||||||
|
|
||||||
|
[泥瓦匠BYSocket 星球](https://t.zsxq.com/nMzjqbe "泥瓦匠BYSocket")
|
||||||
|
|
||||||
|
[泥瓦匠BYSocket 星球](https://t.zsxq.com/nMzjqbe "泥瓦匠BYSocket")
|
||||||
|
|
||||||
|
这个星球里面讲深入讨论 Java 、Java EE、Spring Boot 等框架技术及工作经验交流,分享一路技术走来的经验、教训。硬实力软实力、择业、职业规划等。
|
||||||
|
|
||||||
|
## 计划
|
||||||
|
Spring Boot 2.0 最大的热点肯定是 Web Flux。我这边也就开始操刀了...
|
||||||
|
|
||||||
|
Web Flux 努力进行中,尽情期待。唯一文章入口:[GitChat文章地址](http://gitbook.cn/gitchat/author/58968d35f2b669527d7a7c57 "gitchat")
|
||||||
|
|
||||||
## Spring For All 社区
|
## Spring For All 社区
|
||||||
[Spring For All 社区](http://www.spring4all.com/ "spring4all")是新组建的关于 Spring 的纯技术交流社区(涵盖 Spring Boot、Spring Cloud 等内容),集诸多开源爱好者和技术大牛贡献内容和交流问题。我们不夸大、不装逼、做最纯粹的技术分享!!!
|
[Spring For All 社区](http://www.spring4all.com/ "spring4all")是新组建的关于 Spring 的纯技术交流社区(涵盖 Spring Boot、Spring Cloud 等内容),集诸多开源爱好者和技术大牛贡献内容和交流问题。我们不夸大、不装逼、做最纯粹的技术分享!!!
|
||||||
|
|
||||||
看看我们超强的群众基础,欢迎有兴趣的朋友加入QQ群分享与交流:
|
看看我们超强的群众基础,欢迎有兴趣的朋友加入QQ群分享与交流:[点击立即加入](http://www.spring4all.com/about "about")
|
||||||
Spring For All 社区 ⑪ 517395240 <br>
|
|
||||||
Spring For All 社区 ⑫ 498098401 <br>
|
|
||||||
|
|
||||||
博主微信:139-5868-6678
|
|
||||||
|
|
||||||
## 作者与学习乐园
|
## 作者与学习乐园
|
||||||
源码地址:我的[GitHub地址](https://github.com/JeffLi1993 "GitHub")、[OSCGit地址](https://git.oschina.net/jeff1993/springboot-learning-example "OSCGit")<br>
|
源码地址:我的[GitHub地址](https://github.com/JeffLi1993 "GitHub")、[OSCGit地址](https://git.oschina.net/jeff1993/springboot-learning-example "OSCGit")<br>
|
||||||
@@ -23,7 +30,6 @@ Spring For All 社区 ⑫ 498098401 <br>
|
|||||||
关注微信公众号【泥瓦匠BYSokcet】,及时得到技术文章推送<br>
|
关注微信公众号【泥瓦匠BYSokcet】,及时得到技术文章推送<br>
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
## 一、项目结构
|
## 一、项目结构
|
||||||
「Spring Boot 那些事」:[传送门](http://www.bysocket.com/?page_id=1639 "Spring Boot 那些事")<br>
|
「Spring Boot 那些事」:[传送门](http://www.bysocket.com/?page_id=1639 "Spring Boot 那些事")<br>
|
||||||
|
|
||||||
@@ -74,56 +80,6 @@ Dubbo 服务提供者工程和 Dubbo 服务消费者工程 <br>
|
|||||||
- spring-data-elasticsearch-query <br>
|
- spring-data-elasticsearch-query <br>
|
||||||
spring-data-elasticsearch - 实战案例详解
|
spring-data-elasticsearch - 实战案例详解
|
||||||
|
|
||||||
## 二、项目 Quick Start 快速开发指南
|
|
||||||
#### a. 基本环境配置
|
|
||||||
在 MySQL 中,创建数据库 springbootdb:
|
|
||||||
````
|
|
||||||
CREATE DATABASE springbootdb;
|
|
||||||
````
|
|
||||||
创建表 city 城市 (因为我喜欢徒步)
|
|
||||||
````
|
|
||||||
DROP TABLE IF EXISTS `city`;
|
|
||||||
CREATE TABLE `city` (
|
|
||||||
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市编号',
|
|
||||||
`province_id` int(10) unsigned NOT NULL COMMENT '省份编号',
|
|
||||||
`city_name` varchar(25) DEFAULT NULL COMMENT '城市名称',
|
|
||||||
`description` varchar(25) DEFAULT NULL COMMENT '描述',
|
|
||||||
PRIMARY KEY (`id`)
|
|
||||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
|
|
||||||
````
|
|
||||||
插入基础数据
|
|
||||||
````
|
|
||||||
INSERT city VALUES (1 ,1,'温岭市','BYSocket 的家在温岭。');
|
|
||||||
````
|
|
||||||
<br>
|
|
||||||
|
|
||||||
### 《 springboot-mybatis 工程 Quick Start 》
|
|
||||||
首先 check 基本环境配置完成,创建好数据库和表。
|
|
||||||
|
|
||||||
#### 1. 修改数据库配置
|
|
||||||
配置文件地址:springboot-mybatis/src/main/resources/application.properties
|
|
||||||
修改相应的数据源配置,比如账号、密码等
|
|
||||||
|
|
||||||
#### 2. 编译工程
|
|
||||||
在项目根目录 `springboot-learning-example`,运行 maven 指令:
|
|
||||||
````
|
|
||||||
mvn clean install
|
|
||||||
````
|
|
||||||
#### 3. 运行工程
|
|
||||||
右键运行工程包中 `org.spring.springboot.Application` Spring Boot 应用启动类的 main 函数,然后在浏览器访问:
|
|
||||||
`````
|
|
||||||
http://localhost:8080/api/city?cityName=温岭市
|
|
||||||
`````
|
|
||||||
可以看到返回的 JSON 结果:
|
|
||||||
````
|
|
||||||
{
|
|
||||||
"id": 1,
|
|
||||||
"provinceId": 1,
|
|
||||||
"cityName": "温岭市",
|
|
||||||
"description": "我的家在温岭。"
|
|
||||||
}
|
|
||||||
````
|
|
||||||
最后,<br/>
|
最后,<br/>
|
||||||
|
|
||||||
推荐
|
|
||||||
[《Spring Boot教程与Spring Cloud教程》](https://git.oschina.net/didispace/SpringBoot-Learning "Spring Boot教程与Spring Cloud教程")<br>
|
[《Spring Boot教程与Spring Cloud教程》](https://git.oschina.net/didispace/SpringBoot-Learning "Spring Boot教程与Spring Cloud教程")<br>
|
||||||
|
|||||||
@@ -1,84 +0,0 @@
|
|||||||
<?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>spring.boot.core</groupId>
|
|
||||||
<artifactId>chapter-2-spring-boot-quick-start</artifactId>
|
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<name>chapter-2-spring-boot-quick-start</name>
|
|
||||||
<description>第二章快速入门案例</description>
|
|
||||||
|
|
||||||
<parent>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
|
||||||
<version>2.0.0.M4</version>
|
|
||||||
</parent>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
|
||||||
<java.version>1.8</java.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
|
|
||||||
<!-- Web 依赖 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- 单元测试依赖 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Spring Data JPA 依赖 :: 数据持久层框架 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- h2 数据源连接驱动 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.h2database</groupId>
|
|
||||||
<artifactId>h2</artifactId>
|
|
||||||
<scope>runtime</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- 模板引擎 Thymeleaf 依赖 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<!-- Spring Boot Maven 插件 -->
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
|
||||||
<version>1.5.1.RELEASE</version>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<repositories>
|
|
||||||
<repository>
|
|
||||||
<id>spring-milestones</id>
|
|
||||||
<name>Spring Milestones</name>
|
|
||||||
<url>https://repo.spring.io/libs-milestone</url>
|
|
||||||
<snapshots>
|
|
||||||
<enabled>false</enabled>
|
|
||||||
</snapshots>
|
|
||||||
</repository>
|
|
||||||
</repositories>
|
|
||||||
|
|
||||||
</project>
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
package spring.boot.core;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
public class QuickStartApplication {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(QuickStartApplication.class, args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
package spring.boot.core.domain;
|
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户实体类
|
|
||||||
*
|
|
||||||
* Created by bysocket on 21/07/2017.
|
|
||||||
*/
|
|
||||||
@Entity
|
|
||||||
public class User implements Serializable {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 编号
|
|
||||||
*/
|
|
||||||
@Id
|
|
||||||
@GeneratedValue
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 名称
|
|
||||||
*/
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 年龄
|
|
||||||
*/
|
|
||||||
private Integer age;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 出生时间
|
|
||||||
*/
|
|
||||||
private String birthday;
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getAge() {
|
|
||||||
return age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAge(Integer age) {
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getBirthday() {
|
|
||||||
return birthday;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBirthday(String birthday) {
|
|
||||||
this.birthday = birthday;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "User{" +
|
|
||||||
"id=" + id +
|
|
||||||
", name='" + name + '\'' +
|
|
||||||
", age=" + age +
|
|
||||||
", birthday=" + birthday +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
package spring.boot.core.domain;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户持久层操作接口
|
|
||||||
*
|
|
||||||
* Created by bysocket on 21/07/2017.
|
|
||||||
*/
|
|
||||||
public interface UserRepository extends JpaRepository<User, Long> {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package spring.boot.core.service;
|
|
||||||
|
|
||||||
|
|
||||||
import spring.boot.core.domain.User;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* User 业务层接口
|
|
||||||
*
|
|
||||||
* Created by bysocket on 24/07/2017.
|
|
||||||
*/
|
|
||||||
public interface UserService {
|
|
||||||
|
|
||||||
List<User> findAll();
|
|
||||||
|
|
||||||
User insertByUser(User user);
|
|
||||||
|
|
||||||
User update(User user);
|
|
||||||
|
|
||||||
User delete(Long id);
|
|
||||||
|
|
||||||
User findById(Long id);
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
package spring.boot.core.service.impl;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import spring.boot.core.domain.User;
|
|
||||||
import spring.boot.core.domain.UserRepository;
|
|
||||||
import spring.boot.core.service.UserService;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* User 业务层实现
|
|
||||||
*
|
|
||||||
* Created by bysocket on 24/07/2017.
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class UserServiceImpl implements UserService {
|
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
UserRepository userRepository;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<User> findAll() {
|
|
||||||
return userRepository.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public User insertByUser(User user) {
|
|
||||||
LOGGER.info("新增用户:" + user.toString());
|
|
||||||
return userRepository.save(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public User update(User user) {
|
|
||||||
LOGGER.info("更新用户:" + user.toString());
|
|
||||||
return userRepository.save(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public User delete(Long id) {
|
|
||||||
User user = userRepository.findById(id).get();
|
|
||||||
userRepository.delete(user);
|
|
||||||
|
|
||||||
LOGGER.info("删除用户:" + user.toString());
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public User findById(Long id) {
|
|
||||||
LOGGER.info("获取用户 ID :" + id);
|
|
||||||
return userRepository.findById(id).get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
package spring.boot.core.web;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.ui.ModelMap;
|
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import spring.boot.core.domain.User;
|
|
||||||
import spring.boot.core.service.UserService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户控制层
|
|
||||||
*
|
|
||||||
* Created by bysocket on 24/07/2017.
|
|
||||||
*/
|
|
||||||
@Controller
|
|
||||||
@RequestMapping(value = "/users") // 通过这里配置使下面的映射都在 /users
|
|
||||||
public class UserController {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
UserService userService; // 用户服务层
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取用户列表
|
|
||||||
* 处理 "/users" 的 GET 请求,用来获取用户列表
|
|
||||||
* 通过 @RequestParam 传递参数,进一步实现条件查询或者分页查询
|
|
||||||
*/
|
|
||||||
@RequestMapping(method = RequestMethod.GET)
|
|
||||||
public String getUserList(ModelMap map) {
|
|
||||||
map.addAttribute("userList", userService.findAll());
|
|
||||||
return "userList";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 显示创建用户表单
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@RequestMapping(value = "/create", method = RequestMethod.GET)
|
|
||||||
public String createUserForm(ModelMap map) {
|
|
||||||
map.addAttribute("user", new User());
|
|
||||||
map.addAttribute("action", "create");
|
|
||||||
return "userForm";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建用户
|
|
||||||
* 处理 "/users" 的 POST 请求,用来获取用户列表
|
|
||||||
* 通过 @ModelAttribute 绑定参数,也通过 @RequestParam 从页面中传递参数
|
|
||||||
*/
|
|
||||||
@RequestMapping(value = "/create", method = RequestMethod.POST)
|
|
||||||
public String postUser(@ModelAttribute User user) {
|
|
||||||
userService.insertByUser(user);
|
|
||||||
return "redirect:/users/";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 显示需要更新用户表单
|
|
||||||
* 处理 "/users/{id}" 的 GET 请求,通过 URL 中的 id 值获取 User 信息
|
|
||||||
* URL 中的 id ,通过 @PathVariable 绑定参数
|
|
||||||
*/
|
|
||||||
@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
|
|
||||||
public String getUser(@PathVariable Long id, ModelMap map) {
|
|
||||||
map.addAttribute("user", userService.findById(id));
|
|
||||||
map.addAttribute("action", "update");
|
|
||||||
return "userForm";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理 "/users/{id}" 的 PUT 请求,用来更新 User 信息
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@RequestMapping(value = "/update", method = RequestMethod.POST)
|
|
||||||
public String putUser(@ModelAttribute User user) {
|
|
||||||
userService.update(user);
|
|
||||||
return "redirect:/users/";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理 "/users/{id}" 的 GET 请求,用来删除 User 信息
|
|
||||||
*/
|
|
||||||
@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
|
|
||||||
public String deleteUser(@PathVariable Long id) {
|
|
||||||
|
|
||||||
userService.delete(id);
|
|
||||||
return "redirect:/users/";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
## 是否显示 SQL 语句
|
|
||||||
spring.jpa.show-sql=true
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
/* contentDiv */
|
|
||||||
.contentDiv {padding:20px 60px;}
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 946 B |
@@ -1,55 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="zh-CN">
|
|
||||||
<head>
|
|
||||||
<script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}"></script>
|
|
||||||
<link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>
|
|
||||||
<link th:href="@{/css/default.css}" rel="stylesheet"/>
|
|
||||||
<link rel="icon" th:href="@{/images/favicon.ico}" type="image/x-icon"/>
|
|
||||||
<meta charset="UTF-8"/>
|
|
||||||
<title>用户管理</title>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<div class="contentDiv">
|
|
||||||
|
|
||||||
<h5> 《 Spring Boot 2.x 核心技术实战》第二章快速入门案例</h5>
|
|
||||||
|
|
||||||
<legend>
|
|
||||||
<strong>用户管理</strong>
|
|
||||||
</legend>
|
|
||||||
|
|
||||||
<form th:action="@{/users/{action}(action=${action})}" method="post" class="form-horizontal">
|
|
||||||
|
|
||||||
<input type="hidden" name="id" th:value="${user.id}"/>
|
|
||||||
|
|
||||||
<div class="form-group">
|
|
||||||
<label for="user_name" class="col-sm-2 control-label">名称</label>
|
|
||||||
<div class="col-xs-4">
|
|
||||||
<input type="text" class="form-control" id="user_name" name="name" th:value="${user.name}" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="form-group">
|
|
||||||
<label for="user_age" class="col-sm-2 control-label">年龄:</label>
|
|
||||||
<div class="col-xs-4">
|
|
||||||
<input type="text" class="form-control" id="user_age" name="age" th:value="${user.age}"/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="form-group">
|
|
||||||
<label for="user_birthday" class="col-sm-2 control-label">出生日期:</label>
|
|
||||||
<div class="col-xs-4">
|
|
||||||
<input type="date" class="form-control" id="user_birthday" name="birthday" th:value="${user.birthday}"/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="form-group">
|
|
||||||
<div class="col-sm-offset-2 col-sm-10">
|
|
||||||
<input class="btn btn-primary" type="submit" value="提交"/>
|
|
||||||
<input class="btn" type="button" value="返回" onclick="history.back()"/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="zh-CN">
|
|
||||||
<head>
|
|
||||||
<script type="text/javascript" th:src="@{https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js}"></script>
|
|
||||||
<link th:href="@{https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css}" rel="stylesheet"/>
|
|
||||||
<link th:href="@{/css/default.css}" rel="stylesheet"/>
|
|
||||||
<link rel="icon" th:href="@{/images/favicon.ico}" type="image/x-icon"/>
|
|
||||||
<meta charset="UTF-8"/>
|
|
||||||
<title>用户列表</title>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<div class="contentDiv">
|
|
||||||
|
|
||||||
<h5> 《 Spring Boot 2.x 核心技术实战》第二章快速入门案例</h5>
|
|
||||||
|
|
||||||
<table class="table table-hover table-condensed">
|
|
||||||
<legend>
|
|
||||||
<strong>用户列表</strong>
|
|
||||||
</legend>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>用户编号</th>
|
|
||||||
<th>名称</th>
|
|
||||||
<th>年龄</th>
|
|
||||||
<th>出生时间</th>
|
|
||||||
<th>管理</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
<tr th:each="user : ${userList}">
|
|
||||||
<th scope="row" th:text="${user.id}"></th>
|
|
||||||
<td><a th:href="@{/users/update/{userId}(userId=${user.id})}" th:text="${user.name}"></a></td>
|
|
||||||
<td th:text="${user.age}"></td>
|
|
||||||
<td th:text="${user.birthday}"></td>
|
|
||||||
<td><a class="btn btn-danger" th:href="@{/users/delete/{userId}(userId=${user.id})}">删除</a></td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<div><a class="btn btn-primary" href="/users/create" role="button">创建用户</a></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package spring.boot.core;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest
|
|
||||||
public class QuickStartApplicationTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void contextLoads() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
package spring.boot.core.domain;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
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.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest
|
|
||||||
public class UserRepositoryTests {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
UserRepository userRepository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 单元测试 - 新增用户
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testSave() {
|
|
||||||
User user = new User();
|
|
||||||
user.setName("mumu");
|
|
||||||
user.setAge(2);
|
|
||||||
// user.setBirthday(new Date());
|
|
||||||
user = userRepository.save(user);
|
|
||||||
|
|
||||||
// 验证新增用户
|
|
||||||
Assert.assertNotNull(user.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 单元测试 - 删除用户
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testDelete() {
|
|
||||||
|
|
||||||
// 新增两个用户数据
|
|
||||||
User mumu = new User();
|
|
||||||
mumu.setName("mumu");
|
|
||||||
mumu.setAge(2);
|
|
||||||
// mumu.setBirthday(new Date());
|
|
||||||
userRepository.save(mumu);
|
|
||||||
|
|
||||||
User zizi = new User();
|
|
||||||
zizi.setName("zizi");
|
|
||||||
zizi.setAge(25);
|
|
||||||
// zizi.setBirthday(new Date());
|
|
||||||
userRepository.save(zizi);
|
|
||||||
|
|
||||||
// 验证是否获取的用户列表大小是 2
|
|
||||||
Assert.assertEquals(2, userRepository.findAll().size());
|
|
||||||
|
|
||||||
// 删除用户
|
|
||||||
userRepository.delete(mumu);
|
|
||||||
|
|
||||||
// 验证是否获取的用户列表大小是 1
|
|
||||||
Assert.assertEquals(1, userRepository.findAll().size());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 单元测试 - 更新用户
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testUpdate() {
|
|
||||||
User user = new User();
|
|
||||||
user.setName("mumu");
|
|
||||||
user.setAge(2);
|
|
||||||
// user.setBirthday(new Date());
|
|
||||||
user = userRepository.save(user);
|
|
||||||
|
|
||||||
user.setName("zizi");
|
|
||||||
user = userRepository.save(user);
|
|
||||||
|
|
||||||
// 验证新增用户的编号是否为 1
|
|
||||||
Assert.assertNotNull(user.getId());
|
|
||||||
Assert.assertEquals("zizi", user.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 单元测试 - 获取用户列表
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testFindAll() {
|
|
||||||
// 新增两个用户数据
|
|
||||||
User mumu = new User();
|
|
||||||
mumu.setName("mumu");
|
|
||||||
mumu.setAge(2);
|
|
||||||
// mumu.setBirthday(new Date());
|
|
||||||
userRepository.save(mumu);
|
|
||||||
|
|
||||||
User zizi = new User();
|
|
||||||
zizi.setName("zizi");
|
|
||||||
zizi.setAge(25);
|
|
||||||
// zizi.setBirthday(new Date());
|
|
||||||
userRepository.save(zizi);
|
|
||||||
|
|
||||||
// 验证是否获取的用户列表大小是 2
|
|
||||||
Assert.assertEquals(2, userRepository.findAll().size());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 单元测试 - 获取单个用户
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testFindById() {
|
|
||||||
// 新增用户
|
|
||||||
User mumu = new User();
|
|
||||||
mumu.setName("mumu");
|
|
||||||
mumu.setAge(2);
|
|
||||||
// mumu.setBirthday(new Date());
|
|
||||||
userRepository.save(mumu);
|
|
||||||
|
|
||||||
// 验证是否获取的用户是否是插入的用户
|
|
||||||
User expected = userRepository.findById(1L).get();
|
|
||||||
Assert.assertEquals("mumu", expected.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user