Java 结果集封装

1.返回消息及状态码

/**
 * 

Title: ReturnMsg.java</p> *

Description: 返回結果的状态碼和信息</p> * @author shy * @Version 1.0 * @Date 2018年7月3日上午10:35:05 */ public enum ReturnMsg { SUCCESS("0", "成功"), EXCEPTION1("10001", "1*******异常"), EXCEPTION2("10002", "2*******异常"), EXCEPTION3("10003", "3*******异常"), EXCEPTION4("10004", "4*******异常"), EXCEPTION5("10005", "5*******异常"), ERROR("-1", "失败"); private String code; private String msg; private ReturnMsg (String code, String msg) { this.code = code; this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }

2.封装结果集

/**
 * 

Title: ReturnResult.java</p> *

Description: 返回封裝結果集</p> * @author shy * @Version 1.0 * @Date 2018年7月3日上午10:36:40 */ public class ReturnResult { private long total; private Object data; private String code; private String msg; private ReturnResult(long total, Object data, String code, String msg) { this.total = total; this.data = data; this.code = code; this.msg = msg; } private ReturnResult(Object data) { this.data = data; this.code = ReturnMsg.SUCCESS.getCode(); this.msg = ReturnMsg.SUCCESS.getMsg(); } private ReturnResult(long total, Object data) { this.total = total; this.data = data; this.code = ReturnMsg.SUCCESS.getCode(); this.msg = ReturnMsg.SUCCESS.getMsg(); } private ReturnResult(String code, String msg) { this.code = code; this.msg = msg; } private ReturnResult() { this.code = ReturnMsg.ERROR.getCode(); this.msg = ReturnMsg.ERROR.getMsg(); } /*** * 输入参数: * * @param total * @param data * @param code * @param msg * @return 返回成功,包含total、code、msg、data */ public static ReturnResult success(long total, Object data, String code, String msg) { return new ReturnResult(total, data, code, msg); } /*** * 输入参数 * * @param total * @param data * @return 返回成功,包含total、code、msg、data */ public static ReturnResult success(long total, Object data) { return new ReturnResult(total, data); } /*** * 输入参数: * * @param data * @return 返回成功,包含code、msg、data */ public static ReturnResult success(Object data) { return new ReturnResult(data); } /*** * 无输入参数 返回成功,包含code、msg * * @return */ public static ReturnResult success() { return new ReturnResult(""); } /*** * 无输入参数 返回失败,包含code、msg * * @return */ public static ReturnResult error() { return new ReturnResult(); } public static ReturnResult error(String code, String msg) { return new ReturnResult(code, msg); } public long getTotal() { return total; } public void setTotal(long total) { this.total = total; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }

3.代码使用

@RequestMapping(value = "/list")
@ResponseBody
public ReturnResult getEmpList() throws IOException {
	String name = "admin_1";
	PageHelper.startPage(2, 2);
	PageHelper.orderBy("age");
	List empList = null;
	long total = -1;
	try {
		empList = empService.queryEmpList(name);
		if (null == empList || empList.isEmpty()) {
			return ReturnResult.error(ReturnMsg.EXCEPTION2.getCode(), ReturnMsg.EXCEPTION2.getMsg());
		}
		PageInfo pageEmp = new PageInfo(empList);
		total = pageEmp.getTotal();
		empList = pageEmp.getList();
		System.out.println(total);
	} catch (Exception e) {
		ReturnResult.error(ReturnMsg.EXCEPTION1.getCode(), ReturnMsg.EXCEPTION1.getMsg());
	}
	return ReturnResult.success(total, empList);
}

4.输出

total	4
data	
    0	
        empno	2
        empname	"admin_1"
        age	33
        sex	2
        indate	1529596800000
        deptno	30
    1	
        empno	4
        empname	"admin_4"
        age	44
        sex	2
        indate	1529856000000
        deptno	10
code	"0"
msg	"成功"

你可能感兴趣的:(java)