• SpringBoot项目依赖和配置


    maven依赖及一些配置

    这里主要是搭建项目常用到的maven依赖以及搭建项目会需要用到的一些配置文件,可能下面这些依赖还不是很全,但是应该会满足日常大部分的需求了

    Spring

    Spring项目的依赖

    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>5.3.9version>
    dependency>
    
    <dependency>
        <groupId>org.aspectjgroupId>
        <artifactId>aspectjweaverartifactId>
        <version>1.9.7version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    SpringBoot项目

    parent坐标
    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.4.RELEASEversion>
    parent>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    starter依赖
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starterartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    web starter 依赖
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    devtoos依赖

    开启SpringBoot项目热部署

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-devtoolsartifactId>
        <optional>trueoptional>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    数据库相关

    mysql - connector依赖

    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>8.0.24version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    druid连接池–集成boot项目

    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>druid-spring-boot-starterartifactId>
        <version>1.1.23version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    c3p0 连接池

    
    <dependency>
        <groupId>com.mchangegroupId>
        <artifactId>c3p0artifactId>
        <version>0.9.5.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    ORM框架

    MyBatis
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>3.5.6version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    MyBatis 集成Spring

    
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatis-springartifactId>
        <version>2.0.6version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    MyBatis-plus依赖
    <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-boot-starterartifactId>
        <version>3.4.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    mybatis-plus代码生成器

    <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-generatorartifactId>
        <version>3.2.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    缓存相关

    redis 集成boot项目

    添加的是spring-data-redis的依赖

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-data-redisartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    Spring Cache

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-cacheartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    Jedis

    
    <dependency>
        <groupId>redis.clientsgroupId>
        <artifactId>jedisartifactId>
        <version>2.8.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    安全框架

    shiro框架
    
    <dependency>
        <groupId>org.apache.shirogroupId>
        <artifactId>shiro-spring-boot-web-starterartifactId>
        <version>1.9.0version>
    dependency>  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Spring Security

    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-securityartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    常用工具类

    jwt 用户认证相关

    
    <dependency>
        <groupId>com.auth0groupId>
        <artifactId>java-jwtartifactId>
        <version>4.0.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    打包相关

    spring-boot-loader依赖
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-loaderartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    Json 相关

    org.json
    <dependency>
        <groupId>org.jsongroupId>
        <artifactId>jsonartifactId>
        <version>20160810version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    fastjson
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>fastjsonartifactId>
        <version>1.2.76version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    常用开发工具类

    commons-lang
    <dependency>
        <groupId>commons-langgroupId>
        <artifactId>commons-langartifactId>
        <version>2.6version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    lombok依赖
    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <version>1.18.12version>
        <scope>providedscope>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    junit测试工具类
    
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
        <scope>testscope>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    Http工具类

    普通的是一般的Http请求,第二个是异步请求的工具类

    
    <dependency>
        <groupId>org.apache.httpcomponentsgroupId>
        <artifactId>httpclientartifactId>
        <version>4.5.6version>
    dependency>
    
    <dependency>
        <groupId>org.apache.httpcomponentsgroupId>
        <artifactId>httpasyncclientartifactId>
        <version>4.1.4version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    接口文档相关

    Swagger2依赖

    添加了Swagger依赖和更换Swagger依赖的默认UI,采用了bootstrap-ui面板

    <dependency>
        <groupId>io.springfoxgroupId>
        <artifactId>springfox-swagger2artifactId>
        <version>2.7.0version>
    dependency>  
    
    <dependency>
        <groupId>com.github.xiaoymingroupId>
        <artifactId>swagger-bootstrap-uiartifactId>
        <version>1.9.1version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    knife4j

    <dependency>
        <groupId>com.github.xiaoymingroupId>
        <artifactId>knife4j-spring-boot-starterartifactId>
        <version>3.0.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Servlet 依赖

    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>javax.servlet-apiartifactId>
        <version>4.0.1version>
        <scope>providedscope>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Flink相关的依赖

    flink

    
    
    
    <dependency>
        <groupId>org.apache.flinkgroupId>
        <artifactId>flink-streaming-java_${scala.binary.version}artifactId>
        <version>${flink.version}version>
    dependency>
    <dependency>
        <groupId>org.apache.flinkgroupId>
        <artifactId>flink-clients_${scala.binary.version}artifactId>
        <version>${flink.version}version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    集成kafka
    <dependency>
        <groupId>org.apache.flinkgroupId>
        <artifactId>flink-connector-kafka_${scala.binary.version}artifactId>
        <version>${flink.version}version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    日志

    Logging-4j

    
    
    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-slf4j-implartifactId>
        <version>${log4j.version}version>
    dependency>
    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-apiartifactId>
        <version>${log4j.version}version>
    dependency>
    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-coreartifactId>
        <version>${log4j.version}version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    配置

    SpringBoot项目配置文件application.yml

    # 运行端口
    server:
      port: 9527
    spring:
    #  激活的环境
      profiles:
        active: dev
      application:
        name: reimbursementSystem
      servlet:
        multipart:
          max-file-size: 10MB
          max-request-size: 10MB
      datasource:
    #    druid:
    #      driver-class-name: com.mysql.cj.jdbc.Driver
    #      url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
    #      username: root
    #      password: 123456
    #      maxActive: 100
    #      initialSize: 10
    #   spring 默认的连接池
        url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
        username: root
        password: 123456
        driver-class-name: com.mysql.cj.jdbc.Driver
    
    
    
    • 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
    添加redis
    redis:
        host: localhost
        port: 6379
        password: 123456
        database: 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    添加mybatis-plus
    mybatis-plus:
      configuration:
        #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
        map-underscore-to-camel-case: true
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      global-config:
        db-config:
          id-type: ASSIGN_ID
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    添加Mybatis
    mybatis:
      mapper-location: classpath:/mapper/*.xml
    
    • 1
    • 2
    shiro
    shiro:
      loginUrl: /user/login
    
    • 1
    • 2

    Swagger文档配置SwaggerConfig

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        /**
         * 配置docket以配置Swagger具体参数
         * @return 返回一个docket配置参数
         */
        @Bean
        public Docket docket(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.group.reimbursement.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        /**
         * Api的信息
         * @return ApiInfo
         */
        private ApiInfo apiInfo(){
            Contact contact = new Contact("张连勇、林良怀","https://blog.csdn.net/zly03?spm=1000.2115.3001.5343","lyzhang@163.com");
            return new ApiInfoBuilder()
                    .title("发票管理系统")
                    .version("1.0.2")
                    .description("接口文档")
                    .contact(contact)
                    .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
    • 添加静态资源过滤,如果有添加过滤器和拦截器,也需要在拦截器或者过滤器中放行相关的url*
    /**
         * 设置静态资源
         * @param registry ResourceHandlerRegistry
         */
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            log.info("开始进行静态资源映射...");
            registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    相关的url

    .excludePathPatterns("/doc.html/**")
    .excludePathPatterns("/swagger-ui.html/**")
    .excludePathPatterns("/webjars/**")
    
    • 1
    • 2
    • 3

    mybatis-plus 配置

    /**
     * 配置分页插件
     *
     *@author zhanglianyong
     *@date 2022/8/5
     */
    @Configuration
    public class MybatisPlusConfig {
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
            mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
            return mybatisPlusInterceptor;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    Response 常用的响应封装类

    /**
     * 返回对象
     *
     *@author zhanglianyong
     *@date 2022/8/5
     */
    @Data
    @ApiModel("统一返回类")
    public class Response<T>  implements Serializable {
    
        /**
         * 编码:1成功,0和其它数字为失败
         */
        @ApiModelProperty("状态码,统一200为成功")
        private Integer code;
        /**
         * 错误信息
         */
        @ApiModelProperty("返回信息,错误信息")
        private String message;
        /**
         * 数据
         */
        @ApiModelProperty("返回数据")
        private T data;
        /**
         * 动态数据
         */
        @ApiModelProperty("动态数据")
        private Map<String, Object> map = new HashMap<>();
    
        public Response() {
        }
    
        public static <T> Response<T> successWithMessage(String message) {
            Response<T> r = new Response<>();
            r.message = message;
            r.data = null;
            r.code = HttpStatus.OK.value();
            return r;
        }
    
        public static <T> Response<T> success(T object) {
            Response<T> r = new Response<>();
            r.data = object;
            r.code = HttpStatus.OK.value();
            return r;
        }
    
        public static Response<String> buildJsonString(Object object) throws JsonProcessingException {
            String jsonString = toJsonString(object);
            return Response.success(jsonString);
        }
    
        private static String toJsonString(Object object) throws JsonProcessingException {
    
                ObjectMapper mapper = new ObjectMapper();
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                mapper.setDateFormat(df);
                return mapper.writeValueAsString(object);
        }
    
        public static <T> Response<T> error(String message, int code) {
            Response<T> r = new Response<>();
            r.message = message;
            r.code = code;
            return r;
        }
    
        public static <T> Response<T> common(int code, String message) {
            Response<T> result = new Response<>();
            result.setCode(code);
            result.setMessage(message);
            return result;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public T getData() {
            return data;
        }
    
        public void setData(T data) {
            this.data = data;
        }
    
        public Response(Integer code, String message, T data) {
            this.code = code;
            this.message = message;
            this.data = data;
        }
    
    }
    
    
    • 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

    BaseException 基础异常类BaseException

    /**
     * 基础异常
     *
     *@author zhanglianyong
     *@date 2022/8/4
     */
    public class BaseException extends RuntimeException {
    
        private static final long serialVersionUID = 1L;
    
        /**
         * 编码:1成功,0和其它数字为失败
         */
        private Integer code;
        /**
         * 错误信息
         */
        private String message;
    
        public BaseException(String message) {
            this.message = message;
        }
    
        public BaseException(String message, Throwable cause, Integer code, String message1) {
            super(message, cause);
            this.code = code;
            this.message = message1;
        }
    
        public BaseException(String message, Integer code) {
            this.code = code;
            this.message = message;
        }
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
        @Override
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
    }
    
    
    • 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
  • 相关阅读:
    计算化学:如何在云平台上计算声子谱
    Centos云服务器安装jdk1.8.0_191
    CP AUTOSAR标准之COM(AUTOSAR_CP_SWS_COM)(更新中……)
    Flink从Kafka写入mysql
    vue中addEventListener监听不到,原来是最外层没有设置长宽
    mulesoft Module 10 quiz 解析
    用PyTorch简单实现线性回归
    AI项目十七:ResNet50训练部署教程
    HTTP代理出现错误是什么原因?如何解决?
    【计算机体系知识】操作系统笔记(持续更新)
  • 原文地址:https://blog.csdn.net/zly03/article/details/127631115