• java封装结果集


    package com.credithc.pda.common.utils;

    import java.io.Serializable;

    /**
    * @description:操作结果集
    *
    */
    public class Result implements Serializable {

    private static final long serialVersionUID = 495242526721135983L;
    public static final int SUCCESS = 1;
    public static final int FAILURE = -1;

    private boolean success = false;

    private String msg = "";

    private Object obj = null;

    private int count ;

    public int getCount() {
    return count;
    }

    public void setCount(int count) {
    this.count = count;
    }

    public boolean isSuccess() {
    return success;
    }

    public void setSuccess(boolean success) {
    this.success = success;
    }

    public String getMsg() {
    return msg;
    }

    public void setMsg(String msg) {
    this.msg = msg;
    }

    public Object getObj() {
    return obj;
    }

    public void setObj(Object obj) {
    this.obj = obj;
    }

    }

    *****************************************

    package com.credithc.pda.controller;

    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.List;

    import org.apache.shiro.SecurityUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.web.bind.ServletRequestDataBinder;
    import org.springframework.web.bind.annotation.InitBinder;

    import com.credithc.pda.common.security.ShiroUser;
    import com.credithc.pda.common.utils.Result;
    import com.credithc.pda.common.utils.StringEscapeEditor;
    import com.credithc.pda.model.User;
    import com.credithc.pda.service.UserService;

    /**
    * @description:基础 controller
    *
    */
    public abstract class BaseController {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserService userService;

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
    /**
    * 自动转换日期类型的字段格式
    */
    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));

    /**
    * 防止XSS攻击
    */
    binder.registerCustomEditor(String.class, new StringEscapeEditor(true, false));
    }
    /**
    * 获取当前登录用户对象
    * @return
    */
    public User getCurrentUser() {
    ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
    User currentUser = userService.findUserById(user.getUserId());
    return currentUser;
    }

    /**
    * 获取当前登录用户id
    * @return
    */
    public Long getUserId() {
    return this.getCurrentUser().getUserId();
    }

    /**
    * 获取当前登录用户名
    * @return
    */
    public String getUserName() {
    return this.getCurrentUser().getUserName();
    }

    /**
    * ajax失败
    * @param msg 失败的消息
    * @return {Object}
    */
    public Object renderError(String msg) {
    Result result = new Result();
    result.setMsg(msg);
    return result;
    }

    /**
    * ajax失败
    * @param obj 失败时的对象
    * @return {Object}
    */
    public Object renderError(Object obj) {
    Result result = new Result();
    result.setObj(obj);
    return result;
    }

    /**
    * ajax失败
    * @param msg 失败的消息
    * @param obj 失败时的对象
    * @return {Object}
    */
    public Object renderError(String msg,Object obj) {
    Result result = new Result();
    result.setMsg(msg);
    result.setObj(obj);
    return result;
    }

    /**
    * ajax成功
    * @return {Object}
    */
    public Object renderSuccess() {
    Result result = new Result();
    result.setSuccess(true);
    return result;
    }

    /**
    * ajax成功
    * @param msg 消息
    * @return {Object}
    */
    public Object renderSuccess(String msg) {
    Result result = new Result();
    result.setSuccess(true);
    result.setMsg(msg);
    return result;
    }

    /**
    * ajax成功
    * @param obj 成功时的对象
    * @return {Object}
    */
    public Object renderSuccess(Object obj) {
    Result result = new Result();
    result.setSuccess(true);
    result.setObj(obj);
    return result;
    }

    /**
    * 分页查询成功
    * @param obj 成功时的对象
    * @return {Object}
    */
    public Object renderPage(Object obj,int count) {
    Result result = new Result();
    result.setSuccess(true);
    result.setObj(obj);
    result.setCount(count);
    return result;
    }
    /**
    * 分页查询
    * @param list 查询结果集
    * @return {Object}
    */
    public Object renderPage(List list) {
    Result result = new Result();
    result.setSuccess(true);
    result.setObj(list);
    if(list!=null)result.setCount(list.size());
    return result;
    }
    }

  • 相关阅读:
    sql
    Java后台通过传参得到对应的坐标值的方法
    运行第一个大型项目工程时出现的问题
    对sqlserver存储过程合游标循环中的一点心得
    (基于Java)算法之动态规划——矩阵连乘问题
    算法之线性时间选择(最坏情况下)
    算法之快速排序
    算法之合并排序
    算法之二分搜索法
    设计模式之装饰者模式
  • 原文地址:https://www.cnblogs.com/kekang/p/5993356.html
Copyright © 2020-2023  润新知