接上一篇:[JAVA EE] JPA技术基础:完成数据列表显示

本章完成数据列表的删除

  • 修改 UserController.java
package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
public class UserController {
   
    //@Autowired:自动注入,即对象只需声明,不用new就能使用(Spring IoC技术体现,厉害呀!)
    @Autowired
    UserRepository userRepository;

    @RequestMapping("/")
    public String Index(){
   
        return "redirect:/list";//redirect:请求转发,将请求转发到list
    }

    @RequestMapping("/list")
    public String list(Model model){
   
        //Repository内置的方法,可直接使用,查找所有对象
        List<User> users = userRepository.findAll();
        model.addAttribute("users",users);
        return "user/list";//list.html 显示所有 user 信息
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable Long id){
   
        userRepository.deleteById(id);
        return "redirect:/list";
    }
}
  • 添加依赖
  • pom.xml
<!-- jquery 库-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
<!-- bootstrap 库-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<!-- webjars-locator用于库的版本控制 -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<version>0.31</version>
</dependency>
  • 添加 list.html
  • 在 templates 目录下新建 user 目录,并添加 list.html 页面

    list.html

WebJars是将客户端(浏览器)资源(JavaScript,Css等)打成jar包文件,以对资源进行统一依赖管理。
WebJars的jar包部署在Maven中央仓库上。
我们在开发Java web项目的时候会使用像Maven,Gradle等构建工具以实现对jar包版本依赖管理,以及项目的自动化管理,但是对于JavaScript,Css等前端资源包,我们只能采用拷贝到webapp目录下的手工方式,这样做就无法对这些资源进行依赖管理。而且容易导致文件混乱、版本不一致等问题。那么WebJars就提供给我们这些前端资源的jar包形式,我们就可以进行依赖管理。
WebJars是将这些通用的Web前端资源打包成Java的Jar包,然后借助Maven工具对其管理,保证这些Web资源版本唯一性,升级也比较容易。关于webjars资源,有一个专门的网站http://www.webjars.org/,我们可以到这个网站上找到自己需要的资源,在自己的工程中添加入maven依赖,即可直接使用这些资源了。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>
    <!--使用webjar技术来添加第三库-->
    <script th:src="@{/webjars/jquery/jquery.min.js}"></script>
    <script th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"></script>
    <link th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" rel="stylesheet">
</head>
<body>
<div class="container">
    <h2>用户列表</h2>
    <div class="width:80%">
        <div style="margin:20px;">
            <a href="/add" th:herf="@{/add}" class="btn btn-info">添加用户</a>
        </div>
        <table class="table table-hover">
            <tr>
                <th>用户id</th>
                <th>用户名</th>
                <th>密码</th>
                <th>创建时间</th>
                <th>用户状态</th>
                <th>操作</th>
                <th>操作</th>
            </tr>
            <tr th:each="user:${users}">
                <th scope="row" th:text="${user.id}">1</th>
                <td th:text="${user.username}">neo</td>
                <td th:text="${user.password}">123456</td>
                <td th:text="${#dates.format(user.regdate,'yyyy/MM/dd HH:mm:ss')}">2020/11/11</td>
                <td th:text="${user.status}">0</td>
                <td><a th:href="@{/edit/{id}(id=${user.id})}">编辑</a></td>
                <td><a th:href="@{/delete/{id}(id=${user.id})}" th:onclick="return confirm('确定删除吗?')">删除</a></td>
            </tr>
        </table>
    </div>
</div>
</body>
</html>

运行结果:
删除前:


删除中

删除后:

刷新数据库: