項目結構
分頁

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 +
'}';
}
}
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(); } }

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 }
定義頁面跳轉共用控制器

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 }