Solo  当前访客:0 开始使用

Nick1407 的个人博客

公众号:JavaCase

公共资源定义


一、接口返回值定义:

public class RestResponse<T>{  
    //返回信息  
    private String resultMsg;  
    //返回代码  
    private String resultCode;  
  
    private T data;  
  
    public RestResponse(){  
    }  
  
    public RestResponse(String resultCode, String resultMsg) {  
        this.resultMsg = resultMsg;  
        this.resultCode = resultCode;  
    }  
    public RestResponse(String resultCode, String resultMsg,T data) {  
        this.data=data;  
        this.resultMsg = resultMsg;  
        this.resultCode = resultCode;  
    }  
    public RestResponse<T> normalRestResponse(){  
        this.setResultCode("1");  
        this.setResultMsg("成功");  
        return this;  
    }  
    public RestResponse<T> normalRestResponse(T data){  
        this.setResultCode("1");  
        this.setResultMsg("成功");  
        this.setData(data);  
        return this;  
    }  
  
    public RestResponse<T> excetpionRestResponse(){  
        this.setResultCode("E0001");  
        this.setResultMsg("失败");  
        return this;  
    }  
  
    public RestResponse<T> excetpionRestResponse(T data){  
        this.setResultCode("E0001");  
        this.setResultMsg("失败");  
        this.setData(data);  
        return this;  
    }  
  
    public String getResultMsg() {  
        return resultMsg;  
    }  
  
    public void setResultMsg(String resultMsg) {  
        this.resultMsg = resultMsg;  
    }  
  
    public String getResultCode() {  
        return resultCode;  
    }  
  
    public void setResultCode(String resultCode) {  
        this.resultCode = resultCode;  
    }  
  
    public T getData() {  
        return data;  
    }  
  
    public void setData(T data) {  
        this.data = data;  
    }  
}

二、异常处理:

private Logger logger = LoggerFactory.getLogger(AbstractRestController.class);  
  
interface ExceptionHandler<T>{  
    RestResponse<T> Handle() throws IOException, InterruptedException;  
}  
  
public <T> RestResponse<T> exceptionHandler(ExceptionHandler<T> exceptionHandler){  
    RestResponse<T> restResponse;  
    try {  
        restResponse= exceptionHandler.Handle();  
    }catch (QueryReturnNullException e){  
        restResponse = new RestResponse<>(e.getErrCode(), e.getMessage());  
    }catch (ParamterVerifyException e){  
        restResponse = new RestResponse<>(e.getErrCode(), e.getMessage());  
    }catch (NxtCommonException e) {  
        logger.error(e.getMessage(), e);  
        restResponse = new RestResponse<>(e.getErrCode(), e.getMessage());  
    } catch (Exception e) {  
        logger.error(e.getMessage(), e);  
       restResponse = new RestResponse<T>().excetpionRestResponse();  
    }  
    return restResponse;  
}

//异常返回类

public class NxtCommonException extends RuntimeException{  
    private String errCode;  
  
    public NxtCommonException(String errCode,String message) {  
        super(message);  
       this.errCode=errCode;  
  
    }  
  
    public String getErrCode() {  
        return errCode;  
    }  
  
    public void setErrCode(String errCode) {  
        this.errCode = errCode;  
    }  
}

//异常字段定义

public static final String PARAM_EMPTY="E0001";  
  
public static final String QUERY_RESULT_NULL ="QE0001";  
  
public static class TaskException{  
    public static final String TASK_NOT_EXIST="TE0001";  
    public static final String TASK_FAILURE="TE0002";  
}  
public static class UserException{  
    public static final String USER_NOT_FOUND="UE0001";  
    public static final String USER_PASSWORD_MISMATCH="UE0002";  
}  
  
public static class OrderException{  
    public static final String ORDERNO_NOT_EXIST ="OE0001";  
    public static final String ORDER_SERVICE_EXIST ="OE0002";  
    public static final String PRODUCT_NOT_EXIST="OE1003";  
}  
  
public static class BankException{  
    public static final String BANK_NOT_EXIST="BE0001";  
    public static final String BANK_BRANCH_NOT_EXIST="BE0002";  
    public static final String BANK_ACCOPENNING_INFO_NOT_EXIST="BE0003";  
    public static final String BANK_MOBILE_VERIFYCODE="BE0004";  
}  
  
public enum EntExceptionEnum{  
  
    EntParamMissing("EE0006","字段缺失");  
  
    private String errCode;  
    private String errMsg;  
  
    EntExceptionEnum(String errCode, String errMsg) {  
        this.errCode = errCode;  
        this.errMsg = errMsg;  
    }  
  
    public String getErrCode() {  
        return errCode;  
    }  
  
    public void setErrCode(String errCode) {  
        this.errCode = errCode;  
    }  
  
    public String getErrMsg() {  
        return errMsg;  
    }  
  
    public void setErrMsg(String errMsg) {  
        this.errMsg = errMsg;  
    }  
  
    @Override  
    public String toString() {  
        return "EntExceptionEnum{" +  
                "errCode='" + errCode + '\'' +  
                ", errMsg='" + errMsg + '\'' +  
                '}';  
    }  
}  
  
public static class EntException{  
    public static final String ENT_NOT_EXIST="EE0001";  
    public static final String ENT_ADDRESS_NOT_EXIST="EE0002";  
    public static final String ENT_PERSON_NOT_EXIST="EE0003";  
    public static final String ENT_SELENIUM_ERROR="EE0004";  
    public static final String ENT_EXIST="EE0005";  
  
    public static final String ENT_LICENSE_PARSE_ERROR="EE1001";  
  
  
    public static final String ENT_SCOPE_MAPPING_NOT_FOUND="EE9001";  
  
  
    public static final String ENT_IDENT_IMG_PARSE_FAIR="EE9002";  
}

//接口父类
abstract class AbstractRestController {
    private Logger logger = LoggerFactory.getLogger(AbstractRestController.class);

    interface ExceptionHandler<T>{
        RestResponse<T> Handle() throws IOException, InterruptedException;
    }

    public <T> RestResponse<T> exceptionHandler(ExceptionHandler<T> exceptionHandler){
        RestResponse<T> restResponse;
        try {
            restResponse= exceptionHandler.Handle();
        }catch (QueryReturnNullException e){
            restResponse = new RestResponse<>(e.getErrCode(), e.getMessage());
        }catch (ParamterVerifyException e){
            restResponse = new RestResponse<>(e.getErrCode(), e.getMessage());
        }catch (NxtCommonException e) {
            logger.error(e.getMessage(), e);
            restResponse = new RestResponse<>(e.getErrCode(), e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
           restResponse = new RestResponse<T>().excetpionRestResponse();
        }
        return restResponse;
    }
}

//调用示例
@GetMapping("/searchUserInfo")
public RestResponse<UserInfoEntity> searchUserInfo(Long id) {
	return exceptionHandler(() -> {
		logger.info("查询用户信息,通过id:"+id);
		UserInfoEntity userInfo = userService.findUserInfo(id);
		return new RestResponse<UserInfoEntity>().normalRestResponse(userInfo);
		});
	}

三、动态查询

//封装公用实体类

public class EntitySearchCriteria<T extends Serializable> implements SearchCriteria {  
    private T entity;  
  
    private EntitySearchCriteria(T entity){  
        this.entity = entity;  
    }  
  
    /**  
     * Static method for creating the EntitySearchCriteria obj  
     *  
     * @param entity the wrapped entity  
     * @param <T>  
     * @return the EntitySearchCriteria obj  
     */  
    public static <T extends Serializable> EntitySearchCriteria createCriteria(T entity){  
        return new EntitySearchCriteria<>(entity);  
    }  
  
    /**  
     * Return the entity object.  
     *  
     * @return the entity object  
     */  
    public T getEntity(){  
        return entity;  
    }  
  
}

//定义动态查询接口

public interface SearchCriteria{  
}
EntitySearchCriteria criteria = EntitySearchCriteria.createCriteria(baseData); //参数
List<EntpriseBaseInfoWrapper> fetchWithCriteriaForRatio(@Param("searchCriteria") EntitySearchCriteria searchCriteria); //mapper方法  

//sql

<select id="searchBusiScopeCount" resultType="java.lang.Integer" parameterType="EntitySearchCriteria">  
    select  
    count(*) from  
    nxt_ent_big_data_enterprise_base_info bd  
    where  bd.ent_name_hangye=#{searchCriteria.entity.entNameHangye} and bd.ent_busi_scope=#{searchCriteria.entity.entBusiScope}  
    GROUP BY bd.ent_name_hangye, bd.ent_busi_scope  
</select>
//调用示例
EntitySearchCriteria criteria = EntitySearchCriteria.createCriteria(baseData);

		List<EntpriseBaseInfoWrapper> enterpriseBaseInfo = entBigDataBaseInfoBaseMapper.fetchWithCriteriaForRatio(criteria);

四、公用mapper父类

public interface GenericMyBatisMapper<T extends Serializable, PK> {  
  
    T fetchById(@Param("id") PK id);  
  
    List<T> fetchWithCriteria(@Param("searchCriteria") EntitySearchCriteria searchCriteria);  
  
    void insert(T entity);  
  
    void update(T entity);  
  
    void delete(T entity);  
  
    void softDelete(T entity);  
  
}

五、公用实体父类

public abstract class BaseEntity implements Serializable {  
  
    private static final long serialVersionUID = 1L;  
  
    private Long id; // 数据库默认主键  
  
    protected Long createdBy;  
  
    protected Timestamp createdTime;  
  
    protected Long updatedBy;  
  
    protected Timestamp updatedTime;  
  
    protected boolean deleted;  
  
    public Long getId() {  
        return id;  
    }  
  
    public void setId(Long id) {  
        this.id = id;  
    }  
  
    public Long getCreatedBy() {  
        return createdBy;  
    }  
  
    public void setCreatedBy(Long createdBy) {  
        this.createdBy = createdBy;  
    }  
  
    public Timestamp getCreatedTime() {  
        return createdTime;  
    }  
  
    public void setCreatedTime(Timestamp createdTime) {  
        this.createdTime = createdTime;  
    }  
  
    public Long getUpdatedBy() {  
        return updatedBy;  
    }  
  
    public void setUpdatedBy(Long updatedBy) {  
        this.updatedBy = updatedBy;  
    }  
  
    public Timestamp getUpdatedTime() {  
        return updatedTime;  
    }  
  
    public void setUpdatedTime(Timestamp updatedTime) {  
        this.updatedTime = updatedTime;  
    }  
  
    public boolean isDeleted() {  
        return deleted;  
    }  
  
    public void setDeleted(boolean deleted) {  
        this.deleted = deleted;  
    }  
  
    @Override  
    public boolean equals(Object o) {  
        if (this == o) return true;  
        if (o == null || getClass() != o.getClass()) return false;  
  
        BaseEntity that = (BaseEntity) o;  
  
        if (deleted != that.deleted) return false;  
        if (!id.equals(that.id)) return false;  
        if (createdBy != null ? !createdBy.equals(that.createdBy) : that.createdBy != null) return false;  
        if (createdTime != null ? !createdTime.equals(that.createdTime) : that.createdTime != null) return false;  
        if (updatedBy != null ? !updatedBy.equals(that.updatedBy) : that.updatedBy != null) return false;  
        return updatedTime != null ? updatedTime.equals(that.updatedTime) : that.updatedTime == null;  
    }  
  
    @Override  
    public int hashCode() {  
        int result = id.hashCode();  
        result = 31 * result + (createdBy != null ? createdBy.hashCode() : 0);  
        result = 31 * result + (createdTime != null ? createdTime.hashCode() : 0);  
        result = 31 * result + (updatedBy != null ? updatedBy.hashCode() : 0);  
        result = 31 * result + (updatedTime != null ? updatedTime.hashCode() : 0);  
        result = 31 * result + (deleted ? 1 : 0);  
        return result;  
    }  
  
    @Override  
    public String toString() {  
        return "id=" + id +  
                ", createdBy=" + createdBy +  
                ", createdTime=" + createdTime +  
                ", updatedBy=" + updatedBy +  
                ", updatedTime=" + updatedTime +  
                ", deleted=" + deleted;  
    }  
}


标题:公共资源定义
作者:Nick1407
地址:https://nick1407.com/articles/2019/07/08/1562564271314.html

0 0