• SpringBoot一站式功能提供框架(一)--柚子真好吃


    SpringBoot一站式功能提供框架(一)--柚子真好吃

    一、前言

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

    二、功能描述

    已完成功能:

    1. 整合Mybatis Plus单表查询;
    2. 整合Swagger接口文档;
    3. 整合Druid配置多数据源;
    4. 封装全局异常捕获;
    5. 封装同字段对象间转换方法;

    待整合功能

    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单表查询
      包含Service、ServiceImpl与mapper

      /**
       * @author Ryan
       * @date 2022-07-27
       */
      @Repository
      public interface StudentMapper extends BaseMapper<StudentDO> {
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      	/**
       * @author Ryan
       * @date 2022-07-27
       */
      public interface StudentService extends IService<StudentDO> {
      
          /**
           * 查询单条学生信息
           *
           * @param id
           * @return
           */
          public StudentVO one(String id);
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      /**
       * @author Ryan
       * @date 2022/7/27 11:32
       * @description 学生实现类
       */
      @Service
      public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentDO> implements StudentService {
      
          @Override
          public StudentVO one(String id) {
              StudentDO student = this.getById(id);
              ObjUtils.checkNull(student,"当前id:"+id+"无法确认学生信息");
              return ObjUtils.convert(student,StudentVO.class);
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
    2. 整合Swagger接口文档;
      引入 knife4j swagger增强插件(详见 pom.xml)
      配置类如下:

      /**
       * @author Ryan
       * @date 2022/7/27 20:56
       * @description swagger配置
       */
      @Configuration
      @EnableSwagger2
      @EnableKnife4j
      public class Knife4jConfiguration {
      
          @Bean
          public Docket createRestApi() {
              return new Docket(DocumentationType.SWAGGER_2)
                      .enable(true)
                      .apiInfo(apiInfo())
                      .select()
                      .apis(RequestHandlerSelectors.basePackage("com.ryan.fw.controller"))
                      .paths(PathSelectors.any())
                      .build()
                      .securitySchemes(securitySchemes());
          }
      
          private List<SecurityScheme> securitySchemes() {
              return Arrays.asList(new ApiKey("Authorization", "Authorization", "header"));
          }
      
          private ApiInfo apiInfo() {
              return new ApiInfoBuilder()
                      .title("Framework")
                      .description("Framework")
                      .contact(new Contact("Ryan", "url", "email"))
                      .version("1.0.0")
                      .build();
          }
      
      }
      
      • 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

      Controller中调用如下:

      @Api(tags = "【学生操作】")
      @RestController
      public class StudentController {
      
          @Resource
          private StudentService studentService;
      
          @ApiOperation("根据id查询学生信息")
          @GetMapping("/getStudentById")
          public Result getStudentById(@ApiParam("id") @RequestParam("id") String id) {
              return Result.success("查询成功", studentService.one(id));
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

      效果图如下:
      在这里插入图片描述

    3. 整合Druid配置多数据源;
      引入 dynamic-datasource-spring-boot-starter (详见 pom.xml)
      application.yml配置如下(当前仅为单数据源):

      server:
        port: 8888
      spring:
        datasource:
          dynamic:
            primary: master #设置默认的数据源或者数据源组,默认值即为master
            strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
            datasource:
              master:
                url: jdbc:mysql://127.0.0.1:3306/framework
                username: root
                password: 123456
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    4. 封装全局异常捕获;
      全局捕获 RuntimeException 返回前端结果;

      /**
       * @author Ryan
       * @date 2022/7/27 17:30
       * @description 全局异常处理
       */
      @Slf4j
      @RestControllerAdvice
      public class GlobalExceptionHandle {
          /**
           * 捕获RuntimeException并返回前端结果
           *
           * @param e
           * @return
           */
          @ResponseStatus(HttpStatus.BAD_REQUEST)
          @ExceptionHandler(value = RuntimeException.class)
          public Result handle(RuntimeException e) {
              log.error("【运行异常】" + e.getMessage());
              return Result.err(e.getMessage());
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21

      效果图如下:
      在这里插入图片描述

    5. 封装同字段对象间转换方法
      本功能采用 Dozer 插件进行封装,同时运用单例模式进行对象获取操作,代码如下;

      /**
          * 对象间转换
          *
          * @param obj
          * @param clazz
          * @param 
          * @return
          */
         public static <T> T convert(Object obj, Class<T> clazz) {
             return DozerBean.getInstance().map(obj, clazz);
         }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      /**
       * @author Ryan
       * @date 2022/7/27 18:10
       * @description 转换工具实例
       */
      @NoArgsConstructor
      public class DozerBean {
      
          public static DozerBeanMapper getInstance(){
              return DozerBeanHolder.DOZER_BEAN_MAPPER;
          }
      
          private static class DozerBeanHolder{
              private static final DozerBeanMapper DOZER_BEAN_MAPPER = new DozerBeanMapper();
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17

    四、开源地址

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

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

  • 相关阅读:
    【LSTM回归预测】基于matlab布谷鸟算法优化LSTM回归预测【含Matlab源码 2037期】
    GHost系统备份与还原
    丁鹿学堂:自学前端怎么样才能进步快
    合作式智能运输系统通信架构
    基于深度学习的Wiki中文语料词word2vec向量模型
    虚拟机设置固定ip
    TCP/IP协议栈的心跳、丢包重传、连接超时机制实例详解
    前端大容量存储方案IndexedDB
    『现学现忘』Git基础 — 35、Git中删除文件
    机器学习案例(四):LSTM股价预测
  • 原文地址:https://blog.csdn.net/baidu_39265156/article/details/126024244