• SpringBoot一站式功能提供框架(二)Mybatis Plus分页、Websocket 消息推送、提取word--柚子真好吃


    SpringBoot一站式功能提供框架(二)Mybatis Plus分页、Websocket 消息推送、提取word--柚子真好吃

    一、前言

    此框架主要针对SpringBoot项目各类功能做出封装,整合各类插件,提供简便快速用法;

    二、功能描述

    已完成功能:

    1. 整合Mybatis Plus单表查询;
    2. 整合Swagger接口文档;
    3. 整合Druid配置多数据源;
    4. 封装全局异常捕获;
    5. 封装同字段对象间转换方法;
    6. 整合Mybatis Plus分页查询(新);
    7. 整合WebSocket服务端(新);
      1)客户端注册;
      2)消息推送;
      3)针对IP推送;
    8. 封装Word工具类(新);
      1)提取Word中图片;

    待整合功能

    Nacos
    easy-es
    RabbitMQ
    Redis
    Debezium
    Cancel
    请求拦截器
    内部过滤器
    常用工具类
    gateway
    auth2
    文件上传下载接口封装
    hdfs/fastdfs文件存储
    本地文件夹监控
    文件读取
    压缩包读取
    数据库配置加密
    切面
    配置文件读取
    日期自动填充
    自定义注解转换 0-false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    三、具体实现

    1. 整合Mybatis Plus分页查询
      官网说明:https://baomidou.com/pages/97710a/#paginationinnerinterceptor

      首先根据官网说明进行配置

      	/**
       * @author Ryan
       * @date 2022年03月07日
       * @note 配置分页查询拦截器
       */
      @Configuration
      @MapperScan("com.ryan.fw.mapper")
      public class MybatisPlusConfig {
      
          /**
           * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
           */
          @Bean
          public MybatisPlusInterceptor mybatisPlusInterceptor() {
              MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
              interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
              return interceptor;
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20

      实现类

          @Override
          public StudentPageVO getPageList(Integer currentPage, Integer pageSize) {
              Page<StudentDO> page = this.page(new Page<>(currentPage, pageSize));
              //获取分页查询数据
              List<StudentDO> records = page.getRecords();
              //验证是否为null
              ObjUtils.checkNull(records,"分页查询无数据");
              //类型转换
              List<StudentVO> studentVO = ObjUtils.toList(records, StudentVO.class);
              //返回结果
              return StudentPageVO.builder().data(studentVO).count(page.getSize()).build();
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    2. 整合WebSocket服务端;
      服务端代码如下:(包含:客户端注册、消息推送、针对IP消息推送功能)

      	/**
       * @author Ryan
       * @date 2022/8/1 17:33
       * @note WebSocket服务端
       */
      @Component
      @Slf4j
      @ServerEndpoint("/websocket/{id}")
      public class WebSocketServer {
      
          private static Map<String, WebSocketServer> map = new HashMap<>(16);
      
          private Session session;
      
          /**
           * WebSocket连接
           *
           * @param session 连接session
           * @param id      连接id
           */
          @OnOpen
          public void onOpen(Session session, @PathParam("id") String id) {
              this.session = session;
      
              String ip = WebSocketUtils.getRemoteAddress(session);
              if (Objects.nonNull(ip)) {
                  id = ip + "-" + id;
              }
              map.put(id, this);
      
              try {
                  sendMessage(id, WebSocketVo.builder().flag("WebSocketConnect").status(true).message("WebSocket连接注册成功").data("WebSocket连接注册成功").build());
              } catch (IOException e) {
                  log.error("【WebSocket】推送至" + id + "消息失败");
                  e.printStackTrace();
              }
      
              log.info("【WebSocket】页面: " + id + " WebSocket注册连接成功");
          }
      
          /**
           * WebSocket连接关闭
           *
           * @param session 连接session
           * @param id      连接id
           */
          @OnClose
          public void onClose(Session session, @PathParam("id") String id) {
              String ip = WebSocketUtils.getRemoteAddress(session);
              if (Objects.nonNull(ip)) {
                  id = ip + "-" + id;
              }
              map.remove(id);
              log.info("【WebSocket】页面 " + id + " 断开连接");
              log.info("【WebSocket】当前断开IP" + ip);
              log.info("【WebSocket】当前连接集合:" + map);
              WebSocketVo vo = WebSocketVo.builder().flag("WebSocketDisconnect").status(true).message(id + "页面断开连接").data(id).type("setFlag").build();
              sendAllSession(vo, ip);
          }
      
          /**
           * 发送消息
           *
           * @param id 连接id
           * @param vo 消息内容
           * @return
           * @throws IOException
           */
          public static Boolean sendMessage(String id, WebSocketVo vo) throws IOException {
              WebSocketServer webSocketServer = map.get(id);
              if (webSocketServer.session.isOpen()) {
                  try {
                      webSocketServer.session.getBasicRemote().sendText(JSON.toJSONString(vo));
                  } catch (IllegalStateException e) {
                      return false;
                  }
                  return true;
              } else {
                  log.error("【WebSocket】接收方连接已关闭");
                  return false;
              }
          }
      
          /**
           * 广播发送特定IP消息
           *
           * @param vo 消息内容
           * @param ip 指定IP地址
           * @return
           */
          public synchronized static Boolean sendAllSession(WebSocketVo vo, String ip) {
              for (Map.Entry<String, WebSocketServer> entry : map.entrySet()) {
                  WebSocketServer ws = entry.getValue();
                  String key = entry.getKey();
                  if (key.indexOf(ip) > -1) {
                      try {
                          log.info("【WebSocket】发送" + entry.getKey() + "页面" + vo.getData() + "断开连接");
                          if (ws.session.isOpen()) {
                              ws.session.getBasicRemote().sendText(JSON.toJSONString(vo));
                          }
                      } catch (IOException e) {
                          e.printStackTrace();
                          return false;
                      }
                  }
              }
              return true;
          }
      
          /**
           * 验证当前连接是否存在
           *
           * @param id 连接id
           * @return Boolean
           */
          public synchronized static Boolean vertifyId(String id) {
              return Objects.isNull(map.get(id)) ? false : true;
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
      • 75
      • 76
      • 77
      • 78
      • 79
      • 80
      • 81
      • 82
      • 83
      • 84
      • 85
      • 86
      • 87
      • 88
      • 89
      • 90
      • 91
      • 92
      • 93
      • 94
      • 95
      • 96
      • 97
      • 98
      • 99
      • 100
      • 101
      • 102
      • 103
      • 104
      • 105
      • 106
      • 107
      • 108
      • 109
      • 110
      • 111
      • 112
      • 113
      • 114
      • 115
      • 116
      • 117
      • 118
      • 119
      • 120
    3. 封装word处理工具类;
      当前功能:提取word文件中图片;

      /**
       * @author Ryan
       * @date 2022年04月24日
       * @note
       */
      @Slf4j
      public class WordUtils {
      
          /**
           * 解析word中图片
           *
           * @param bytes
           * @return
           */
          public static List<MultipartFile> getPictures(byte[] bytes) {
              List<MultipartFile> list = new ArrayList<>();
              InputStream in = new ByteArrayInputStream(bytes);
              try {
                  Document doc = new Document(in);
                  NodeCollection nodes = doc.getChildNodes(NodeType.SHAPE, true);
                  for (Object obj : nodes) {
                      Shape shape = (Shape) obj;
                      if (shape.hasImage()) {
                          String name = UUID.randomUUID().toString() + ".png";
                          byte[] imageBytes = shape.getImageData().getImageBytes();
                          MultipartFile file = new MockMultipartFile(name, imageBytes);
                          list.add(file);
                      }
                  }
              } catch (Exception e) {
                  e.printStackTrace();
                  log.error("【Word解析】解析文件中图片失败");
              }
              return list;
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37

    四、开源地址

    GitHub: https://github.com/fsyxjwxw/SpringBoot-Framework

    如有其他想法或想要整合的插件请与本人联系;

  • 相关阅读:
    数据安全出境系列——数据追溯能力
    计算机网络最后复习
    web网页设计实例作业HTML+CSS+JavaScript蔬菜水果商城购物设计
    CCF ChinaSoft 2023 论坛巡礼|形式验证@EDA论坛
    Android开发笔记(一百八十八)工作管理器WorkManager
    从Mpx资源构建优化看splitChunks代码分割
    OCP Java17 SE Developers 复习题05
    redis bitmap数据结构之java对等操作
    从零搭建开发脚手架 顺应潮流开启升级 - SpringBoot 从2.x 升级到3.x
    [附源码]计算机毕业设计JAVA校园闲置物品租赁系统
  • 原文地址:https://blog.csdn.net/baidu_39265156/article/details/126111528