原创

一键生成项目(目前只有想法)


代码生成器

最近再次阅读了MyBatis-Plus的官方文档,发现已经实现了代码生成器的绝大部分内容,但还没达到能直接生成一个项目的水平,因此我想在此基础上继续封装:

1、Controller层

该层包括四种接口:分页查询,单个信息查询,添加/修改,删除(可配置成逻辑删除)

2、基于MyBatis-Plus的分页插件

@Configuration
public class MyBatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        return paginationInterceptor;
    }
}

3、异常处理包

4、全局异常处理器

@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(ServiceException.class)
    public Result exceptionHandler(ServiceException e) {
        return Result.exception(e);
    }
}

5、拦截器

@Component
public class WebAppInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return super.preHandle(request, response, handler);
    }
}

@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private WebAppInterceptor webAppInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(webAppInterceptor);
    }
}

6、返回结果再次封装(还需完善)

/
 * 封装响应结果类
 *
 * @author lzlg
 */
@Getter
@Setter
public class Result<T> {

    private Integer code;

    private String message;

    private T data;

    private Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(200, "success", data);
    }

    public static <T> Result<T> ok() {
        return new Result<>(200, "success");
    }

    public static <T> Result<T> error(String message) {
        return new Result<>(500, message);
    }

    public static <T> Result<T> exception(ServiceException e) {
        return new Result<>(e.getCode(), e.getMessage());
    }
}

7、配置信息

application.yml文件自动生成并同时生成三种环境的配置文件,dev,test,pro环境。默认激活dev环境。还会根据maven依赖,添加相应的默认配置,比如redis,email等。

server:
  port: 80
spring:
  profiles:
    active: dev
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test_generator?useUnicode=true&useSSL=false&characterEncoding=utf8
    username: root
    password: password
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

  global-config:
    db-config:
      logic-delete-field: flag
      logic-delete-value: 1
      logic-not-delete-value: 0

8、maven的pom.xml

pom.xml文件自动生成,依赖根据用户选择依次添加

9、日志配置文件

logback-spring.xml

10、项目生成web界面

模仿 Spring Initializr 配置项目,甚至一键生成可直接部署和运行的jar或war包。

小工具
Spring Boot
  • 作者:lzlg520
  • 发表时间:2019-12-18 14:23
  • 版权声明:自由转载-非商用-非衍生-保持署名
  • 公众号转载:请在文末添加作者公众号二维码