[项目实战篇] Emos在线办公小程序--搭建项目

1.创建ck-emos项目

1)创建SpringBoot项目;

2)添加Maven依赖:

        SpringBoot DevTool 热部署插件;

        Lombook 生成SET/GET方法;

        SpringWeb SpringMVC框架;

        Mysql Driver Mysql数据库驱动;

        Mybatis Framework Mybatis 持久层框架;

        Spring Data Redis 操作Redis技术;

        Spring Data MongoDB 操作MongoDB技术;

        Spring For RabbitMQ RabbitMQ;

        Java Mail Sender 邮件发送技术;

        Quartz Scheduler 定时技术

3) 项目配置:

server:
  #配置Tomcat
  tomcat:
    uri-encoding: UTF-8
    threads:
      max: 200
      min-spare: 30
    connection-timeout: 5000ms
  port: 8080
  servlet:
    context-path: /emos
spring:
  #配置Mysql数据源
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://1.117.153.182:3305/ckkj_emos?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
      username: root
      password: ****
      initial-size: 8
      max-active: 16
      min-idle: 8
      max-wait: 60000
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
  #配置Redis数据源
  redis:
    database: 0
    host: 1.117.153.182
    port: 6379
    password:
    jedis:
      pool:
        max-active: 1000
        max-wait: -1ms
        max-idle: 16
        min-idle: 8
  #配置mongodb数据库
  data:
    mongodb:
      host: 1.117.153.182
      port: 27017
      database: emos
      authentication-database: admin
      username: admin
      password: ****
#配置Mybatis
mybatis:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.ck.emos.app.pojo
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
#配置日志输出
logging:
  level:
    root: info
    com:ck:emos:app:mapper : warn
  pattern:
    console: "%d{HH:mm:ss} %-5level %msg%n"

2.整合工具包

1)自定义异常类:

        自定义异常类继承的父类,我没有选择Exception。因为Exception类型的异常,我们必须要手动显式处理,要么上抛,要么捕获。我希望我定义的异常采用既可以采用显式处理,也可以隐式处理,所以我选择继承RuntimeException这个父类。RuntimeException类型的异常可以被虚拟机隐式处理,这样就省去了我们很多手动处理异常的麻烦。

package com.ck.emos.app.common;

import lombok.Data;

/**
 * 

* 自定义异常工具类 *

* * @author wzh * @since 2022-03-26 */ @Data public class EmosException extends RuntimeException{ /** * 错误消息 */ private String msg; /** * 状态码 */ private Integer code=500; public EmosException(String msg){ super(msg); this.msg=msg; } public EmosException(String msg,Throwable e){ super(msg,e); this.msg=msg; } public EmosException(String msg,Integer code){ super(msg); this.msg=msg; this.code=code; } public EmosException(String msg,Integer code,Throwable e){ super(msg,e); this.msg=msg; this.code=code; } }

2)定义标准返回对象

        采用Apache 的 httpcomponents 库里面的 HttpStatus 类封装了很多状态码;

package com.ck.emos.app.common;

import cn.hutool.http.HttpStatus;
import java.util.HashMap;
import java.util.Map;

/**
 * 

* 标准返回对象 *

* * @author wzh * @since 2022-03-26 */ public class Result extends HashMap { public Result(){ put("code", HttpStatus.HTTP_OK); put("msg","成功"); put("data",""); } /** * 生成链式调用put方法 * @param key * @param value * @return */ public Result put(String key,Object value){ super.put(key,value); return this; } /** * 生成静态工程方法 * @return */ public static Result success(){ return new Result(); } public static Result success(String msg,Object data){ Result result = new Result(); result.put("msg",msg); result.put("data",data); return result; } public static Result success(Object data){ Result result = new Result(); result.put("data",data); return result; } public static Result success(Map map){ Result result = new Result(); result.putAll(map); return result; } public static Result error(){ Result result = new Result(); result.put("code",HttpStatus.HTTP_INTERNAL_ERROR); result.put("msg","系统异常,请联系管理员!"); return result; } public static Result error(String msg){ Result result = new Result(); result.put("code",HttpStatus.HTTP_INTERNAL_ERROR); result.put("msg",msg); return result; } public static Result error(Integer code,String msg){ Result result = new Result(); result.put("code",code); result.put("msg",msg); return result; } }

3)集成Swagger:

[项目实战篇] Emos在线办公小程序--搭建项目_第1张图片

 

package com.ck.emos.app.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * 

* 集成Swagger *

* * @author wzh * @since 2022-03-26 */ @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi(){ Docket docket = new Docket(DocumentationType.SWAGGER_2); // 配置页面基本信息 ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder(); apiInfoBuilder.title("Emos在线办公系统"); ApiInfo apiInfo = apiInfoBuilder.build(); docket.apiInfo(apiInfo); // 添加类的方法 ApiSelectorBuilder selectorBuilder=docket.select(); // 先设定所有类的所有方法 selectorBuilder.paths(PathSelectors.any()); // 定义扫描的标识注解 selectorBuilder.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)); docket=selectorBuilder.build(); // 添加单点登录 ApiKey apiKey = new ApiKey("token", "token", "header"); List apiKeyList = new ArrayList<>(); apiKeyList.add(apiKey); docket.securitySchemes(apiKeyList); // 设定作用域--三层封装 AuthorizationScope scope=new AuthorizationScope("global","accessEverything"); AuthorizationScope[] scopes={scope}; SecurityReference reference=new SecurityReference("token",scopes); List securityReferences = new ArrayList<>(); securityReferences.add(reference); SecurityContext context=SecurityContext.builder().securityReferences(securityReferences).build(); List securityContexts = new ArrayList<>(); securityContexts.add(context); docket.securityContexts(securityContexts); return docket; } }

4) 添加后端校验:

[项目实战篇] Emos在线办公小程序--搭建项目_第2张图片

package com.ck.emos.app.param;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotNull;

@ApiModel(value = "前端请求封装参数")
@Data
public class TestSayHelloParam {

    @NotNull
    @ApiModelProperty(value = "名称")
    private String name;
}

你可能感兴趣的:(数据库,Lamdba,mysql,java,maven,spring)