创建UserController、BookController、OrderController、CartController类,分别对应四张表的请求处理。在Controller层中调用Service层,完成对应的业务逻辑操作,并返回结果。
时间: 2023-07-10 20:05:54 浏览: 87
关于一个类操作多个表问题的程序
好的,下面是示例代码:
UserController:
```java
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable Long id) {
User user = userService.getUserById(id);
return ResponseEntity.ok(user);
}
@PostMapping("/")
public ResponseEntity<User> createUser(@RequestBody User userRequestBody) {
User user = userService.createUser(userRequestBody);
return ResponseEntity.status(HttpStatus.CREATED).body(user);
}
@PutMapping("/{id}")
public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User userRequestBody) {
User user = userService.updateUser(id, userRequestBody);
return ResponseEntity.ok(user);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
return ResponseEntity.noContent().build();
}
}
```
BookController:
```java
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("/{id}")
public ResponseEntity<Book> getBookById(@PathVariable Long id) {
Book book = bookService.getBookById(id);
return ResponseEntity.ok(book);
}
@PostMapping("/")
public ResponseEntity<Book> createBook(@RequestBody Book bookRequestBody) {
Book book = bookService.createBook(bookRequestBody);
return ResponseEntity.status(HttpStatus.CREATED).body(book);
}
@PutMapping("/{id}")
public ResponseEntity<Book> updateBook(@PathVariable Long id, @RequestBody Book bookRequestBody) {
Book book = bookService.updateBook(id, bookRequestBody);
return ResponseEntity.ok(book);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteBook(@PathVariable Long id) {
bookService.deleteBook(id);
return ResponseEntity.noContent().build();
}
}
```
OrderController:
```java
@RestController
@RequestMapping("/orders")
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping("/{id}")
public ResponseEntity<Order> getOrderById(@PathVariable Long id) {
Order order = orderService.getOrderById(id);
return ResponseEntity.ok(order);
}
@PostMapping("/")
public ResponseEntity<Order> createOrder(@RequestBody Order orderRequestBody) {
Order order = orderService.createOrder(orderRequestBody);
return ResponseEntity.status(HttpStatus.CREATED).body(order);
}
@PutMapping("/{id}")
public ResponseEntity<Order> updateOrder(@PathVariable Long id, @RequestBody Order orderRequestBody) {
Order order = orderService.updateOrder(id, orderRequestBody);
return ResponseEntity.ok(order);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteOrder(@PathVariable Long id) {
orderService.deleteOrder(id);
return ResponseEntity.noContent().build();
}
}
```
CartController:
```java
@RestController
@RequestMapping("/carts")
public class CartController {
@Autowired
private CartService cartService;
@GetMapping("/{id}")
public ResponseEntity<Cart> getCartById(@PathVariable Long id) {
Cart cart = cartService.getCartById(id);
return ResponseEntity.ok(cart);
}
@PostMapping("/")
public ResponseEntity<Cart> createCart(@RequestBody Cart cartRequestBody) {
Cart cart = cartService.createCart(cartRequestBody);
return ResponseEntity.status(HttpStatus.CREATED).body(cart);
}
@PutMapping("/{id}")
public ResponseEntity<Cart> updateCart(@PathVariable Long id, @RequestBody Cart cartRequestBody) {
Cart cart = cartService.updateCart(id, cartRequestBody);
return ResponseEntity.ok(cart);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteCart(@PathVariable Long id) {
cartService.deleteCart(id);
return ResponseEntity.noContent().build();
}
}
```
这里假设每个Controller都有对应的Service层实现了相应的逻辑,示例代码就不再赘述。
阅读全文