• SpringBoot实战项目(二)--项目目录结构及定义公共基础类


    项目结构

     分页

    package com.beilin.result;
    
    import lombok.Data;
    import lombok.extern.slf4j.Slf4j;
    
    import java.io.Serializable;
    
    /**
     * 分页查询参数
     */
    @Slf4j
    @Data
    public class Page implements Serializable {
    
        private static final long serialVersionUID = 7328071045193618467L;
        private Integer page;//当前页
        private Integer limit;//每页显示多少条
        private Integer offset;//数据库偏移量
    
        public void countOffset() {
            if (null == this.page || null == this.limit) {
                this.offset = 0;
                return;
            }
            this.offset = (this.page - 1) * this.limit;
        }
    
    
        @Override
        public String toString() {
            return "PageTableRequest{" +
                    "page=" + page +
                    ", limit=" + limit +
                    ", offset=" + offset +
                    '}';
        }
    }
    Page

    Json统一结果返回类 跟 公共请求信息

    package com.beilin.result;
    
    import lombok.Data;
    
    import java.io.Serializable;
    import java.util.List;
    
    @Data
    public class Results<T>  implements Serializable {
    
        int count;//数据数量
        Integer code;//代码
        String msg;//信息
        List<T> datas;//返回数据
        T data;//任何类型条件
    
        public Results() {
        }
        public Results(Integer code, String msg) {
            super();
            this.code = code;
            this.msg = msg;
        }
    
    
        public Results(Integer code, String msg, T data, Integer count, List<T> datas) {
            this.code = code;
            this.msg = msg;
            this.data = data;
            this.count = count;
            this.datas = datas;
        }
    
    
        /* 无数据传输的 成功返回 */
        public static <T> Results<T> success() {
            return new Results<T>( ResponseCode.SUCCESS.getCode(),  ResponseCode.SUCCESS.getMessage());
        }
    
        public static <T> Results<T> success(String msg) {
            return new Results<T>(ResponseCode.SUCCESS.getCode(), msg);
        }
    
        public static <T> Results<T> success(ResponseCode resultCode) {
            return new Results<T>( resultCode.getCode(),  resultCode.getMessage());
        }
    
        /* 单个数据传输的 成功返回 */
        public static <T> Results<T> success(T data) {
            return new Results<T>( ResponseCode.SUCCESS.getCode(),  ResponseCode.SUCCESS.getMessage(), data, 0, null);
        }
         public static <T> Results<T> success( List<T> datas) {
            return new Results<T>(ResponseCode.TABLE_SUCCESS.getCode(),ResponseCode.SUCCESS.getMessage(),null,0,datas);
        }
    
        public static <T> Results<T> success(String msg, T data) {
            return new Results<T>(ResponseCode.SUCCESS.getCode(), msg, data, 0, null);
        }
    
        public static <T> Results<T> success(ResponseCode resultCode, T data) {
            return new Results<T>( resultCode.getCode(),  resultCode.getMessage(), data, 0, null);
        }
    
        /* 分页数据传输的 成功返回 */
        public static <T> Results<T> success(Integer count, List<T> datas) {
            return new Results<T>(ResponseCode.TABLE_SUCCESS.getCode(),ResponseCode.SUCCESS.getMessage(),null,count, datas);
        }
    
        public static <T> Results<T> success(String msg, Integer count, List<T> datas) {
            return new Results<T>(ResponseCode.TABLE_SUCCESS.getCode(), msg, null, count, datas);
        }
    
        public static <T> Results<T> success(ResponseCode resultCode, Integer count, List<T> datas) {
            return new Results<T>( resultCode.getCode(),  resultCode.getMessage(), null, count, datas);
        }
        /* 无数据传输的 失败返回 */
        public static <T> Results<T> failure() {
            return new Results<T>( ResponseCode.FAIL.getCode(), ResponseCode.FAIL.getMessage());
        }
    
        public static <T> Results<T> failure(ResponseCode resultCode) {
            return new Results<T>( resultCode.getCode(),  resultCode.getMessage());
        }
    
        public static <T> Results<T> failure(Integer code, String msg) {
            return new Results<T>( code,  msg);
        }
    
        public static Results ok() {
            return new Results();
        }
    
    }
    Results
     1 package com.beilin.result;
     2 
     3 public enum  ResponseCode {
     4 
     5     // 公共请求信息
     6     SUCCESS(200, "请求成功"),
     7     TABLE_SUCCESS(0, "请求成功"),
     8     FAIL(500, "请求失败,请联系实训邦小姐姐"),
     9     PARAMETER_MISSING(600,"参数缺失"),
    10     UNAUTHORIZED(401,"未授权"),
    11     // ..一真往后面加
    12 
    13     //用户信息
    14     //5000100 - 5000200
    15     USERNAME_REPEAT(5000100,"用户名已存在"),
    16     PHONE_REPEAT(5000101,"手机号已存在"),
    17     EMAIL_REPEAT(5000102,"邮箱已存在"),
    18     //用户-角色
    19     //5000201 - 5000300
    20     USER_ROLE_NO_CLEAR(5000201,"该角色存在用户关联,无法删除")
    21 
    22 ;
    23     private Integer code;
    24     
    25     private String message;
    26     
    27     ResponseCode(Integer code, String message) {
    28         this.code = code;
    29         this.message = message;
    30     }
    31 
    32     public Integer getCode() {
    33         return code;
    34     }
    35 
    36     public void setCode(Integer code) {
    37         this.code = code;
    38     }
    39 
    40     public String getMessage() {
    41         return message;
    42     }
    43 
    44     public void setMessage(String message) {
    45         this.message = message;
    46     }
    47 
    48     public static String getMessage(String name) {
    49         for (ResponseCode item : ResponseCode.values()) {
    50             if (item.name().equals(name)) {
    51                 return item.message;
    52             }
    53         }
    54         return null;
    55     }
    56 
    57     public static Integer getCode(String name) {
    58         for (ResponseCode item : ResponseCode.values()) {
    59             if (item.name().equals(name)) {
    60                 return item.code;
    61             }
    62         }
    63         return null;
    64     }
    65 }
    ResponseCode

    定义页面跳转共用控制器

     1 package com.beilin.controller;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.servlet.ModelAndView;
     6 
     7 @Controller
     8 @RequestMapping("${api-url}")
     9 public class ApiController {
    10 
    11     @RequestMapping("/getPage")
    12     public ModelAndView getPage(ModelAndView model,String pageName){
    13         model.setViewName(pageName);
    14         return model;
    15     }
    16 
    17 }
    ApiController
  • 相关阅读:
    composer的使用
    tp5短信接口的使用
    PHP序列化与反序列化
    PHP 的oop思想
    php单例模式
    统计图的使用(chart)
    jq的时间插件
    php中Excel操作
    Linux 常用命令
    think cmfx目录结构
  • 原文地址:https://www.cnblogs.com/wx60079/p/12676621.html
Copyright © 2020-2023  润新知