核心内容摘要
张婉莹的笔尖芳华:那些治愈你我的免费视频集锦
以下是一个基于Java的无人共享图书借阅平台的核心源码实现方案涵盖后端架构、关键模块、数据库设计及核心代码示例采用Spring Boot MyBatis-Plus Redis RabbitMQ技术栈
项目架构src/ ├── main/ │ ├── java/com/library/ │ │ ├── config/ # 配置类Swagger, Redis, Security等 │ │ ├── controller/ # 接口层 │ │ ├── dto/ # 数据传输对象 │ │ ├── entity/ # 数据库实体 │ │ ├── exception/ # 异常处理 │ │ ├── mapper/ # MyBatis映射接口 │ │ ├── service/ # 业务逻辑层 │ │ ├── utils/ # 工具类JWT, 加密等 │ │ └── LibraryApplication.java # 启动类 │ └── resources/ │ ├── application.yml # 配置文件 │ └── mapper/ # XML映射文件
核心模块实现
数据库实体设计图书实体 (Book.java)javaData TableName(t_book) public class Book { TableId(type IdType.AUTO) private Long id; private String title; // 书名 private String isbn; // ISBN号 private String author; // 作者 private Integer total; // 总数量 private Integer available; // 可借数量 private String location; // 存放位置货架号 private LocalDate publishDate; // 出版日期 private LocalDateTime createTime; // 创建时间 }借阅记录 (BorrowRecord.java)javaData TableName(t_borrow_record) public class BorrowRecord { TableId(type IdType.AUTO) private Long id; private Long userId; // 用户ID private Long bookId; // 图书ID private LocalDateTime borrowTime; // 借出时间 private LocalDateTime dueTime; // 应还时间 private LocalDateTime returnTime; // 实际归还时间 private Integer status; // 状态0-借出中 1-已归还 2-逾期 }
核心业务逻辑图书服务层 (BookService.java)javaService RequiredArgsConstructor public class BookService { private final BookMapper bookMapper; private final RedisTemplateString, Integer redisTemplate; private final RabbitTemplate rabbitTemplate; // 查询图书列表带缓存 public ListBook listBooks(String keyword) { String cacheKey books:list: keyword; ListBook books redisTemplate.opsForValue().get(cacheKey); if (books ! null) return books; books bookMapper.selectList( new QueryWrapperBook() .like(title, keyword) .or() .like(author, keyword) ); redisTemplate.opsForValue().set(cacheKey, books, 10, TimeUnit.MINUTES); return books; } // 借书分布式锁库存扣减 Transactional public boolean borrowBook(Long bookId, Long userId) { //
检查库存Redis原子操作 String lockKey lock:book: bookId; try { Boolean locked redisTemplate.opsForValue().setIfAbsent(lockKey, 1, 5, TimeUnit.SECONDS); if (!locked) throw new RuntimeException(操作频繁请稍后再试); Integer available redisTemplate.opsForValue().get(stock: bookId); if (available null || available
{ throw new RuntimeException(图书已借完); } //
扣减库存Redis redisTemplate.opsForValue().decrement(stock: bookId); //
创建借阅记录MySQL BorrowRecord record new BorrowRecord(); record.setUserId(userId); record.setBookId(bookId); record.setBorrowTime(LocalDateTime.now()); record.setDueTime(LocalDateTime.now().plusDays(
); record.setStatus(
; bookMapper.insertBorrowRecord(record); //
发送逾期提醒消息RabbitMQ rabbitTemplate.convertAndSend( borrow.exchange, due.remind, record.getId() , record.getDueTime() ); return true; } finally { redisTemplate.delete(lockKey); } } }
控制器层 (BookController.java)javaRestController RequestMapping(/api/books) RequiredArgsConstructor public class BookController { private final BookService bookService; GetMapping public ResultListBook listBooks(RequestParam String keyword) { return Result.success(bookService.listBooks(keyword)); } PostMapping(/{bookId}/borrow) PreAuthorize(hasRole(USER)) public ResultString borrowBook( PathVariable Long bookId, AuthenticationPrincipal JwtUser user ) { boolean success bookService.borrowBook(bookId, user.getId()); return success ? Result.success(借书成功) : Result.error(借书失败); } }
消息队列消费者 (DueRemindConsumer.java)javaComponent RabbitListener(queues due.remind.queue) RequiredArgsConstructor public class DueRemindConsumer { private final BookMapper bookMapper; RabbitHandler public void handleDueRemind(String message) { String[] parts message.split(,); Long recordId Long.parseLong(parts[0]); LocalDateTime dueTime LocalDateTime.parse(parts[1]); if (LocalDateTime.now().isAfter(dueTime)) { // 更新借阅状态为逾期 bookMapper.updateBorrowStatus(recordId,
; // 实际项目中可集成短信/邮件服务发送提醒 System.out.println(【逾期提醒】借阅记录ID: recordId); } } }
三、