请根据user(uid,uname,upwd),commodity(cid,cname,cprice,cinventory),cart(cartid,cid,uid,cnum)三个表使用springboot帮我实现购物车功能,包括entity,service、controller、html、repository相关文件和完整代码
时间: 2024-03-14 14:43:36 浏览: 86
好的,我来帮您实现购物车功能。
首先是实体类:
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>
```
以上是购物车功能的完整代码,希望能对您有所帮助。
阅读全文