[JAVA EE] JPA技术基础:完成数据列表的删除
接上一篇:[JAVA EE] JPA技術(shù)基礎(chǔ):完成數(shù)據(jù)列表顯示
本章完成數(shù)據(jù)列表的刪除
- 修改 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:自動(dòng)注入,即對(duì)象只需聲明,不用new就能使用(Spring IoC技術(shù)體現(xiàn),厲害呀!)@AutowiredUserRepository userRepository;@RequestMapping("/")public String Index(){return "redirect:/list";//redirect:請(qǐng)求轉(zhuǎn)發(fā),將請(qǐng)求轉(zhuǎn)發(fā)到list}@RequestMapping("/list")public String list(Model model){//Repository內(nèi)置的方法,可直接使用,查找所有對(duì)象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 庫(kù)-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
<!-- bootstrap 庫(kù)-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<!-- webjars-locator用于庫(kù)的版本控制 -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<version>0.31</version>
</dependency>
- 添加 list.html
- 在 templates 目錄下新建 user 目錄,并添加 list.html 頁(yè)面
list.html
WebJars是將客戶端(瀏覽器)資源(JavaScript,Css等)打成jar包文件,以對(duì)資源進(jìn)行統(tǒng)一依賴管理。
WebJars的jar包部署在Maven中央倉(cāng)庫(kù)上。
我們?cè)陂_發(fā)Java web項(xiàng)目的時(shí)候會(huì)使用像Maven,Gradle等構(gòu)建工具以實(shí)現(xiàn)對(duì)jar包版本依賴管理,以及項(xiàng)目的自動(dòng)化管理,但是對(duì)于JavaScript,Css等前端資源包,我們只能采用拷貝到webapp目錄下的手工方式,這樣做就無(wú)法對(duì)這些資源進(jìn)行依賴管理。而且容易導(dǎo)致文件混亂、版本不一致等問題。那么WebJars就提供給我們這些前端資源的jar包形式,我們就可以進(jìn)行依賴管理。
WebJars是將這些通用的Web前端資源打包成Java的Jar包,然后借助Maven工具對(duì)其管理,保證這些Web資源版本唯一性,升級(jí)也比較容易。關(guān)于webjars資源,有一個(gè)專門的網(wǎng)站http://www.webjars.org/,我們可以到這個(gè)網(wǎng)站上找到自己需要的資源,在自己的工程中添加入maven依賴,即可直接使用這些資源了。
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>用戶列表</title><!--使用webjar技術(shù)來(lái)添加第三庫(kù)--><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>創(chuàng)建時(shí)間</th><th>用戶狀態(tài)</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>
運(yùn)行結(jié)果:
刪除前:
刪除中
刪除后:
刷新數(shù)據(jù)庫(kù):
總結(jié)
以上是生活随笔為你收集整理的[JAVA EE] JPA技术基础:完成数据列表的删除的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个关于汪苏泷的个性签名!
- 下一篇: [JAVA EE] JPA 技术实践:完