@RequestMapping("/list") public String list(@RequestParam(name = "page", defaultValue = "1") Integer pageNum, String content, Model model, HttpSession session) { String userId = session.getAttribute("id").toString(); User user = userDao.findById(userId).get(); MongoPageAble mongoPageAble = MongoPageAble.of(pageNum, 10000); Query query = new Query(); query.with(mongoPageAble); if (StrUtil.isNotBlank(content)) { Pattern pattern = Pattern.compile("^.*" + content + ".*$", Pattern.CASE_INSENSITIVE); query.addCriteria(Criteria.where("content").regex(pattern)); } if (user.getRoleId().equals("1")){ query.addCriteria(Criteria.where("userId").regex(userId)); } query.with(Sort.by(Sort.Direction.DESC, "createTime")); Page<Maintenance> page = MongoPageUtil.findPage(query, mongoPageAble, Maintenance.class, mongoTemplate); List<Maintenance> lists = page.getContent(); for (int i = 0; i < lists.size(); i++) { Maintenance maintenance = lists.get(i); try { String id = maintenance.getHouseId(); House house = houseDao.findById(id).get(); maintenance.setHouseId(house.getName()); }catch (Exception e){ maintenance.setHouseId("房产已删除"); } } model.addAttribute("list", page); return "maintenance/list"; }解释
时间: 2023-06-25 11:05:22 浏览: 144
这是一个Java Spring MVC的请求映射方法,用于处理“/list”页面的GET请求。这个方法有多个参数:
- pageNum:页码,表示要显示第几页的内容,默认值为1;
- content:查询关键字,用于搜索内容;
- model:用于存储视图中要显示的数据;
- session:用于获取当前用户的信息。
这个方法首先从session中获取当前用户的id,然后根据id查询到对应的User对象。接下来,使用MongoPageUtil提供的工具类进行MongoDB分页查询,查询条件包括:
- 如果content不为空,则根据content查询;
- 如果当前用户的roleId为1,则只查询该用户发布的维修信息。
最后,将查询到的维修信息列表封装到Page对象中,并将Page对象存储到model中,最终返回“maintenance/list”视图页面。在获取维修信息列表之前,还对每条维修信息中的houseId进行了处理,如果查询到对应的房产信息,则将houseId替换为房产名称,否则将houseId替换为“房产已删除”。
相关问题
我使用 MySQL 创建了一个用户表TbUser,userId的类型为Integer,并且使用@GetMapping("/alterListUsers") public String alterListUsers(@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-alter"; }完成了用户分页查询功能,查询的结果返回了user-alter.html,请使用springboot+hibernate+thymeleaf帮助我完成编辑用户的功能,点击编辑按钮后,弹窗一个表单可以修改用户所有信息,点击确定后将信息保存到数据库,包含DAO、Controller、Entity、Service、impl的详细代码
好的,下面是对应的代码:
TbUser Entity类:
```java
@Entity
@Table(name = "tb_user")
public class TbUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Integer userId;
@Column(name = "user_name")
private String userName;
@Column(name = "user_password")
private String userPassword;
// 省略getter和setter方法
}
```
TbUserRepository接口:
```java
@Repository
public interface TbUserRepository extends JpaRepository<TbUser, Integer> {
}
```
TbUserService接口:
```java
public interface TbUserService {
Page<TbUser> findUsers(int pageNum);
TbUser updateUser(TbUser user);
}
```
TbUserServiceImpl类:
```java
@Service
public class TbUserServiceImpl implements TbUserService {
@Autowired
private TbUserRepository userRepository;
@Override
public Page<TbUser> findUsers(int pageNum) {
Pageable pageable = PageRequest.of(pageNum - 1, 10);
return userRepository.findAll(pageable);
}
@Override
public TbUser updateUser(TbUser user) {
return userRepository.save(user);
}
}
```
UserController类:
```java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private TbUserService userService;
@GetMapping("/alterListUsers")
public String alterListUsers(@RequestParam(defaultValue = "1") int pageNum, Model model) {
Page<TbUser> page = userService.findUsers(pageNum);
List<TbUser> userList = page.getContent();
model.addAttribute("userList", userList);
model.addAttribute("currentPage", pageNum);
model.addAttribute("totalPages", page.getTotalPages());
return "management/user-alter";
}
@PostMapping("/updateUser")
@ResponseBody
public TbUser updateUser(@RequestBody TbUser user) {
return userService.updateUser(user);
}
}
```
user-alter.html页面:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>User Alter</title>
</head>
<body>
<div th:each="user : ${userList}">
<div>
<span th:text="${user.userId}"></span>
<span th:text="${user.userName}"></span>
<span th:text="${user.userPassword}"></span>
<button onclick="editUser(this)" th:data-userId="${user.userId}"
th:data-userName="${user.userName}" th:data-userPassword="${user.userPassword}">Edit
</button>
</div>
</div>
<!-- 弹窗表单 -->
<div id="editUserDialog" style="display: none">
<form id="editUserForm">
<input type="hidden" name="userId" id="editUserId">
User Name: <input type="text" name="userName" id="editUserName"><br>
Password: <input type="password" name="userPassword" id="editUserPassword"><br>
<button type="submit">Submit</button>
</form>
</div>
<!-- 引入jQuery和Ajax -->
<script th:src="@{/webjars/jquery/jquery.min.js}"></script>
<script th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"></script>
<script>
function editUser(button) {
var userId = $(button).data("userid");
var userName = $(button).data("username");
var userPassword = $(button).data("userpassword");
$("#editUserId").val(userId);
$("#editUserName").val(userName);
$("#editUserPassword").val(userPassword);
// 弹出表单
$("#editUserDialog").dialog({
modal: true,
width: 400,
buttons: {
"Submit": function () {
var userId = $("#editUserId").val();
var userName = $("#editUserName").val();
var userPassword = $("#editUserPassword").val();
// 发送Ajax请求保存用户信息
$.ajax({
url: "/user/updateUser",
type: "POST",
contentType: "application/json",
data: JSON.stringify({
userId: userId,
userName: userName,
userPassword: userPassword
}),
success: function (data) {
// 更新页面信息
$(button).data("username", userName);
$(button).data("userpassword", userPassword);
$(button).siblings("span").eq(1).text(userName);
$(button).siblings("span").eq(2).text(userPassword);
alert("Update user successfully!");
$("#editUserDialog").dialog("close");
},
error: function () {
alert("Update user failed!");
}
});
},
"Cancel": function () {
$("#editUserDialog").dialog("close");
}
}
});
}
</script>
</body>
</html>
```
注:需要在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.0.2</version>
</dependency>
```
我使用 MySQL 创建了一个用户表TbUser,userId类型为Integer,并且使用@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
好的,以下是你所需要的代码:
TbUser Entity类
```
@Entity
@Table(name = "TbUser")
public class TbUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer userId;
private String username;
private String password;
private Integer status;
// 省略getter和setter方法
}
```
TbUser DAO接口
```
@Repository
public interface TbUserDao extends JpaRepository<TbUser, Integer> {
Page<TbUser> findAllByStatus(Integer status, Pageable pageable);
}
```
TbUser Service接口
```
public interface TbUserService {
Page<TbUser> findUsers(int pageNum);
void logicDeleteUser(Integer userId);
}
```
TbUser Service实现类
```
@Service
public class TbUserServiceImpl implements TbUserService {
@Autowired
private TbUserDao tbUserDao;
@Override
public Page<TbUser> findUsers(int pageNum) {
Pageable pageable = PageRequest.of(pageNum - 1, 10); // 每页显示10条记录
return tbUserDao.findAllByStatus(0, pageable); // 查询未删除的用户
}
@Override
@Transactional(rollbackFor = Exception.class)
public void logicDeleteUser(Integer userId) {
TbUser tbUser = tbUserDao.findById(userId).orElse(null);
if (tbUser != null) {
tbUser.setStatus(1); // 将用户的状态设置为已删除
tbUserDao.save(tbUser);
}
}
}
```
Controller类
```
@Controller
@RequestMapping("/user")
public class UserController {
@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";
}
@GetMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable("id") Integer userId) {
tbUserService.logicDeleteUser(userId);
return "redirect:/user/deleteListUsers";
}
}
```
在user-delete.html中添加删除按钮
```
<table>
<thead>
<tr>
<th>用户ID</th>
<th>用户名</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${userList}">
<td th:text="${user.userId}"></td>
<td th:text="${user.username}"></td>
<td>
<a th:href="@{/user/deleteUser/{id}(id=${user.userId})}">删除</a>
</td>
</tr>
</tbody>
</table>
```
以上代码实现了逻辑删除单个用户的功能,当点击删除按钮后,会将该用户的状态设置为已删除,并重定向到用户列表页面。
阅读全文