1. 程式人生 > >介面統一引數格式封裝方法引數傳遞工具類

介面統一引數格式封裝方法引數傳遞工具類

1...ResultInfo實體承載類

package com.test.domi.common.system;

public class ResultInfo<T> {

    private String code;
    private String message;
    private T data;

    public ResultInfo(){
    }

    public  String getCode(){
        return this.code;
    }

    public void setCode(String code){
        
this.code = code; } public String getMessage(){ return this.message; } public void setMessage(String message){ this.message = message; } public Object getData(){ return this.data; } public void setData(T data){ this.data = data; } }

2...ResultCode列舉類

package com.test.domi.common.system;

public enum ResultCode {

    SUCCESS("000000","成功"),
    CONNECT_ERROR("100001","網路連線失敗"),
    CONNECT_TIMEOUT("100002","網路連線超時"),
    INTERNAL_SERVER_ERROR("100003","伺服器內部錯誤"),
    QUERY_ERROR("100004","查詢失敗"),
    INSERT_ERROR("100005","儲存資料失敗"),
    UPDATE_ERROR(
"100006","更新資料失敗"), DELETE_ERROR("100007","刪除資料失敗"); private String code; private String msg; ResultCode(String code, String msg) { this.code = code; this.msg = msg; } public String getCode() { return code; } public String getMsg() { return msg; } }

3...ResultUtil例項化工具類

package com.test.domi.common.system;

public class ResultUtil {

    public ResultUtil(){
    }

    public static ResultInfo getSuccessResult(Object object){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(ResultCode.SUCCESS.getCode());
        resultInfo.setMessage(ResultCode.SUCCESS.getMsg());
        resultInfo.setData(object);
        return resultInfo;
    }

    public static ResultInfo getFailResult(ResultCode resultCode){
        return getFailResult(resultCode,(Object)null);
    }

    public static ResultInfo getFailResult(String resultCode,String resultMessage,Object data){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(resultCode);
        resultInfo.setMessage(resultMessage);
        resultInfo.setData(data);
        return resultInfo;
    }

    public  static  ResultInfo getFailResult(ResultCode resultCode,Object data){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(resultCode.getCode());
        resultInfo.setMessage(resultCode.getMsg());
        resultInfo.setData(data);
        return resultInfo;
    }

}

4...業務方法之間引數傳遞的工具類

package com.test.domi.common.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 業務方法的引數傳遞類
 */
public class CommonMethedParam {

    /**
     * 業務方法的引數
     */
    private Object[] args;
    /**
     * 業務方法的返回值
     */
    private Object result;
    /**
     * 自定義引數map封裝
     */
    private Map<String,Object> paramMap;

    public CommonMethedParam(Object[] args){
        this.args = args;
        this.paramMap = new HashMap<>();
    }

    public CommonMethedParam(){
        this.paramMap = new HashMap<>();
    }

    /**
     * 獲取業務方法的引數
     */
    public Object[] getArgs(){
        return args;
    }

    /**
     * 獲取業務方法的返回值
     */
    public Object getResult(){
        return result;
    }

    /**
     * 設定業務方法的返回值
     */
    public void setResult(Object result){
        this.result = result;
    }

    /**
     * 獲取自定義引數的值
     */
    public Object get(String key){
        return paramMap.get(key);
    }

    /**
     * 設定自定義引數的值,用於在不同功能之間傳遞自定義引數
     */
    public void put(String key,Object value){
        paramMap.put(key,value);
    }

}

5...獲取springBean工具類

package com.test.domi.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring上下文工具類
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    /**
     * spring應用上下文
     */
    private static ApplicationContext applicationContext;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    /**
     * 獲取Bean物件
     * @param calssz bean類
     * @return bean例項
     */
    public static <T> T getBean(Class<T> calssz){
        return applicationContext.getBean(calssz);
    }

    /**
     * 獲取Bean物件
     * @param name beanName
     * @return bean物件
     */
    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }

}