库存管理javaspringboot
时间: 2023-11-29 14:47:13 浏览: 92
针对库存管理的Java Spring Boot应用,可以实现以下功能:
1. 商品信息管理:包括商品的名称、价格、库存数量等信息的录入、查询、编辑和删除。
2. 库存管理:对商品的库存数量进行增加、减少、查询等操作。
3. 订单管理:对用户的订单进行管理,包括订单的创建、查询、编辑和删除等操作。
4. 权限管理:对系统中的用户进行权限管理,包括管理员和普通用户两种角色,管理员可以进行所有操作,而普通用户只能进行部分操作。
5. 日志管理:记录系统中的所有操作,便于管理员进行问题排查。
以下是一个简单的Java Spring Boot库存管理应用的代码示例:
```java
// 商品信息实体类
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;
private int stock;
// 省略getter和setter方法
}
// 商品信息Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
}
// 商品信息Service
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public List<Product> getAllProducts() {
return productRepository.findAll();
}
public Product getProductById(Long id) {
return productRepository.findById(id).orElse(null);
}
public Product saveProduct(Product product) {
return productRepository.save(product);
}
public void deleteProduct(Long id) {
productRepository.deleteById(id);
}
}
// 商品信息Controller
@RestController
@RequestMapping("/api/products")
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("")
public List<Product> getAllProducts() {
return productService.getAllProducts();
}
@GetMapping("/{id}")
public Product getProductById(@PathVariable Long id) {
return productService.getProductById(id);
}
@PostMapping("")
public Product saveProduct(@RequestBody Product product) {
return productService.saveProduct(product);
}
@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable Long id) {
productService.deleteProduct(id);
}
}
// 库存管理Service
@Service
public class StockService {
@Autowired
private ProductRepository productRepository;
public int getStockById(Long id) {
Product product = productRepository.findById(id).orElse(null);
if (product != null) {
return product.getStock();
}
return 0;
}
public void increaseStock(Long id, int amount) {
Product product = productRepository.findById(id).orElse(null);
if (product != null) {
product.setStock(product.getStock() + amount);
productRepository.save(product);
}
}
public void decreaseStock(Long id, int amount) {
Product product = productRepository.findById(id).orElse(null);
if (product != null) {
product.setStock(product.getStock() - amount);
productRepository.save(product);
}
}
}
// 库存管理Controller
@RestController
@RequestMapping("/api/stock")
public class StockController {
@Autowired
private StockService stockService;
@GetMapping("/{id}")
public int getStockById(@PathVariable Long id) {
return stockService.getStockById(id);
}
@PostMapping("/{id}/increase")
public void increaseStock(@PathVariable Long id, @RequestParam int amount) {
stockService.increaseStock(id, amount);
}
@PostMapping("/{id}/decrease")
public void decreaseStock(@PathVariable Long id, @RequestParam int amount) {
stockService.decreaseStock(id, amount);
}
}
// 订单实体类
@Entity
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long productId;
private int amount;
// 省略getter和setter方法
}
// 订单Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
}
// 订单Service
@Service
public class OrderService {
@Autowired
private OrderRepository orderRepository;
public List<Order> getAllOrders() {
return orderRepository.findAll();
}
public Order getOrderById(Long id) {
return orderRepository.findById(id).orElse(null);
}
public Order saveOrder(Order order) {
return orderRepository.save(order);
}
public void deleteOrder(Long id) {
orderRepository.deleteById(id);
}
}
// 订单Controller
@RestController
@RequestMapping("/api/orders")
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping("")
public List<Order> getAllOrders() {
return orderService.getAllOrders();
}
@GetMapping("/{id}")
public Order getOrderById(@PathVariable Long id) {
return orderService.getOrderById(id);
}
@PostMapping("")
public Order saveOrder(@RequestBody Order order) {
return orderService.saveOrder(order);
}
@DeleteMapping("/{id}")
public void deleteOrder(@PathVariable Long id) {
orderService.deleteOrder(id);
}
}
// 权限管理实体类
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private String role;
// 省略getter和setter方法
}
// 权限管理Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
// 权限管理Service
@Service
public class UserService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found");
}
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), new ArrayList<>());
}
public List<User> getAllUsers() {
return userRepository.findAll();
}
public User getUserById(Long id) {
return userRepository.findById(id).orElse(null);
}
public User saveUser(User user) {
return userRepository.save(user);
}
public void deleteUser(Long id) {
userRepository.deleteById(id);
}
}
// 权限管理Controller
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("")
public List<User> getAllUsers() {
return userService.getAllUsers();
}
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
return userService.getUserById(id);
}
@PostMapping("")
public User saveUser(@RequestBody User user) {
return userService.saveUser(user);
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
}
}
// 日志管理
@Configuration
@EnableAspectJAutoProxy
public class LoggingAspect {
@Autowired
private HttpServletRequest request;
@Autowired
private HttpServletResponse response;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserService userService;
@Pointcut("execution(* com.example.demo.controller.*.*(..))")
public void controllerPointcut() {}
@Around("controllerPointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
String username = request.getUserPrincipal().getName();
User user = userService.findByUsername(username);
String method = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
String url = request.getRequestURI();
String ip = request.getRemoteAddr();
String userAgent = request.getHeader("User-Agent");
// 记录日志
System.out.println(String.format("User %s [%s] %s %s %s %s %s", user.getRole(), user.getUsername(), method, args, url, ip, userAgent));
return joinPoint.proceed();
}
}
```
阅读全文