@GetMapping("/findUser") public String findUserAll(User user, HttpServletRequest request) { List<User> list = userService.findUserAll(user); request.setAttribute("list",list); return "admin/userlist"; } }
时间: 2024-04-20 11:26:10 浏览: 59
这段代码是一个使用Spring MVC框架的Java代码片段。它表示一个GET请求映射的方法,路径为"/findUser"。该方法接收一个User对象和HttpServletRequest对象作为参数。
在方法体内,它调用userService的findUserAll方法,传入user对象作为参数,获取到一个User对象的列表list。
然后,将list对象存储在HttpServletRequest对象的属性中,属性名为"list"。
最后,返回一个字符串"admin/userlist",表示返回到名为"admin/userlist"的视图页面。
这段代码的作用是查询用户信息,并将查询结果存储在request对象中,用于展示用户列表的视图页面。
相关问题
请根据user(uid,uname,upwd),commodity(cid,cname,cprice,cinventory),cart(cartid,cid,uid,cnum)三个表使用springboot帮我实现购物车功能,包括entity,service、controller、html、repository相关文件和完整代码
好的,我来帮您实现购物车功能。
首先是实体类:
User.java
```java
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "uid")
private Long uid;
@Column(name = "uname")
private String uname;
@Column(name = "upwd")
private String upwd;
// 省略setter和getter
}
```
Commodity.java
```java
@Entity
@Table(name = "commodity")
public class Commodity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cid")
private Long cid;
@Column(name = "cname")
private String cname;
@Column(name = "cprice")
private BigDecimal cprice;
@Column(name = "cinventory")
private Integer cinventory;
// 省略setter和getter
}
```
Cart.java
```java
@Entity
@Table(name = "cart")
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cartid")
private Long cartid;
@Column(name = "cid")
private Long cid;
@Column(name = "uid")
private Long uid;
@Column(name = "cnum")
private Integer cnum;
// 省略setter和getter
}
```
接下来是DAO层:
UserRepository.java
```java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUname(String uname);
}
```
CommodityRepository.java
```java
@Repository
public interface CommodityRepository extends JpaRepository<Commodity, Long> {
}
```
CartRepository.java
```java
@Repository
public interface CartRepository extends JpaRepository<Cart, Long> {
List<Cart> findByUid(Long uid);
}
```
然后是Service层:
UserService.java
```java
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User findByUname(String uname) {
return userRepository.findByUname(uname);
}
}
```
CommodityService.java
```java
@Service
public class CommodityService {
@Autowired
private CommodityRepository commodityRepository;
public List<Commodity> findAll() {
return commodityRepository.findAll();
}
public Commodity findById(Long cid) {
return commodityRepository.findById(cid).orElse(null);
}
}
```
CartService.java
```java
@Service
public class CartService {
@Autowired
private CartRepository cartRepository;
public List<Cart> findByUid(Long uid) {
return cartRepository.findByUid(uid);
}
public void save(Cart cart) {
cartRepository.save(cart);
}
public void deleteById(Long id) {
cartRepository.deleteById(id);
}
}
```
最后是Controller层:
CartController.java
```java
@Controller
public class CartController {
@Autowired
private UserService userService;
@Autowired
private CommodityService commodityService;
@Autowired
private CartService cartService;
@GetMapping("/cart")
public String cart(Model model, HttpServletRequest request) {
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
if (user == null) {
return "redirect:/login";
}
List<Cart> cartList = cartService.findByUid(user.getUid());
model.addAttribute("cartList", cartList);
return "cart";
}
@PostMapping("/cart/add")
public String addCart(Long cid, Integer cnum, HttpServletRequest request) {
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
if (user == null) {
return "redirect:/login";
}
Commodity commodity = commodityService.findById(cid);
if (commodity == null) {
return "redirect:/commodity";
}
Cart cart = new Cart();
cart.setUid(user.getUid());
cart.setCid(cid);
cart.setCnum(cnum);
cartService.save(cart);
return "redirect:/cart";
}
@PostMapping("/cart/delete")
public String deleteCart(Long id) {
cartService.deleteById(id);
return "redirect:/cart";
}
}
```
最后是HTML页面:
cart.html
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>购物车</title>
</head>
<body>
<table>
<tr>
<th>商品名称</th>
<th>商品价格</th>
<th>商品数量</th>
<th>操作</th>
</tr>
<tr th:each="cart : ${cartList}">
<td th:text="${commodityService.findById(cart.cid).cname}"></td>
<td th:text="${commodityService.findById(cart.cid).cprice}"></td>
<td th:text="${cart.cnum}"></td>
<td>
<form method="post" th:action="@{/cart/delete}">
<input type="hidden" th:name="id" th:value="${cart.cartid}">
<button type="submit">删除</button>
</form>
</td>
</tr>
</table>
<form method="post" th:action="@{/cart/add}">
<select th:name="cid">
<option th:each="commodity : ${commodityList}" th:value="${commodity.cid}" th:text="${commodity.cname}"></option>
</select>
<input type="number" th:name="cnum" min="1">
<button type="submit">添加到购物车</button>
</form>
</body>
</html>
```
以上是购物车功能的完整代码,希望能对您有所帮助。
阅读全文