pom.xml文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baijie</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MySQL连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 打jar包时如果不配置该插件,打出来的jar包没有清单文件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
</project>
application.properties文件
#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/baijie?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
#JPA Configuration:
spring.jpa.database=MySQL
#日志显示SQL语句
spring.jpa.show-sql=true
#自动更新
spring.jpa.generate-ddl=true
#自动更新
spring.jpa.hibernate.ddl-auto=update
spring.thymeleaf.cache=false
实体类:User
package com.baijie.demo;
import lombok.Data;
import javax.persistence.*;
@Entity
@Data
public class User {
// 主键
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// 用户名
private String username;
// 密码
@Column(name = "password")
private String password;
}
接口:UserRepository
package com.baijie.demo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User,Long> {
}
控制层:TestControl
package com.baijie.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
@Controller
@RequestMapping("/test")
public class TestControl {
@Resource
private UserRepository dao;
@RequestMapping("/list")
public String show(Model model){
List<User> userList = dao.findAll();
model.addAttribute("userList",userList);
return "index";
}
//删除功能
@RequestMapping("/delete")
public String delete(Long id){
System.out.println("删除ID:"+id);
dao.deleteById(id);
return "redirect:list";
}
//跳转添加数据页面
@RequestMapping("/addPage")
public String addPageMethod(){
System.out.println("进入添加页面啦");
return "add";
}
//跳转添加数据页面
@RequestMapping("/update")
public String updatePageMethod(Long id,Model model){
//根据ID,获得user对象
User user = dao.findById(id).get();
model.addAttribute("user",user);
return "update";
}
//保存数据(添加功能+修改功能)
@RequestMapping("/save")
public String saveUser(User user){
System.out.println(user.toString());
dao.save(user);
return "redirect:list";
}
}
index.xml页面
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="margin: auto; width: 500px">
<button><a th:href="@{/test/addPage}">添加</a> </button>
<table border="1">
<tr><th>编号</th><th>姓名</th><th>密码</th><th>操作</th></tr>
<tr th:each="user:${userList}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td><a th:href="@{'/test/delete?id='+${user.id}}">删除</a></td>
<td><a th:href="@{'/test/update?id='+${user.id}}">修改</a></td>
</tr>
</table>
</div>
</body>
</html>
add.html页面:
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/test/save" method="post">
用户名:<input type="text" name="username" value="阿斗"><br>
密码:<input type="password" name="password" value="123456"><br>
<input type="submit" value="添加">
</form>
</body>
</html>
update.html页面:
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/test/save" method="post" th:object="${user}">
编号:<input type="text" name="id" th:value="*{id}" readonly><br>
用户名:<input type="text" name="username" th:value="*{username}"><br>
密码:<input type="password" name="password" th:value="*{password}"><br>
<input type="submit" value="修改">
</form>
</body>
</html>