zoukankan      html  css  js  c++  java
  • java返回数据的封装

    package entity;
    
    /**
     * 返回结果实体类
     */
    public class Result<T> {
    
        private boolean flag;//是否成功
        private Integer code;//返回码
        private String message;//返回消息
    
        private T data;//返回数据
    
        public Result(boolean flag, Integer code, String message, Object data) {
            this.flag = flag;
            this.code = code;
            this.message = message;
            this.data = (T)data;
        }
    
        public Result(boolean flag, Integer code, String message) {
            this.flag = flag;
            this.code = code;
            this.message = message;
        }
    
        public Result() {
            this.flag = true;
            this.code = StatusCode.OK;
            this.message = "执行成功";
        }
    
        public boolean isFlag() {
            return flag;
        }
    
        public void setFlag(boolean flag) {
            this.flag = flag;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public T getData() {
            return data;
        }
    
        public void setData(T data) {
            this.data = data;
        }
    }
    
    package entity;
    
    /**
     * 返回码
     */
    public class StatusCode {
    
        public static final int OK=20000;//成功
        public static final int ERROR =20001;//失败
        public static final int LOGINERROR =20002;//用户名或密码错误
        public static final int ACCESSERROR =20003;//权限不足
        public static final int REMOTEERROR =20004;//远程调用失败
        public static final int REPERROR =20005;//重复操作
    
    }
    
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false)  Brand brand, @PathVariable  int page, @PathVariable  int size){
            //调用BrandService实现分页条件查询Brand
            PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
            return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }
    
    /***
         * Brand多条件分页查询
         * @param brand
         * @param page
         * @param size
         * @return
     */
     PageInfo<Brand> findPage(Brand brand, int page, int size);
     /**
         * Brand条件+分页查询
         * @param brand 查询条件
         * @param page 页码
         * @param size 页大小
         * @return 分页结果
         */
        @Override
        public PageInfo<Brand> findPage(Brand brand, int page, int size){
            //分页
            PageHelper.startPage(page,size);
            //搜索条件构建
            Example example = createExample(brand);
            //执行搜索
            return new PageInfo<Brand>(brandMapper.selectByExample(example));
        }
    

      

    /**
         * Brand构建查询对象
         * @param brand
         * @return
         */
        public Example createExample(Brand brand){
            Example example=new Example(Brand.class);
            Example.Criteria criteria = example.createCriteria();
            if(brand!=null){
                // 品牌id
                if(!StringUtils.isEmpty(brand.getId())){
                        criteria.andEqualTo("id",brand.getId());
                }
                // 品牌名称
                if(!StringUtils.isEmpty(brand.getName())){
                        criteria.andLike("name","%"+brand.getName()+"%");
                }
                // 品牌图片地址
                if(!StringUtils.isEmpty(brand.getImage())){
                        criteria.andEqualTo("image",brand.getImage());
                }
                // 品牌的首字母
                if(!StringUtils.isEmpty(brand.getLetter())){
                        criteria.andEqualTo("letter",brand.getLetter());
                }
                // 排序
                if(!StringUtils.isEmpty(brand.getSeq())){
                        criteria.andEqualTo("seq",brand.getSeq());
                }
            }
            return example;
        }
    
    public interface BrandMapper extends Mapper<Brand> {
    }

    测试:

     

  • 相关阅读:
    Confluence 6 使用 WebDAV 客户端来对页面进行操作
    Confluence 6 的 WebDAV 客户端整合介绍
    Confluence 6 MySQL 3.x 字符集编码问题
    Confluence 6 € 欧元字符集不能正常显示
    Confluence 6 字符集编码的问题解决
    Confluence 6 数据库字符集编码和问题
    Confluence 6 配置字符集编码
    Confluence 6 邮件队列
    pyDay6
    c++第十六天
  • 原文地址:https://www.cnblogs.com/yscec/p/13028756.html
Copyright © 2011-2022 走看看