帮我用springboot、thymleaf、mariadb写一个购物车功能,实现加购,增减商品数量,全选、全不选、计算总价子功能,包括html、controller、service、dao以及数据库
时间: 2023-10-20 12:58:44 浏览: 46
好的,我可以帮你实现这个功能。首先,我们需要安装一些必要的工具和依赖项:
- JDK 8 或以上版本
- Maven
- IntelliJ IDEA 或 Eclipse
接下来,我们可以按照以下步骤来实现购物车功能:
1. 创建一个Spring Boot项目,并添加以下Maven依赖项:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.2</version>
</dependency>
```
2. 配置数据库连接,在application.properties文件中添加以下内容:
```properties
spring.datasource.url=jdbc:mariadb://localhost:3306/shopping_cart
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
```
3. 创建数据库shopping_cart,以及一个名为product的表,表结构如下:
```sql
CREATE TABLE `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`price` decimal(10,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
4. 创建Product实体类,包含id、name、price三个属性,以及对应的getter和setter方法。
5. 创建ProductRepository接口,继承JpaRepository,用于实现对数据库product表的CRUD操作。
```java
@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {
}
```
6. 创建ShoppingCartItem实体类,包含id、product、quantity和selected四个属性,以及对应的getter和setter方法。
7. 创建ShoppingCartItemRepository接口,继承JpaRepository,用于实现对数据库shopping_cart_item表的CRUD操作。
```java
@Repository
public interface ShoppingCartItemRepository extends JpaRepository<ShoppingCartItem, Integer> {
}
```
8. 创建ShoppingCartService类,用于实现购物车相关功能,包括加购、增减商品数量、全选、全不选、计算总价等。
```java
@Service
public class ShoppingCartService {
@Autowired
private ProductRepository productRepository;
@Autowired
private ShoppingCartItemRepository shoppingCartItemRepository;
public List<ShoppingCartItem> getShoppingCartItems() {
return shoppingCartItemRepository.findAll();
}
public void addItem(int productId) {
Product product = productRepository.findById(productId).orElse(null);
if (product != null) {
ShoppingCartItem item = shoppingCartItemRepository.findByProduct(product);
if (item == null) {
item = new ShoppingCartItem();
item.setProduct(product);
item.setQuantity(1);
item.setSelected(true);
} else {
item.setQuantity(item.getQuantity() + 1);
}
shoppingCartItemRepository.save(item);
}
}
public void removeItem(int itemId) {
shoppingCartItemRepository.deleteById(itemId);
}
public void updateQuantity(int itemId, int quantity) {
ShoppingCartItem item = shoppingCartItemRepository.findById(itemId).orElse(null);
if (item != null) {
item.setQuantity(quantity);
shoppingCartItemRepository.save(item);
}
}
public void selectAll() {
List<ShoppingCartItem> items = shoppingCartItemRepository.findAll();
for (ShoppingCartItem item : items) {
item.setSelected(true);
}
shoppingCartItemRepository.saveAll(items);
}
public void unselectAll() {
List<ShoppingCartItem> items = shoppingCartItemRepository.findAll();
for (ShoppingCartItem item : items) {
item.setSelected(false);
}
shoppingCartItemRepository.saveAll(items);
}
public BigDecimal getTotalPrice() {
List<ShoppingCartItem> items = shoppingCartItemRepository.findAll();
BigDecimal totalPrice = BigDecimal.ZERO;
for (ShoppingCartItem item : items) {
if (item.isSelected()) {
totalPrice = totalPrice.add(item.getProduct().getPrice().multiply(new BigDecimal(item.getQuantity())));
}
}
return totalPrice;
}
}
```
9. 创建ShoppingCartController类,用于实现购物车相关的页面跳转和请求处理,包括商品列表、加购、增减商品数量、全选、全不选、计算总价等。
```java
@Controller
public class ShoppingCartController {
@Autowired
private ShoppingCartService shoppingCartService;
@Autowired
private ProductRepository productRepository;
@GetMapping("/")
public String index(Model model) {
List<Product> products = productRepository.findAll();
model.addAttribute("products", products);
return "index";
}
@PostMapping("/add")
public String addItem(@RequestParam int productId) {
shoppingCartService.addItem(productId);
return "redirect:/cart";
}
@PostMapping("/remove")
public String removeItem(@RequestParam int itemId) {
shoppingCartService.removeItem(itemId);
return "redirect:/cart";
}
@PostMapping("/update")
public String updateQuantity(@RequestParam int itemId, @RequestParam int quantity) {
shoppingCartService.updateQuantity(itemId, quantity);
return "redirect:/cart";
}
@PostMapping("/select_all")
public String selectAll() {
shoppingCartService.selectAll();
return "redirect:/cart";
}
@PostMapping("/unselect_all")
public String unselectAll() {
shoppingCartService.unselectAll();
return "redirect:/cart";
}
@GetMapping("/cart")
public String cart(Model model) {
List<ShoppingCartItem> items = shoppingCartService.getShoppingCartItems();
BigDecimal totalPrice = shoppingCartService.getTotalPrice();
model.addAttribute("items", items);
model.addAttribute("totalPrice", totalPrice);
return "cart";
}
}
```
10. 创建index.html页面,用于展示商品列表和加购功能。
```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Shopping Cart</title>
</head>
<body>
<h1>Product List</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr th:each="product : ${products}">
<td th:text="${product.name}"></td>
<td th:text="${product.price}"></td>
<td>
<form method="post" action="/add">
<input type="hidden" name="productId" th:value="${product.id}">
<button type="submit">Add to Cart</button>
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
```
11. 创建cart.html页面,用于展示购物车列表、增减商品数量、全选、全不选、计算总价等功能。
```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Shopping Cart</title>
</head>
<body>
<h1>Shopping Cart</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total Price</th>
<th>Select</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr th:each="item : ${items}">
<td th:text="${item.product.name}"></td>
<td th:text="${item.product.price}"></td>
<td>
<form method="post" action="/update">
<input type="hidden" name="itemId" th:value="${item.id}">
<input type="number" name="quantity" th:value="${item.quantity}" min="1" max="100">
<button type="submit">Update</button>
</form>
</td>
<td th:text="${item.product.price.multiply(item.quantity)}"></td>
<td>
<form method="post" action="${item.selected ? '/unselect_all' : '/select_all'}">
<input type="hidden" name="_csrf" th:value="${_csrf.token}">
<button type="submit" th:text="${item.selected ? 'Unselect' : 'Select'}"></button>
</form>
</td>
<td>
<form method="post" action="/remove">
<input type="hidden" name="itemId" th:value="${item.id}">
<button type="submit">Remove</button>
</form>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">
<form method="post" action="${_csrf.token ? '/unselect_all' : '/select_all'}">
<input type="hidden" name="_csrf" th:value="${_csrf.token}">
<button type="submit" th:text="${_csrf.token ? 'Unselect All' : 'Select All'}"></button>
</form>
</td>
<td>Total Price: <span th:text="${totalPrice}"></span></td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
</body>
</html>
```
好的,现在我们已经实现了购物车功能,包括加购、增减商品数量、全选、全不选、计算总价等功能。你可以在浏览器中访问http://localhost:8080/来查看效果。