013醫學項目-模組一:加入工具類ResultUtil

來源:互聯網
上載者:User

標籤:except   public   ges   auth   div   函數   roc   details   建立   

 

這篇文章要做的就是最佳化,封裝。把之前的代碼盡量封裝進類,並且不要寫入程式碼。

在UserServiceimpl中的insertSysuser()函數之前是這麼寫的:

ResultInfo resultInfo=new ResultInfo();            resultInfo.setType(ResultInfo.TYPE_RESULT_FAIL);            String message=ResourcesUtil.getValue("resources.messages", "213");            resultInfo.setMessage(message);            throw new ExceptionResultInfo(resultInfo);

覺得太麻煩。我們實現一個工具類ResultUtil ,

package yycg.base.process.result;import java.util.List;import yycg.util.ResourcesUtil;/** * 系統結果工具類 * @author mrt * */public class ResultUtil {          /**     * 建立錯誤結果     * @param MESSAGE     * @return     */    public static ResultInfo createFail(String fileName,int messageCode,Object[] objs){        String message=null;        if(objs == null){            message = ResourcesUtil.getValue(fileName, messageCode+"");        }else{            message = ResourcesUtil.getValue(fileName, messageCode+"",objs);        }        return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,messageCode,message);    }    /**     * 建立敬告提示結果     */    public static ResultInfo createWarning(String fileName,int messageCode,Object[] objs){        String message=null;        if(objs == null){            message = ResourcesUtil.getValue(fileName, messageCode+"");        }else{            message = ResourcesUtil.getValue(fileName, messageCode+"",objs);        }        return new ResultInfo(ResultInfo.TYPE_RESULT_WARN,messageCode,message);    }        /**     * 建立成功提示結果     */    public static ResultInfo createSuccess(String fileName,int messageCode,Object[] objs){                String message=null;        if(objs == null){            message = ResourcesUtil.getValue(fileName, messageCode+"");        }else{            message = ResourcesUtil.getValue(fileName, messageCode+"",objs);        }        return new ResultInfo(ResultInfo.TYPE_RESULT_SUCCESS,messageCode,message);    }                /**     * 建立普通訊息提示結果     */    public static ResultInfo createInfo(String fileName,int messageCode,Object[] objs){                String message=null;        if(objs == null){            message = ResourcesUtil.getValue(fileName, messageCode+"");        }else{            message = ResourcesUtil.getValue(fileName, messageCode+"",objs);        }        return new ResultInfo(ResultInfo.TYPE_RESULT_INFO,messageCode,message);    }        /**     * 拋出異常     * @param resultInfo     * @throws ExceptionResultInfo     */    public static void throwExcepion(ResultInfo resultInfo) throws ExceptionResultInfo{        throw new ExceptionResultInfo(resultInfo);    }    public static void throwExcepion(ResultInfo resultInfo,List<ResultInfo> details) throws ExceptionResultInfo{        if(resultInfo != null){            resultInfo.setDetails(details);        }        throw new ExceptionResultInfo(resultInfo);    }    /**     * 建立提交結果資訊     * @param resultInfo     * @return     */    public static SubmitResultInfo createSubmitResult(ResultInfo resultInfo){        return new SubmitResultInfo(resultInfo);    }    /**     * 建立提交結果資訊,包括明細資訊     * @param resultInfo     * @param details     * @return     */    public static SubmitResultInfo createSubmitResult(ResultInfo resultInfo,List<ResultInfo> details){        if(resultInfo != null){            resultInfo.setDetails(details);        }        return new SubmitResultInfo(resultInfo);    }        public static void main(String[] args) {        // TODO Auto-generated method stub    }}

有了這工具類之後,之前的代碼可以這麼寫:

ResultInfo resultInfo=ResultUtil.createFail(Config.MESSAGE, 213, null);            new ResultUtil().throwExcepion(resultInfo);

具體著了說不清還是看文檔比較好。

 

013醫學項目-模組一:加入工具類ResultUtil

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.