帮我在user(uid,uname,upwd),commodity(cid,cname,cprice),cart(cartid,uid,cid,cnum,cartprice,cartselected)三个表的基础上,用springboot 、maven完成购物车功能,包括entiity,controller,service,reposity,HTML等文件,告诉我代码以及pom文件里添加的依赖项
时间: 2024-03-16 15:47:23 浏览: 108
基于springboot实现购物商城前后台管理系统(springboot+mybatis+maven+mysql+redis)
好的,以下是购物车功能的代码示例和pom文件依赖项:
1. Entity类:
User.java
```
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int uid;
private String uname;
private String upwd;
// getter, setter, constructor
}
Commodity.java
@Entity
@Table(name = "commodity")
public class Commodity {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int cid;
private String cname;
private double cprice;
// getter, setter, constructor
}
Cart.java
@Entity
@Table(name = "cart")
public class Cart {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int cartid;
private int uid;
private int cid;
private int cnum;
private double cartprice;
private boolean cartselected;
// getter, setter, constructor
}
```
2. Repository类:
UserRepository.java
```
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
// 根据用户名和密码查询用户
User findByUnameAndUpwd(String uname, String upwd);
}
CommodityRepository.java
@Repository
public interface CommodityRepository extends JpaRepository<Commodity, Integer> {
}
CartRepository.java
@Repository
public interface CartRepository extends JpaRepository<Cart, Integer> {
// 根据用户id查询购物车
List<Cart> findByUid(int uid);
// 根据用户id和商品id查询购物车
Cart findByUidAndCid(int uid, int cid);
// 根据用户id和购物车id查询购物车
Cart findByUidAndCartid(int uid, int cartid);
}
```
3. Service类:
UserService.java
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
// 登录
public User login(String uname, String upwd) {
return userRepository.findByUnameAndUpwd(uname, upwd);
}
}
CommodityService.java
@Service
public class CommodityService {
@Autowired
private CommodityRepository commodityRepository;
// 查询所有商品
public List<Commodity> findAll() {
return commodityRepository.findAll();
}
}
CartService.java
@Service
public class CartService {
@Autowired
private CartRepository cartRepository;
// 查询购物车
public List<Cart> findByUid(int uid) {
return cartRepository.findByUid(uid);
}
// 添加商品到购物车
public boolean addToCart(Cart cart) {
Cart oldCart = cartRepository.findByUidAndCid(cart.getUid(), cart.getCid());
if (oldCart != null) {
oldCart.setCnum(oldCart.getCnum() + cart.getCnum());
oldCart.setCartprice(oldCart.getCartprice() + cart.getCartprice());
cartRepository.save(oldCart);
} else {
cartRepository.save(cart);
}
return true;
}
// 修改购物车商品数量
public boolean updateCart(Cart cart) {
Cart oldCart = cartRepository.findByUidAndCartid(cart.getUid(), cart.getCartid());
if (oldCart != null) {
oldCart.setCnum(cart.getCnum());
oldCart.setCartprice(cart.getCartprice());
cartRepository.save(oldCart);
return true;
}
return false;
}
// 删除购物车商品
public boolean deleteCart(int uid, int cartid) {
Cart cart = cartRepository.findByUidAndCartid(uid, cartid);
if (cart != null) {
cartRepository.delete(cart);
return true;
}
return false;
}
}
4. Controller类:
UserController.java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
// 登录
@PostMapping("/login")
public String login(@RequestParam String uname, @RequestParam String upwd) {
User user = userService.login(uname, upwd);
if (user != null) {
return "success";
}
return "fail";
}
}
CommodityController.java
@RestController
@RequestMapping("/commodity")
public class CommodityController {
@Autowired
private CommodityService commodityService;
// 查询所有商品
@GetMapping("/findAll")
public List<Commodity> findAll() {
return commodityService.findAll();
}
}
CartController.java
@RestController
@RequestMapping("/cart")
public class CartController {
@Autowired
private CartService cartService;
// 查询购物车
@GetMapping("/findByUid")
public List<Cart> findByUid(@RequestParam int uid) {
return cartService.findByUid(uid);
}
// 添加商品到购物车
@PostMapping("/addToCart")
public String addToCart(@RequestParam int uid, @RequestParam int cid, @RequestParam int cnum, @RequestParam double cartprice) {
Cart cart = new Cart();
cart.setUid(uid);
cart.setCid(cid);
cart.setCnum(cnum);
cart.setCartprice(cartprice);
boolean result = cartService.addToCart(cart);
if (result) {
return "success";
}
return "fail";
}
// 修改购物车商品数量
@PostMapping("/updateCart")
public String updateCart(@RequestBody Cart cart) {
boolean result = cartService.updateCart(cart);
if (result) {
return "success";
}
return "fail";
}
// 删除购物车商品
@PostMapping("/deleteCart")
public String deleteCart(@RequestParam int uid, @RequestParam int cartid) {
boolean result = cartService.deleteCart(uid, cartid);
if (result) {
return "success";
}
return "fail";
}
}
5. HTML文件:
购物车页面cart.html:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
</head>
<body>
<table>
<tr>
<th>商品名称</th>
<th>商品价格</th>
<th>商品数量</th>
<th>商品小计</th>
<th>操作</th>
</tr>
<tbody id="cartTable"></tbody>
</table>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(function() {
// 查询购物车
$.ajax({
url: "/cart/findByUid",
type: "GET",
data: {
uid: 1
},
success: function(data) {
var html = "";
for (var i = 0; i < data.length; i++) {
var item = data[i];
html += "<tr>";
html += "<td>" + item.cname + "</td>";
html += "<td>" + item.cprice + "</td>";
html += "<td><input type='number' value='" + item.cnum + "' onchange='updateCart(" + item.cartid + ", $(this).val())' /></td>";
html += "<td>" + item.cartprice + "</td>";
html += "<td><a href='#' onclick='deleteCart(" + item.cartid + ")'>删除</a></td>";
html += "</tr>";
}
$("#cartTable").html(html);
}
});
});
// 添加商品到购物车
function addToCart(cid, cprice) {
$.ajax({
url: "/cart/addToCart",
type: "POST",
data: {
uid: 1,
cid: cid,
cnum: 1,
cartprice: cprice
},
success: function(data) {
if (data == "success") {
alert("添加成功!");
} else {
alert("添加失败!");
}
}
});
}
// 修改购物车商品数量
function updateCart(cartid, cnum) {
$.ajax({
url: "/cart/updateCart",
type: "POST",
dataType: "json",
contentType: "application/json;charset=utf-8",
data: JSON.stringify({
uid: 1,
cartid: cartid,
cnum: cnum,
cartprice: 0
}),
success: function(data) {
if (data == "success") {
alert("修改成功!");
} else {
alert("修改失败!");
}
}
});
}
// 删除购物车商品
function deleteCart(cartid) {
$.ajax({
url: "/cart/deleteCart",
type: "POST",
data: {
uid: 1,
cartid: cartid
},
success: function(data) {
if (data == "success") {
alert("删除成功!");
window.location.reload();
} else {
alert("删除失败!");
}
}
});
}
</script>
</body>
</html>
```
6. pom.xml文件依赖项:
```
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
</dependencies>
```
阅读全文