基于Spring Boot的商品管理系统是一种使用Spring Boot框架构建的电子商务系统,用于管理商品信息、订单、库存等。该系统可以方便地与数据库进行交互,实现商品的增删改查等功能。以下是一个简单的示例:
1. 首先,创建一个Spring Boot项目,并添加所需的依赖项。在pom.xml文件中添加以下依赖:
```xml
```
2. 创建实体类(Product.java):
```java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private Double price;
private Integer stock;
// getter和setter方法
}
```
3. 创建Repository接口(ProductRepository.java):
```java
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductRepository extends JpaRepository
}
```
4. 创建Service接口(ProductService.java):
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public Product saveProduct(Product product) {
return productRepository.save(product);
}
public void deleteProduct(Long id) {
productRepository.deleteById(id);
}
public Product findProductById(Long id) {
return productRepository.findById(id).orElse(null);
}
public List
return productRepository.findAll();
}
}
```
5. 创建Controller类(ProductController.java):
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/products")
public class ProductController {
@Autowired
private ProductService productService;
@PostMapping("/add")
public Product addProduct(@RequestBody Product product) {
return productService.saveProduct(product);
}
@GetMapping("/find/{id}")
public Product findProductById(@PathVariable Long id) {
return productService.findProductById(id);
}
@GetMapping("/find")
public List
return productService.findAllProducts();
}
}
```
6. 运行Spring Boot项目,访问http://localhost:8080/api/products,即可看到商品管理系统的界面。用户可以通过POST请求向/api/products/add端点添加商品,通过GET请求向/api/products/find端点查询商品信息。