JavaEE作业-实验三

目录

[1 实验内容](#1 实验内容)

[2 实验要求](#2 实验要求)

[3 思路](#3 思路)

[4 核心代码](#4 核心代码)

[5 实验结果](#5 实验结果)


1 实验内容

简单的线上图书交易系统的web层

2 实验要求

①采用SpringMVC框架,采用REST风格

②要求具有如下功能:商品分类、订单、购物车、库存

③独立完成,编写实验报告

3 思路

①确保项目结构清晰,按照MVC的结构进行组织

②在pom.xml中添加SpringMVC相关依赖,以及其他可能需要的依赖,比如数据库连接池、模板引擎等:

③配置web.xml文件,配置DispatcherServlet和Spring监听器:

④在WEB-INF目录下创建相关文件,配置相关内容:

⑤在com.exp.controller包下创建控制器类,使用@RestController注解,以REST风格暴露接口

⑥创建JSP文件,用于显示前端页面。

4 核心代码

BookController类

@RestController
@RequestMapping("/books")
public class BookController {

@Autowired

private BookService bookService;

@GetMapping

public List<Book> getAllBooks() {

return bookService.getAllBooks();
}

@GetMapping("/{id}")

public Book getBookById(@PathVariable Long id) {

return bookService.getBookById(id);
}

@PostMapping

public void addBook(@RequestBody Book book) {

bookService.addBook(book);
}

@PutMapping("/{id}")

public void updateBook(@PathVariable Long id, @RequestBody Book book) {
book.setId(id);

bookService.updateBook(book);
}

@DeleteMapping("/{id}")

public void deleteBook(@PathVariable Long id) {

bookService.deleteBook(id);
}
}

②OrderController

@RestController
@RequestMapping("/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 void addOrder(@RequestBody Order order) {
orderService.addOrder(order);
}

@PutMapping("/{id}")

public void updateOrder(@PathVariable Long id, @RequestBody Order order) {
order.setId(id);
orderService.updateOrder(order);
}

@DeleteMapping("/{id}")

public void deleteOrder(@PathVariable Long id) {
orderService.deleteOrder(id);
}
}

③CartController

@RestController
@RequestMapping("/cart")
public class CartController {

@Autowired

private CartService cartService;

@GetMapping("/{userId}")

public List<CartItem> getCartItemsByUserId(@PathVariable Long userId) {

return cartService.getCartItemsByUserId(userId);
}

@PostMapping

public void addCartItem(@RequestBody CartItem cartItem) {

cartService.addCartItem(cartItem);
}

@PutMapping("/{id}")

public void updateCartItem(@PathVariable Long id, @RequestBody CartItem cartItem) {
cartItem.setId(id);

cartService.updateCartItem(cartItem);
}

@DeleteMapping("/{id}")

public void deleteCartItem(@PathVariable Long id) {

cartService.deleteCartItem(id);
}
}

④InventoryController

@RestController
@RequestMapping("/inventory")
public class InventoryController {

@Autowired

private InventoryService inventoryService;

@GetMapping("/{bookId}")

public Inventory getInventoryByBookId(@PathVariable Long bookId) {

return inventoryService.getInventoryByBookId(bookId);
}

@PostMapping

public void addInventory(@RequestBody Inventory inventory) {

inventoryService.addInventory(inventory);
}

@PutMapping("/{id}")

public void updateInventory(@PathVariable Long id, @RequestBody Inventory inventory) {
inventory.setId(id);

inventoryService.updateInventory(inventory);
}

@DeleteMapping("/{id}")

public void deleteInventory(@PathVariable Long id) {

inventoryService.deleteInventory(id);
}
}

5 实验结果

①购物车界面

库存界面

订单界面

商品(书籍)界面

相关推荐
较真的菜鸟14 分钟前
使用ASM和agent监控属性变化
java
黎雁·泠崖21 分钟前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
qq_12498707532 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_2 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.2 小时前
Day06——权限认证-项目集成
java
瑶山2 小时前
Spring Cloud微服务搭建四、集成RocketMQ消息队列
java·spring cloud·微服务·rocketmq·dashboard
abluckyboy2 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
2301_818732062 小时前
前端调用控制层接口,进不去,报错415,类型不匹配
java·spring boot·spring·tomcat·intellij-idea
2501_941982052 小时前
深度对比:Java、Go、Python 实现企微外部群推送,哪个效率更高?
java·golang·企业微信
马猴烧酒.3 小时前
【面试八股|JAVA多线程】JAVA多线程常考面试题详解
java·服务器·数据库