用springboot,在表cart(cartid,cid,uid,cnum,cprice,cartselected),orders(oid,uid,cid,ocondition,oprice,ocount)的基础上实现购物车功能,代码包括html,entity,repository,service,controller
时间: 2023-09-14 07:09:32 浏览: 105
好的,以下是使用Spring Boot实现购物车功能的代码:
1. 实体类 Cart.java:
```java
@Entity
@Table(name = "cart")
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long cartid;
private Long cid;
private Long uid;
private Integer cnum;
private Double cprice;
private Boolean cartselected;
// constructor, getter, setter, toString
}
```
2. 实体类 Orders.java:
```java
@Entity
@Table(name = "orders")
public class Orders {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long oid;
private Long uid;
private Long cid;
private String ocondition;
private Double oprice;
private Integer ocount;
// constructor, getter, setter, toString
}
```
3. CartRepository.java:
```java
@Repository
public interface CartRepository extends JpaRepository<Cart, Long> {
List<Cart> findByUid(Long uid);
Cart findByUidAndCid(Long uid, Long cid);
void deleteByUidAndCid(Long uid, Long cid);
}
```
4. OrdersRepository.java:
```java
@Repository
public interface OrdersRepository extends JpaRepository<Orders, Long> {
List<Orders> findByUid(Long uid);
void deleteByUidAndCid(Long uid, Long cid);
}
```
5. CartService.java:
```java
@Service
public class CartService {
@Autowired
private CartRepository cartRepository;
public List<Cart> findAllCartsByUid(Long uid) {
return cartRepository.findByUid(uid);
}
public void addCart(Cart cart) {
Cart existingCart = cartRepository.findByUidAndCid(cart.getUid(), cart.getCid());
if (existingCart != null) {
existingCart.setCnum(existingCart.getCnum() + cart.getCnum());
cartRepository.save(existingCart);
} else {
cart.setCartselected(true);
cartRepository.save(cart);
}
}
public void deleteCart(Long uid, Long cid) {
cartRepository.deleteByUidAndCid(uid, cid);
}
}
```
6. OrdersService.java:
```java
@Service
public class OrdersService {
@Autowired
private OrdersRepository ordersRepository;
@Autowired
private CartRepository cartRepository;
public List<Orders> findAllOrdersByUid(Long uid) {
return ordersRepository.findByUid(uid);
}
public void addOrders(Orders orders) {
ordersRepository.save(orders);
cartRepository.deleteByUidAndCid(orders.getUid(), orders.getCid());
}
}
```
7. CartController.java:
```java
@RestController
@RequestMapping("/cart")
public class CartController {
@Autowired
private CartService cartService;
@GetMapping("/{uid}")
public List<Cart> findAllCartsByUid(@PathVariable Long uid) {
return cartService.findAllCartsByUid(uid);
}
@PostMapping
public void addCart(@RequestBody Cart cart) {
cartService.addCart(cart);
}
@DeleteMapping("/{uid}/{cid}")
public void deleteCart(@PathVariable Long uid, @PathVariable Long cid) {
cartService.deleteCart(uid, cid);
}
}
```
8. OrdersController.java:
```java
@RestController
@RequestMapping("/orders")
public class OrdersController {
@Autowired
private OrdersService ordersService;
@GetMapping("/{uid}")
public List<Orders> findAllOrdersByUid(@PathVariable Long uid) {
return ordersService.findAllOrdersByUid(uid);
}
@PostMapping
public void addOrders(@RequestBody Orders orders) {
ordersService.addOrders(orders);
}
}
```
9. HTML页面的实现,可以参考以下代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
</head>
<body>
<table>
<tr>
<th>商品ID</th>
<th>商品名称</th>
<th>商品数量</th>
<th>商品单价</th>
<th>操作</th>
</tr>
<tbody id="cart-list">
</tbody>
</table>
<button onclick="handleCheckout()">结算</button>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
const uid = 123; // 假设当前用户ID为123
// 获取购物车列表
function getCartList() {
$.get(`/cart/${uid}`, function (data) {
let html = '';
data.forEach(item => {
html += `
<tr>
<td>${item.cid}</td>
<td>${item.cname}</td>
<td>${item.cnum}</td>
<td>${item.cprice}</td>
<td>
<button onclick="handleDelete(${item.cid})">删除</button>
</td>
</tr>
`;
});
$('#cart-list').html(html);
});
}
// 添加商品到购物车
function handleAdd(cid, cname, cprice) {
const cnum = 1; // 添加商品数量默认为1
const data = {cid, cname, uid, cnum, cprice, cartselected: true};
$.post('/cart', data, function () {
getCartList();
});
}
// 从购物车删除商品
function handleDelete(cid) {
$.ajax({
url: `/cart/${uid}/${cid}`,
type: 'DELETE',
success: function () {
getCartList();
}
});
}
// 结算
function handleCheckout() {
$.get(`/cart/${uid}`, function (data) {
data.forEach(item => {
const orders = {
uid,
cid: item.cid,
ocondition: '待支付',
oprice: item.cprice,
ocount: item.cnum
};
$.post('/orders', orders, function () {
getCartList();
});
});
});
}
$(function () {
getCartList();
});
</script>
</body>
</html>
```
以上是实现购物车功能的完整代码,可以根据需要进行改进和优化。
阅读全文