• 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> {
    }

    测试:

     

  • 相关阅读:
    Kubernetes 弹性伸缩全场景解析 (四)- 让核心组件充满弹性
    15分钟在笔记本上搭建 Kubernetes + Istio开发环境
    idea 插件的使用
    jQuery获取select元素选择器练习
    【Maven】添加ueditor到maven本地仓库
    jQuery序列化乱码解决
    Linux安装RedHat
    MyBatis SQL xml处理小于号与大于号
    js判断数据类型
    基于SSM框架的通用权限框架设计
  • 原文地址:https://www.cnblogs.com/yscec/p/13028756.html
Copyright © 2020-2023  润新知