@Configuration
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
Mybatis-plus分页插件使用的是IPage进行分页。IPage内部原理是基于拦截器,拦截的是方法以及方法中的参数。【判断是否是查询操作,如果是查询操作,才会进入分页的处理逻辑】。 进入分页逻辑处理后,拦截器会通过反射获取该方法的参数进行判断是否存在IPage对象的实现类。如果不存在则不进行分页,存在则将该参数赋值给IPage对象,(上述已说IPage分页,故参数给IPage对象)然后进行拼接sql的处理完成分页操作。
常量类模式
public class ResultCode {
/**成功 **/
public final static int OK = 20000;
/** 失败 **/
public final static int ERROR = 20001;
/**用户名或密码错误 **/
public final static int LOGIN_ERROR = 20002;
/** 权限不足**/
public final static int ACCESS_ERROR = 20003;
/** 远程调用失败**/
public final static int REMOTE_ERROR = 20004;
/** 重复操作**/
public final static int REPEAT_ERROR = 20005;
}
枚举类模式
public enum ResponseCodeEnum {
REQUEST_OK(200, "请求成功!"),
OPERATE_FAIL(101, "操作失败"),
BAD_REQUEST(400, "错误请求"),
ERROR(500, "系统异常"),
UNAUTHORIZED(401, "未授权"),
FORBIDDEN(403, "访问受限,授权过期"),
NOT_FOUND(404, "资源,服务未找到"),
NO_USER_LOGIN(1000, "用户没有登录或登录超时失效"),
AUTHORITY_NOT_ALLOW(1002, "权限不匹配"),
PARAM_ENUM_TRANS_EXP(2000, "传入的参数无法找到系统对应的配置值"),
SERIOUS_PARAM_EMPTY(2001, "必输参数为空"),
PARAM_IS_INVALID(2002, "参数无效"),
SYSTEM_ERROR(3001, "系统异常,请稍后重试"),
GLOBAL_EXCEPTION(6666, "抱歉,系统开小差了");
/** 编码 */
private Integer code;
private String message;
ResponseCodeEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
@Data
@ApiModel(value = "全局统一返回结果")
public class R implements Serializable {
public final static String OK_MSG = "请求成功";
public final static String FAIL_MSG = "请求失败";
@ApiModelProperty(value = "是否成功")
private boolean success;
@ApiModelProperty(value = "返回码")
private Integer code;
@ApiModelProperty(value = "返回消息")
private String message;
@ApiModelProperty(value = "返回数据")
private Object data;
@ApiModelProperty(value = "总条数")
private Long total;
@ApiModelProperty(value = "分页信息")
private PageInfo pageInfo;
@Data
public static class PageInfo {
@ApiModelProperty("当前页")
protected int currentPage;
@ApiModelProperty("页大小")
protected int pageSize;
@ApiModelProperty("总记录数")
protected long totalCount;
@ApiModelProperty("总页数")
protected long totalPage;
public PageInfo() {
}
@ConstructorProperties({"currentPage", "pageSize", "totalCount", "totalPage"})
public PageInfo(int currentPage, int pageSize, long totalCount, long totalPage) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
this.totalPage = totalPage;
}
}
private R(){}
private R(int code, String msg, Object data) {
this.code = code;
this.message = msg;
//代码重要点 此处区分分页结果或是 普通结果
if (data instanceof Page>) {
Page> page = (Page>) data;
this.total = page.getTotal();
this.data = page.getRecords();
this.pageInfo = new PageInfo((int)page.getCurrent(), (int)page.getSize(), page.getTotal(), page.getPages());
} else {
this.data = data;
}
}
public static R ok(){
R r = new R();
r.setSuccess(true);
r.setCode(ResultCode.OK);
r.setMessage("成功");
return r;
}
public static R ok(Object data) {
return new R(ResultCode.OK, OK_MSG, data);
}
public static R ok(String msg, Object data) {
return new R(ResultCode.OK, msg, data);
}
public static R error(){
R r = new R();
r.setSuccess(false);
r.setCode(ResultCode.ERROR);
r.setMessage("失败");
return r;
}
public static R error(String msg) {
return new R(ResultCode.ERROR, msg, null);
}
public static R error(int errorCode, String msg) {
return new R(errorCode, msg, null);
}
public R message(String message){
this.setMessage(message);
return this;
}
public R code(Integer code){
this.setCode(code);
return this;
}
public R data(Object data){
this.setData(data);
return this;
}
}
public class ResponseResult {
// 结果状态码
private Integer code;
// 响应信息
private String message;
// 响应数据
private T data;
//方法名 ok可改为success 看个人习惯
public static ResponseResult ok(T data) {
return new ResponseResult<>(codeEnum.REQUEST_OK.getCode(), codeEnum.REQUEST_OK.getMessage(), data);
}
public static ResponseResult fail(T data, Integer code, String msg) {
return new ResponseResult<>(code, msg, data);
}
public static ResponseResult fail(T data, codeEnum e) {
return new ResponseResult<>(e.getCode(), e.getMessage(), data);
}
public static ResponseResult fail(codeEnum e) {
return new ResponseResult<>(e.getCode(), e.getMessage(), null);
}