基于Spring Boot+mysql的网上商城购物系统的代码参考
时间: 2023-11-28 14:47:45 浏览: 119
基于Spring Boot+mysql的网上商城购物系统设计与实现(源码+文档).zip
5星 · 资源好评率100%
这里提供一个基于Spring Boot+MySQL的简单网上商城购物系统的代码参考:
1. 首先,需要创建一个MySQL数据库和相应的表。可以使用以下SQL语句:
```sql
CREATE DATABASE shopping;
USE shopping;
CREATE TABLE products (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
description VARCHAR(255) NOT NULL,
price DECIMAL(10,2) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE orders (
id INT NOT NULL AUTO_INCREMENT,
customer_name VARCHAR(50) NOT NULL,
customer_email VARCHAR(50) NOT NULL,
total_price DECIMAL(10,2) NOT NULL,
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
CREATE TABLE order_items (
id INT NOT NULL AUTO_INCREMENT,
order_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (order_id) REFERENCES orders(id),
FOREIGN KEY (product_id) REFERENCES products(id)
);
```
2. 接下来,创建Spring Boot项目并添加必要的依赖。
3. 在项目中创建Product和Order实体类,并使用JPA注解进行映射。
```java
@Entity
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private BigDecimal price;
// constructors, getters and setters
}
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String customerName;
private String customerEmail;
private BigDecimal totalPrice;
private LocalDateTime orderDate;
// constructors, getters and setters
}
```
4. 创建相应的Repository,使用Spring Data JPA进行数据操作。
```java
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
}
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
}
```
5. 创建相应的Service和Controller,提供API接口。
```java
@Service
public class ProductService {
private final ProductRepository productRepository;
public ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}
public List<Product> getAllProducts() {
return productRepository.findAll();
}
public Product getProductById(Long id) {
return productRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Product not found with id " + id));
}
// other methods
}
@RestController
@RequestMapping("/api/products")
public class ProductController {
private final ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
@GetMapping
public List<Product> getAllProducts() {
return productService.getAllProducts();
}
@GetMapping("/{id}")
public Product getProductById(@PathVariable Long id) {
return productService.getProductById(id);
}
// other methods
}
```
6. 配置数据库连接信息,在application.properties文件中添加以下配置:
```
spring.datasource.url=jdbc:mysql://localhost:3306/shopping?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create-drop
```
注意,这里设置了"create-drop"选项,每次启动应用程序时都会重新创建数据库表。在生产环境中,应该设置为"update"选项。
7. 最后,运行应用程序并测试API接口。可以使用Postman等工具进行测试。例如,使用GET请求访问"http://localhost:8080/api/products",应该返回所有产品的JSON列表。
阅读全文