我使用 MySQL 创建了一个用户表TbUser,userId类型为Integer,status默认值为0,并且使用@GetMapping("/deleteListUsers") public String deleteListUsers(@RequestParam(defaultValue = "1") int pageNum, Model model) { Page<TbUser> page = tbUserService.findUsers(pageNum); List<TbUser> userList = page.getContent(); model.addAttribute("userList", userList); model.addAttribute("currentPage", pageNum); model.addAttribute("totalPages", page.getTotalPages()); return "management/user-delete"; }完成了用户分页查询功能,查询的结果返回了user-delete.html,请帮使用springboot+hibernate+thymeleaf帮助我完成删除用户的功能,删除操作不做物理删除,只做逻辑删除(0表示未删除,1表示已删除),使用逻辑删除标识列status来记录,包含DAO、Controller、Entity、Service、impl
时间: 2024-02-13 19:05:39 浏览: 86
PHP基于ORM方式操作MySQL数据库实例
好的,很乐意为您解答。首先,我们来创建实体类TbUser:
```java
@Entity
@Table(name = "tb_user")
public class TbUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer userId;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private String password;
private Integer status = 0;
// 省略getter和setter方法
}
```
然后,创建DAO接口TbUserDao:
```java
@Repository
public interface TbUserDao extends JpaRepository<TbUser, Integer> {
}
```
创建TbUserService接口和TbUserServiceImpl实现类:
```java
public interface TbUserService {
Page<TbUser> findUsers(int pageNum);
void deleteUser(Integer userId);
}
@Service
@Transactional
public class TbUserServiceImpl implements TbUserService {
@Autowired
private TbUserDao tbUserDao;
@Override
public Page<TbUser> findUsers(int pageNum) {
Pageable pageable = PageRequest.of(pageNum - 1, 10, Sort.by(Sort.Direction.ASC, "userId"));
return tbUserDao.findAll(pageable);
}
@Override
public void deleteUser(Integer userId) {
TbUser tbUser = tbUserDao.getOne(userId);
tbUser.setStatus(1);
tbUserDao.save(tbUser);
}
}
```
最后,创建Controller类TbUserController:
```java
@Controller
@RequestMapping("/user")
public class TbUserController {
@Autowired
private TbUserService tbUserService;
@GetMapping("/deleteListUsers")
public String deleteListUsers(@RequestParam(defaultValue = "1") int pageNum, Model model) {
Page<TbUser> page = tbUserService.findUsers(pageNum);
List<TbUser> userList = page.getContent();
model.addAttribute("userList", userList);
model.addAttribute("currentPage", pageNum);
model.addAttribute("totalPages", page.getTotalPages());
return "management/user-delete";
}
@PostMapping("/deleteUser")
@ResponseBody
public String deleteUser(@RequestParam Integer userId) {
try {
tbUserService.deleteUser(userId);
return "success";
} catch (Exception e) {
return "error";
}
}
}
```
在user-delete.html中添加删除用户的按钮,并通过Ajax异步请求后台删除用户:
```html
<table>
<!-- 省略表头 -->
<tbody>
<tr th:each="user : ${userList}">
<td th:text="${user.userId}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td th:text="${user.status} == 0 ? '未删除' : '已删除'"></td>
<td>
<button class="btn btn-danger" th:attr="data-id=${user.userId}" onclick="deleteUser(this)">删除</button>
</td>
</tr>
</tbody>
</table>
<script>
function deleteUser(btn) {
if (confirm("确定要删除该用户吗?")) {
var userId = $(btn).data("id");
$.ajax({
url: "/user/deleteUser",
type: "POST",
data: {userId: userId},
success: function(result) {
if (result == "success") {
alert("删除成功");
window.location.reload();
} else {
alert("删除失败");
}
},
error: function() {
alert("删除异常");
}
});
}
}
</script>
```
这样,我们就完成了使用springboot+hibernate+thymeleaf删除用户的功能,只做逻辑删除,使用逻辑删除标识列status来记录。
阅读全文