mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 06:03:52 +08:00
快速入门案例
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
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 +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
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> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
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/";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
## 是否显示 SQL 语句
|
||||
spring.jpa.show-sql=true
|
||||
@@ -0,0 +1,2 @@
|
||||
/* contentDiv */
|
||||
.contentDiv {padding:20px 60px;}
|
||||
BIN
chapter-2-spring-boot-quick-start/src/main/resources/static/images/favicon.ico
Executable file
BIN
chapter-2-spring-boot-quick-start/src/main/resources/static/images/favicon.ico
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 946 B |
@@ -0,0 +1,55 @@
|
||||
<!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>
|
||||
@@ -0,0 +1,46 @@
|
||||
<!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>
|
||||
Reference in New Issue
Block a user