用springboot,在表cart(cartid,cid,uid,cnum,cprice,cartselected),orders(oid,uid,cid,ocondition,oprice,ocount)的基础上实现购物车功能,代码包括html,entity,service,controller
时间: 2023-09-14 12:09:32 浏览: 139
好的,下面是代码实现:
1. 实体类 Cart
```java
@Entity
@Table(name = "cart")
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer cartid;
private Integer cid;
private Integer uid;
private Integer cnum;
private Double cprice;
private Boolean cartselected;
// 省略getter和setter
}
```
2. 实体类 Orders
```java
@Entity
@Table(name = "orders")
public class Orders {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer oid;
private Integer uid;
private Integer cid;
private String ocondition;
private Double oprice;
private Integer ocount;
// 省略getter和setter
}
```
3. CartService接口
```java
public interface CartService {
List<Cart> listByUid(Integer uid);
void add(Integer uid, Integer cid, Integer cnum, Double cprice);
void update(Integer cartid, Integer cnum, Double cprice, Boolean cartselected);
void delete(Integer cartid);
}
```
4. CartServiceImpl实现类
```java
@Service
public class CartServiceImpl implements CartService {
@Autowired
private CartRepository cartRepository;
@Override
public List<Cart> listByUid(Integer uid) {
return cartRepository.findByUid(uid);
}
@Override
public void add(Integer uid, Integer cid, Integer cnum, Double cprice) {
Cart cart = cartRepository.findByUidAndCid(uid, cid);
if (cart == null) {
cart = new Cart();
cart.setUid(uid);
cart.setCid(cid);
cart.setCnum(cnum);
cart.setCprice(cprice);
cart.setCartselected(true);
} else {
cart.setCnum(cart.getCnum() + cnum);
}
cartRepository.save(cart);
}
@Override
public void update(Integer cartid, Integer cnum, Double cprice, Boolean cartselected) {
Cart cart = cartRepository.findById(cartid).orElse(null);
if (cart != null) {
if (cnum != null) {
cart.setCnum(cnum);
}
if (cprice != null) {
cart.setCprice(cprice);
}
if (cartselected != null) {
cart.setCartselected(cartselected);
}
cartRepository.save(cart);
}
}
@Override
public void delete(Integer cartid) {
cartRepository.deleteById(cartid);
}
}
```
5. CartController
```java
@RestController
@RequestMapping("/cart")
public class CartController {
@Autowired
private CartService cartService;
@GetMapping("/list")
public List<Cart> listByUid(Integer uid) {
return cartService.listByUid(uid);
}
@PostMapping("/add")
public void add(Integer uid, Integer cid, Integer cnum, Double cprice) {
cartService.add(uid, cid, cnum, cprice);
}
@PostMapping("/update")
public void update(Integer cartid, Integer cnum, Double cprice, Boolean cartselected) {
cartService.update(cartid, cnum, cprice, cartselected);
}
@PostMapping("/delete")
public void delete(Integer cartid) {
cartService.delete(cartid);
}
}
```
6. 页面
购物车列表:
```html
<div id="cartList"></div>
<script>
$.get("/cart/list?uid=1", function (data) {
var html = "";
for (var i = 0; i < data.length; i++) {
html += "<div>";
html += "<input type='checkbox' name='cartselected' value='" + data[i].cartid + "' " + (data[i].cartselected ? "checked" : "") + ">";
html += "<span>" + data[i].cid + "</span>";
html += "<button onclick='deleteCart(" + data[i].cartid + ")'>删除</button>";
html += "</div>";
}
$("#cartList").html(html);
});
</script>
```
添加商品到购物车:
```html
<form onsubmit="return false;">
<input type="hidden" name="uid" value="1">
<input type="number" name="cid" placeholder="商品ID" required>
<input type="number" name="cnum" placeholder="数量" required>
<input type="number" name="cprice" placeholder="价格" required>
<button onclick="addCart()">添加到购物车</button>
</form>
<script>
function addCart() {
$.post("/cart/add", $("form").serialize(), function () {
alert("添加成功");
});
}
</script>
```
更新购物车中商品的数量、价格和勾选状态:
```html
<div>
<input type="number" name="cnum" placeholder="数量" required>
<input type="number" name="cprice" placeholder="价格" required>
<input type="checkbox" name="cartselected" value="1" checked>勾选
<button onclick="updateCart(1)">更新</button>
</div>
<script>
function updateCart(cartid) {
$.post("/cart/update", {cartid: cartid, cnum: $("input[name=cnum]").val(), cprice: $("input[name=cprice]").val(), cartselected: $("input[name=cartselected]").prop("checked")}, function () {
alert("更新成功");
});
}
</script>
```
删除购物车中的商品:
```html
<button onclick="deleteCart(1)">删除</button>
<script>
function deleteCart(cartid) {
$.post("/cart/delete", {cartid: cartid}, function () {
alert("删除成功");
});
}
</script>
```
阅读全文