统一返回知识点
1.统一返回结果
1.使用泛型在工具类中创建统一返回类
@Data
public class RequestResult < T > {
private String code;
private String msg;
private T data;
}
2.使用系统常量来定义返回的状态码(constant)
public static final String SYS_RESP_SUCCESS_CODE = "0000" ;
public static final String SYS_RESP_SUCCESS_MSG = "SUCCESS" ;
public static final String SYS_RESP_FAILURE_CODE = "0001" ;
public static final String SYS_RESP_FAILURE_MSG = "FAILURE" ;
public static final int SYS_INIT_PAGE_SIZE = 3 ;
public static final int SYS_INIT_PAGE_NO = 1 ;
3.创建统一返回的工具类
public class ResultBuildUtil {
public static < T > RequestResult < T > success ( ) {
return success ( null ) ;
}
public static < T > RequestResult < T > success ( T t) {
RequestResult < T > requestResult = new RequestResult < > ( ) ;
requestResult. setCode ( SystemConstant . SYS_RESP_SUCCESS_CODE) ;
requestResult. setMsg ( SystemConstant . SYS_RESP_SUCCESS_MSG) ;
requestResult. setData ( t) ;
return requestResult;
}
public static < T > RequestResult < T > failure ( ) {
RequestResult < T > requestResult = new RequestResult < > ( ) ;
requestResult. setCode ( SystemConstant . SYS_RESP_FAILURE_CODE) ;
requestResult. setMsg ( SystemConstant . SYS_RESP_FAILURE_MSG) ;
requestResult. setData ( null ) ;
return requestResult;
}
public static < T > RequestResult < T > failure ( String errorCode, String errorMsg) {
RequestResult < T > requestResult = new RequestResult < > ( ) ;
requestResult. setCode ( errorCode) ;
requestResult. setMsg ( errorMsg) ;
requestResult. setData ( null ) ;
return requestResult;
}
}
4.使用
@GetMapping ( "/edcoByAuthor" )
public RequestResult < List < EdocEntry > > querySelectEdocByAuthor ( @RequestParam String author) {
log. info ( "---- 跟据作者查询文档列表 ----" ) ;
return ResultBuildUtil . success ( edocdbInfoService. queryEdocsByAuthor ( author) ) ;
}
2.统一返回分页结果
1.创建统一分页工具类
@Data
public class PageSupport < T > {
private int currPageNo;
private int pageIndex;
private int pageSize;
private int totalCount;
private int totalPage;
private Collection < T > data;
public void setCurrPageNo ( int currPageNo) {
if ( currPageNo > 0 ) {
this . currPageNo = currPageNo;
this . pageIndex = ( currPageNo - 1 ) * pageSize;
}
}
public void setTotalCount ( int totalCount) {
if ( totalCount > 0 ) {
this . totalCount = totalCount;
this . totalPage = this . totalCount % this . pageSize == 0 ? this . totalCount / this . pageSize : this . totalCount / this . pageSize+ 1 ;
}
}
}
2.service实现业务接口使用分页查询(两个参数,当前页码和页面容量)
@Override
public PageSupport < EdocEntry > queryEdocsUsePage ( Integer pageNo, Integer pageSize) {
PageSupport < EdocEntry > pageSupport = new PageSupport < > ( ) ;
if ( null == pageSize || pageSize <= 0 ) {
pageSupport. setPageSize ( SystemConstant . SYS_INIT_PAGE_SIZE) ;
} else {
pageSupport. setPageSize ( pageSize) ;
}
if ( null == pageNo || pageNo <= 0 ) {
pageSupport. setCurrPageNo ( SystemConstant . SYS_INIT_PAGE_NO) ;
} else {
pageSupport. setCurrPageNo ( pageNo) ;
}
EdocEntryExample edocEntryExample = new EdocEntryExample ( ) ;
pageSupport. setTotalCount ( ( int ) edocEntryDao. countByExample ( edocEntryExample) ) ;
edocEntryExample. setOrderByClause ( "id desc" ) ;
edocEntryExample. setOffset ( Long . valueOf ( pageSupport. getPageIndex ( ) ) ) ;
edocEntryExample. setLimit ( pageSupport. getPageSize ( ) ) ;
pageSupport. setData ( edocEntryDao. selectByExample ( edocEntryExample) ) ;
return pageSupport;
}
3.调用接口使用
3.异常统一返回
1.创建系统自定义异常枚举EdocExceptionEnum
public enum EdocExceptionEnum {
SYSTEM_EXCPT ( "9000" , "系统异常" ) ,
REQUEST_PARAM_EMPTY ( "1001" , "传入参数为空" ) ,
REQUEST_PARAM_TLLEGAL ( "1002" , "传入参数非法" ) ,
DATABASE_OPRRATOR_EXCPT ( "3001" , "数据库操作异常" ) ,
NETWORK_CONNECT_EXCPT ( "5001" , "网络连接异常" ) ;
private String excptCode;
private String excptMsg;
EdocExceptionEnum ( String excptCode, String excptMsg) {
this . excptCode = excptCode;
this . excptMsg = excptMsg;
}
public String getExcptCode ( ) {
return excptCode;
}
public String getExcptMsg ( ) {
return excptMsg;
}
}
2.自定义异常 (业务功能+ Exception)
public class EdocException extends RuntimeException {
private static final long serialVersionUID = - 7678037759463770915L ;
private EdocExceptionEnum edocExceptionEnum;
public EdocException ( ) {
}
public EdocException ( String message) {
super ( message) ;
}
public EdocException ( String message, Throwable cause) {
super ( message, cause) ;
}
public EdocException ( Throwable cause) {
super ( cause) ;
}
public EdocException ( String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super ( message, cause, enableSuppression, writableStackTrace) ;
}
public EdocException ( EdocExceptionEnum edocExcepttionEnum) {
this . edocExceptionEnum = edocExcepttionEnum;
}
public EdocExceptionEnum getEdocExcepttionEnum ( ) {
return edocExceptionEnum;
}
}
3.自定义异常统一处理类
@ControllerAdvice
@Slf4j
public class EdocExceptionHandler {
@ResponseBody
@ExceptionHandler ( EdocException . class )
public RequestResult < String > handEdocException ( EdocException ee) {
log. error ( "----- 获取系统抛出的自定义异常,异常码:{},异常信息:{}" , ee. getEdocExcepttionEnum ( ) . getExcptCode ( ) , ee. getEdocExcepttionEnum ( ) . getExcptMsg ( ) ) ;
return ResultBuildUtil . failure ( ee. getEdocExcepttionEnum ( ) . getExcptCode ( ) , ee. getEdocExcepttionEnum ( ) . getExcptMsg ( ) ) ;
}
}
4.使用自定义异常
@GetMapping ( "/edocsUsePage" )
public RequestResult < PageSupport < EdocEntry > > querySelectEdocByAuthor ( @RequestParam Integer pageNo, @RequestParam Integer pageSize) {
try {
log. info ( "---- 查询文档列表使用分页,当前页码{},页面容量 ----" , pageNo, pageSize) ;
return ResultBuildUtil . success ( edocdbInfoService. queryEdocsUsePage ( pageNo, pageSize) ) ;
} catch ( Exception e) {
log. error ( "查询电子文档列表,产生异常,异常为:{}" , e. getMessage ( ) ) ;
throw new EdocException ( EdocExceptionEnum . DATABASE_OPRRATOR_EXCPT) ;
}
}
4.数据校验
1.在controller使用@valid标注实体
@GetMapping ( "/testvaild" )
public RequestResult < String > testVaild ( @Valid EdocEntryFrom edocEntryFrom , BindingResult bindingResult) {
if ( bindingResult. hasErrors ( ) ) {
return ResultBuildUtil . failure ( "7001" , bindingResult. getFieldError ( ) . getDefaultMessage ( ) ) ;
}
return ResultBuildUtil . success ( ) ;
}
2.在接受前端参数的实体上进行验证
@Data
public class EdocEntryFrom {
private Long id;
@Range ( min = 1 , message = "cid非法" )
private Long cId;
@NotNull ( message = "标题不能为空" )
private String title;
@NotNull ( message = "摘要不能为空" )
private String summary;
private String uploadUser;
private Date createDate;
private static final long serialVersionUID = 1L ;
}
5.vo、po、dto、bo、pojo、entity、mode如何区分
// 通过这个拷贝参数
// BeanUtils.copyProperties(实体);
view <-- vo --> action
action <-- DTO --> service
serviece <-- DO --> Mapper
Mapper <-- PO --> JDBC
VO
value object:值对象
通常用于业务层之间的数据传递,由new创建,由GC回收
PO
persistant object:持久层对象
是ORM(Objevt Relational Mapping)框架中Entity,PO属性和数据库中表的字段形成一一对应关系
VO和PO,都是属性加上属性的get和set方法;
DTO
data transfer object:数据传输对象
是一种设计模式之间传输数据的软件应用系统,数据传输目标往往是数据访问对象从数据库中检索数据
// 主要是以上三种,下面了解
BO
business object:业务对象
BO把业务逻辑封转为一个对象,通过调用DAO方法,结合PO或VO进行业务操作
PO组合,如投保人是一个PO,被保险人是一个PO,险种信息是一个PO等等,他们组合气来是第一张保单的BO
POJO
plian ordinary java object:简单无规则java对象
纯的传统意义的java对象,最基本的Java Bean只有属性加上属性的get和set方法
可以额转化为PO、DTO、VO;比如POJO在传输过程中就是DTO
DAO
data access object:数据访问对象
是sun的一个标准j2ee设计模式,这个模式中有个接口就是DAO,负责持久层的操作
主要用来封装对数据的访问,注意,是对数据的访问,不是对数据库的访问
Entity
实体,和PO的功能类似,和数据表一一对应,一个实体一张表