最近再次阅读了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;
}
}
@ControllerAdvice
public class GlobalExceptionHandler {
@ResponseBody
@ExceptionHandler(ServiceException.class)
public Result exceptionHandler(ServiceException e) {
return Result.exception(e);
}
}
@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);
}
}
/
* 封装响应结果类
*
* @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());
}
}
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
pom.xml文件自动生成,依赖根据用户选择依次添加
logback-spring.xml
模仿 Spring Initializr 配置项目,甚至一键生成可直接部署和运行的jar或war包。