版本:spingboot2.0.7
参考文章:https://www.cnblogs.com/ityouknow/p/7594801.html
目录结构
application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1/springboot2?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
spring.thymeleaf.cache=false
User.java
package com.zdw.entity;
import javax.persistence.*;
@Entity
@Table
public class User {
@Id
@GeneratedValue
private long id;
@Column(nullable = false, unique = true)
private String userName;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private int age;
public User() {
}
seter geter..
}
UserRepositry.java
@Component(value = "userRepositry")
public interface UserRepositry extends JpaRepository<User, Long> {
User findById(long id);
//Long deleteById(Long id);
}
UserService.java
public interface UserService {
public List<User> getUserList();
public User findUserById(long id);
public void save(User user);
public void edit(User user);
public void delete(long id);
}
UserServiceImpl.java
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserRepositry userRepositry;
public List<User> getUserList() {
return userRepositry.findAll();
}
public User findUserById(long id) {
return userRepositry.findById(id);
}
public void save(User user) {
userRepositry.save(user);
}
public void edit(User user) {
userRepositry.save(user);
}
public void delete(long id) {
userRepositry.deleteById(id);
}
}
HelloController.java
@Controller
public class HelloController {
@RequestMapping(value = "/hello")
public String hello
(Model model, @RequestParam(value = "name",required =
false,defaultValue = "迈克尔杰克逊") String name){
model.addAttribute("name",name);
return "hello";
}
}
图片.png
UserController.java
@Controller
public class UserController {
@Resource
UserService userService;
@RequestMapping("/")
public String index(){
return "redirect:/list";
}
@RequestMapping("list")
public String list(Model model){
List<User> users=userService.getUserList();
model.addAttribute("users",users);
return "user/list";
}
@RequestMapping("/toAdd")
public String toAdd(){
return"user/userAdd";
}
@RequestMapping("/add")
public String add(User user){
userService.save(user);
return "redirect:/list";
}
@RequestMapping("/toEdit")
public String toEdit(Model model,Long id){
User user=userService.findUserById(id);
model.addAttribute("user",user);
return "user/userEdit";
}
@RequestMapping("/edit")
public String edit(User user){
userService.edit(user);
return "redirect:/list";
}
@RequestMapping("/delete")
public String delete(Long id){
userService.delete(id);
return "redirect:/list";
}
}
list.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>user_name</th>
<th>password</th>
<th>age</th>
<th>Edit</th>
<th>delete</th>
</tr>
<tr th:each="user:${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.userName}"></td>
<td th:text="${user.password}"></td>
<td th:text="${user.age}"></td>
<td><a th:href="@{/toEdit(id=${user.id})}">edit</a></td>
<td><a th:href="@{/delete(id=${user.id})}">delete</a></td>
</tr>
</table>
<br>
<a href="/toAdd" th:href="@{/toAdd}">add</a>
</body>
</html>
userEdit.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/edit}" th:object="${user}" method="post">
<input type="hidden" name="id" th:value="*{id}"/><br>
userName:<input type="text" name="userName" th:value="*{userName}"/><br>
age: <input type="text" name="age" th:value="*{age}"/><br>
password:<input type="text" name="password" th:value="*{password}"/><br>
<input type="submit" value="Edit"/><br>
</form>
</body>
</html>
userAdd.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/edit}" th:object="${user}" method="post">
<input type="hidden" name="id" th:value="*{id}"/><br>
userName:<input type="text" name="userName" th:value="*{userName}"/><br>
age: <input type="text" name="age" th:value="*{age}"/><br>
password:<input type="text" name="password" th:value="*{password}"/><br>
<input type="submit" value="Edit"/><br>
</form>
</body>
</html>
图片.png
网友评论