zoukankan      html  css  js  c++  java
  • 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;
    }
    }

  • 相关阅读:
    C# -- 使用缓冲区进行文件下载操作
    C# -- 使用ODBC连接数据库
    C# -- Quartz.Net入门案例
    C# -- LinkedList的使用
    ASP.NET -- 获取浏览器信息
    Windows -- 从注册表删除IE浏览器加载项
    C# -- FTP上传下载
    C# -- 使用Ping检查网络是否正常
    WinForm -- 为TextBox文本框添加鼠标右键菜单
    C# -- 使用Parallel并行执行任务
  • 原文地址:https://www.cnblogs.com/kekang/p/5993356.html
Copyright © 2011-2022 走看看