接口
/** * 邀请用户记录 * Created by wfl on 2017/9/20. */ public interface UserService { //邀请用户记录 Result<PageVo<UserVo>> findUserInfo(UserVo userVo); }
public class UserServericeImpl implements Userervice { @Autowired private ConvertService<UserVo,User> convertService; @Autowired private UserBpo UserBpo; @Override public Result<PageVo<UserVo>> findUserInfo(UserVo userVo) { PageVo<UserVo> pageVo = null; try { long startTime = System.currentTimeMillis(); Log.APP.info("查询明细开始,入参: " + inviteRewardRecordVo); if (null == userVo) { return ResultUtils.ResultFailure(ResultEnum.paramsError.getMessage()); } User user = convertService.VO2PO(userVo); PageVo<Usesr> page = userBpo.findUserInfo(user); if(page.getThisPageElements() != null && !page.getThisPageElements().isEmpty()){ List<User> userList = page.getThisPageElements(); List<UserVo> voList = new ArrayList<>(); for (User user: userList) { UserVo vo = convertService.PO2VO(user); voList.add(vo); } pageVo = new PageVo(page.getPageNo(),page.getPageSize(),voList,page.getTotalCount()); }else{ pageVo = PageVo.emptyPage(); } Log.APP.info("查询明细结束,耗时" + (System.currentTimeMillis() - startTime) + " ms"); } catch (Exception e) { Log.APP.error("--------------查询明细错误-----------"); Log.APP.error("错误信息", e); return ResultUtils.ResultFailure(ResultEnum.systemError.getMessage()); } return ResultUtils.ResultSuccess(pageVo); } }
Result
public class Result<T> implements Serializable{ public static final Integer CODE_SUCCESS = 1; public static final Integer CODE_FAILURE = 0; private Integer code; private String msg; private T data; public Result() { } public Result(Integer code, String msg) { this.code = code; this.msg = msg; } public Result(Integer code, String msg, T data) { this.code = code; this.msg = msg; this.data = data; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } public boolean isSuccess(){ if(this.code==Result.CODE_SUCCESS){ return true; } return false; } }
ResultUtils
public class ResultUtils{ //成功提示 public static Result ResultSuccess(){ return new Result(Result.CODE_SUCCESS,"成功"); } //成功提示 public static Result ResultSuccess(Object o){ return new Result(Result.CODE_SUCCESS,"成功",o); } //失败提示 public static Result ResultFailure(){ return new Result(Result.CODE_FAILURE,"失败"); } //失败提示 public static Result ResultFailure(Object o){ return new Result(Result.CODE_FAILURE,"失败",o); } public static Result ResultFailure(String msg){ return new Result(Result.CODE_FAILURE,msg); } public static <T> Result<T> result(String code, String info, T o) { return new Result(Integer.valueOf(code),info,o); } }
查询vo继承PageInfo
/** * Created by wfl on 2017/9/20. */ public class UserVo extends PageInfo { private String Name; private String password; private String age; public String getName() { return Name; } public void setName(String name) { Name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
PageInfo 封装了分页及排序
/** * Created by wfl on 2017/9/20. */ public class PageInfo implements Serializable{
protected Integer pageNo;//起始页 protected Integer pageSize;//每页显示多少条 protected String sort;//需要排序的字段 protected String dir;//排序的 private static final long serialVersionUID = -6414350656252331011L; public PageInfo(){ this(null,null); } public PageInfo(Integer pageNo, Integer pageSize) { this(pageNo,pageSize,null,null); } public PageInfo(Integer pageNo, Integer pageSize, String sort, String dir) { this.pageNo = pageNo; this.pageSize = pageSize; this.sort = sort; this.dir = dir; } public void setPageNo(Integer pageNo) { this.pageNo = pageNo; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public String getSort() { return sort; } public void setSort(String sort) { this.sort = sort; } public String getDir() { return dir; } public void setDir(String dir) { this.dir = dir; } public Integer getPageNo() { return pageNo; } public Integer getPageSize() { return pageSize; } }
ResultEnum 枚举类型的参数
public enum ResultEnum { paramsError(1111,"参数错误"), systemError(2222,"系统错误"); private int code; private String message; ResultEnum(int code, String message) { this.code = code; this.message = message; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }