spring*的一些開發技巧或者工具

1、當某字段的記錄不能重複時,可在數據庫中創建唯一索引,這樣就不用在業務代碼中寫判斷。

2、可能不按照主鍵查詢的話,添加單索引,也能添加組合索引,即傳兩個及以上的id

3、時間戳,數據記錄時間,數據最後更新時間。記錄時間只在創建時寫入,之後不會改變,這樣做爲了方便之後的維護,而且更新時間可寫在sql中,在代碼中可不操作更新時間字段

4、mybatis

    4.1 mybatis-generator 自動生成與數據庫交互的代碼

    4.2 mybatis-plugin idea插件,實現接口與xml之間的互相跳轉,找來找去

    4.3 mybatis-pagehelper 分頁組件,開源

5、在簡單常量的分類時,可選擇使用內部接口,複雜時還是用枚舉或其他吧

6、解決浮點型商業運算中丟失精度使用Bigdecimal

 //加
    public static BigDecimal add(double d1,double d2){
        BigDecimal b1 = new BigDecimal(Double.toString(d1));
        BigDecimal b2 = new BigDecimal(Double.toString(d2));
        return b1.add(b2);
    }
    //減
    public static BigDecimal sub(double d1,double d2){
        BigDecimal b1 = new BigDecimal(Double.toString(d1));
        BigDecimal b2 = new BigDecimal(Double.toString(d2));
        return b1.subtract(b2);
    }
    //乘
    public static BigDecimal mul(double d1,double d2){
        BigDecimal b1 = new BigDecimal(Double.toString(d1));
        BigDecimal b2 = new BigDecimal(Double.toString(d2));
        return b1.multiply(b2);
    }
    //除
    public static BigDecimal div(double d1,double d2){
        BigDecimal b1 = new BigDecimal(Double.toString(d1));
        BigDecimal b2 = new BigDecimal(Double.toString(d2));
        return b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//保留兩位小數,四捨五入
    }

實際使用時,使用Bigdecimal的String構造方法

例:

BigDecimal bigDecimal = new BigDecimal("0");

7、返回Json數據的話,定義返回格式類

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//保證序列化json的時候,如果是null的對象,key也會消失
public class ServerResponse<T> implements Serializable {

//    狀態
    private int status;
//    內容
    private T data;
//    信息
    private String msg;

    private ServerResponse(int status) {
        this.status = status;
    }

    private ServerResponse(int status, T data) {
        this.status = status;
        this.data = data;
    }

    private ServerResponse(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    private ServerResponse(int status, T data, String msg) {
        this.status = status;
        this.data = data;
        this.msg = msg;
    }

    @JsonIgnore
    //使之不在json序列化結果當中
    public boolean isSuccess(){
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> ServerResponse<T> createBySuccess(){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String msg){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public static <T> ServerResponse<T> createBySuccess(T data){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }

    public static <T> ServerResponse<T> createBySuccess(T data,String msg){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data,msg);
    }

    public static <T> ServerResponse<T> createByError(){
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
    }

    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage){
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMessage);
    }

    public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage){
        return new ServerResponse<T>(errorCode,errorMessage);
    }

}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章