最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Springboot封裝一個自適配的數(shù)據(jù)單位轉(zhuǎn)換工具類

 更新時間:2023年03月08日 10:03:20   作者:小目標(biāo)青年  
我們在接收前臺傳輸?shù)臄?shù)據(jù)時,往往SpringBoot使用內(nèi)置的數(shù)據(jù)類型轉(zhuǎn)換器把我們提交的數(shù)據(jù)自動封裝成對象等類型,下面這篇文章主要給大家介紹了關(guān)于使用Springboot封裝一個自適配的數(shù)據(jù)單位轉(zhuǎn)換工具類的相關(guān)資料,需要的朋友可以參考下

前言

平時做一些統(tǒng)計數(shù)據(jù),經(jīng)常從數(shù)據(jù)庫或者是從接口獲取出來的數(shù)據(jù),單位是跟業(yè)務(wù)需求不一致的。

比如, 我們拿出來的, 實際上要是

又比如,我們拿到的數(shù)據(jù)需要 乘以100 返回給前端做 百分比展示

又比如, 千分比轉(zhuǎn)換

又比如,拿出來的金額需要變成 萬為單位

又比如,需要保留2位小數(shù)

......

等等等等。

平時我們怎么搞?

很多時候拿到的是一個數(shù)據(jù)集合list,就需要去遍歷然后根據(jù)每個DTO的屬性去做相關(guān)單位轉(zhuǎn)換。

一直get 完 set ,get 完 set ,get 完 set ,get 完 set ,get 完 set ,人都麻了。

就像這樣:

所以,如果通過反射自動匹配出來一些操作轉(zhuǎn)換,是不是就看代碼看起來舒服一點,人也輕松一點。

答案: 是的

然后,我就搞了。

本篇內(nèi)容簡要:

① 初步的封裝,通過map去標(biāo)記需要轉(zhuǎn)換的 類屬性字段

② 進一步的封裝, 配合老朋友自定義注解搞事情

產(chǎn)品:

支付總金額 換成萬 為單位, 方便運營統(tǒng)計 ;

那個什么計數(shù),要是百分比的 ;

然后還有一個是千分比;

另外,還有2個要保留2位小數(shù);

還有啊,那個。。。。。。

我:

別說了,喝口水吧。

拿到的數(shù)據(jù)都在這個DTO里面 :

開始封裝:

① 初步的封裝,通過map去標(biāo)記需要轉(zhuǎn)換的 類屬性字段

思路玩法: 

a.通過反射拿出字段

b.配合傳入的轉(zhuǎn)換標(biāo)記Map 匹配哪些字段需要操作

c.然后從map取出相關(guān)字段的具體操作是什么,然后執(zhí)行轉(zhuǎn)換操作

d.重新賦值 

① 簡單弄個枚舉,列出現(xiàn)在需求上的轉(zhuǎn)換操作類型

UnitConvertType.java

/**
 * @Author : JCccc
 * @CreateTime : 2023/01/14
 * @Description :
 **/
public enum UnitConvertType {
 
    /**
     * 精度
     */
    R,
    /**
     * 萬元
     */
    B,
    /**
     * 百分
     */
    PERCENTAGE,
    /**
     * 千分
     */
    PERMIL
}

② 核心封裝的轉(zhuǎn)換函數(shù) 

UnitConvertUtil.java

import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @Author : JCccc
 * @CreateTime : 2023/01/14
 * @Description :
 **/
@Slf4j
public class UnitConvertUtil {
 
    public static <T> void unitMapConvert(List<T> list, Map<String, UnitConvertType> propertyMap) {
        for (T t : list) {
            Field[] declaredFields = t.getClass().getDeclaredFields();
            for (Field declaredField : declaredFields) {
                if (propertyMap.keySet().stream().anyMatch(x -> x.equals(declaredField.getName()))) {
                    try {
                        declaredField.setAccessible(true);
                        Object o = declaredField.get(t);
                        UnitConvertType unitConvertType = propertyMap.get(declaredField.getName());
                        if (o != null) {
                            if (unitConvertType.equals(UnitConvertType.PERCENTAGE)) {
                                BigDecimal bigDecimal = ((BigDecimal) o).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
                                declaredField.set(t, bigDecimal);
                            }
                            if (unitConvertType.equals(UnitConvertType.PERMIL)) {
                                BigDecimal bigDecimal = ((BigDecimal) o).multiply(new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_UP);
                                declaredField.set(t, bigDecimal);
                            }
                            if (unitConvertType.equals(UnitConvertType.B)) {
                                BigDecimal bigDecimal = ((BigDecimal) o).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
                                declaredField.set(t, bigDecimal);
                            }
                            if (unitConvertType.equals(UnitConvertType.R)) {
                                BigDecimal bigDecimal = ((BigDecimal) o).setScale(2, BigDecimal.ROUND_HALF_UP);
                                declaredField.set(t, bigDecimal);
                            }
                        }
                    } catch (Exception ex) {
                        log.error("處理失敗");
                        continue;
                    }
 
                }
            }
        }
    }
    
    public static void main(String[] args) {
 
        //獲取模擬數(shù)據(jù)
        List<MySumReportDTO> list = getMySumReportList();
 
        Map<String, UnitConvertType> map =new HashMap<>();
        map.put("payTotalAmount", UnitConvertType.B);
        map.put("jcAmountPercentage", UnitConvertType.PERCENTAGE);
        map.put("jcCountPermillage", UnitConvertType.PERMIL);
        map.put("length", UnitConvertType.R);
        map.put("width", UnitConvertType.R);
        unitMapConvert(list,map);
        System.out.println("通過map標(biāo)識的自動轉(zhuǎn)換玩法:"+list.toString());
 
    }
 
    private static List<MySumReportDTO> getMySumReportList() {
        MySumReportDTO mySumReportDTO = new MySumReportDTO();
        mySumReportDTO.setPayTotalAmount(new BigDecimal(1100000));
        mySumReportDTO.setJcAmountPercentage(BigDecimal.valueOf(0.695));
        mySumReportDTO.setJcCountPermillage(BigDecimal.valueOf(0.7894));
        mySumReportDTO.setLength(BigDecimal.valueOf(1300.65112));
        mySumReportDTO.setWidth(BigDecimal.valueOf(6522.12344));
 
        MySumReportDTO mySumReportDTO1 = new MySumReportDTO();
        mySumReportDTO1.setPayTotalAmount(new BigDecimal(2390000));
        mySumReportDTO1.setJcAmountPercentage(BigDecimal.valueOf(0.885));
        mySumReportDTO1.setJcCountPermillage(BigDecimal.valueOf(0.2394));
        mySumReportDTO1.setLength(BigDecimal.valueOf(1700.64003));
        mySumReportDTO1.setWidth(BigDecimal.valueOf(7522.12344));
 
        List<MySumReportDTO> list = new ArrayList<>();
 
        list.add(mySumReportDTO);
        list.add(mySumReportDTO1);
        return list;
    }
}

代碼簡析: 

看看怎么調(diào)用的:

public static void main(String[] args) {

    //獲取模擬數(shù)據(jù)
    List<MySumReportDTO> list = getMySumReportList();
    System.out.println("轉(zhuǎn)換前:"+list.toString());
    Map<String, UnitConvertType> map =new HashMap<>();
    map.put("payTotalAmount", UnitConvertType.B);
    map.put("jcAmountPercentage", UnitConvertType.PERCENTAGE);
    map.put("jcCountPermillage", UnitConvertType.PERMIL);
    map.put("length", UnitConvertType.R);
    map.put("width", UnitConvertType.R);
    unitMapConvert(list,map);
    System.out.println("通過map標(biāo)識的自動轉(zhuǎn)換玩法:"+list.toString());
}

代碼簡析: 

效果:

整個集合list的 對應(yīng)字段都自動轉(zhuǎn)換成功(轉(zhuǎn)換邏輯想怎么樣就自己在對應(yīng)if里面調(diào)整、拓展): 

② 進一步的封裝, 配合老朋友自定義注解搞事情

實說實話,第一步的封裝程度已經(jīng)夠用了,就是傳map標(biāo)識出來哪些需要轉(zhuǎn)換,對應(yīng)轉(zhuǎn)換枚舉類型是什么。

其實我感覺是夠用的。

但是么,為了用起來更加方便,或者說 更加地可拓展, 那么配合自定義注解是更nice的。

開搞。

創(chuàng)建一個自定義注解 ,JcBigDecConvert.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
/**
 * @Author : JCccc
 * @CreateTime : 2023/01/14
 * @Description :
 **/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface JcBigDecConvert {
 
    UnitConvertType name();
}

怎么用? 就是在我們的報表DTO里面,去標(biāo)記字段。

示例:

MyYearSumReportDTO.java

ps: 可以看到我們在字段上面使用了自定義注解

import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
 
/**
 * @Author : JCccc
 * @CreateTime : 2023/2/03
 * @Description :
 **/
 
@Data
public class MyYearSumReportDTO implements Serializable {
    private static final long serialVersionUID = 5285987517581372888L;
 
    //支付總金額
    @JcBigDecConvert(name=UnitConvertType.B)
    private BigDecimal payTotalAmount;
 
    //jc金額百分比
    @JcBigDecConvert(name=UnitConvertType.PERCENTAGE)
    private BigDecimal jcAmountPercentage;
 
    //jc計數(shù)千分比
    @JcBigDecConvert(name=UnitConvertType.PERMIL)
    private BigDecimal jcCountPermillage;
 
    //保留2位
    @JcBigDecConvert(name=UnitConvertType.R)
    private BigDecimal length;
 
    //保留2位
    @JcBigDecConvert(name=UnitConvertType.R)
    private BigDecimal width;
}

然后針對配合我們的自定義,封一個轉(zhuǎn)換函數(shù),反射獲取屬性字段,然后解析注解,然后做對應(yīng)轉(zhuǎn)換操作。

 代碼:

    public static <T> void unitAnnotateConvert(List<T> list) {
        for (T t : list) {
            Field[] declaredFields = t.getClass().getDeclaredFields();
            for (Field declaredField : declaredFields) {
                    try {
                        if (declaredField.getName().equals("serialVersionUID")){
                            continue;
                        }
                        JcBigDecConvert myFieldAnn = declaredField.getAnnotation(JcBigDecConvert.class);
                        if(Objects.isNull(myFieldAnn)){
                            continue;
                        }
                        UnitConvertType unitConvertType = myFieldAnn.name();
                        declaredField.setAccessible(true);
                        Object o = declaredField.get(t);
                        if (Objects.nonNull(o)) {
                            if (unitConvertType.equals(UnitConvertType.PERCENTAGE)) {
                                BigDecimal bigDecimal = ((BigDecimal) o).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
                                declaredField.set(t, bigDecimal);
                            }
                            if (unitConvertType.equals(UnitConvertType.PERMIL)) {
                                BigDecimal bigDecimal = ((BigDecimal) o).multiply(new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_UP);
                                declaredField.set(t, bigDecimal);
                            }
                            if (unitConvertType.equals(UnitConvertType.B)) {
                                BigDecimal bigDecimal = ((BigDecimal) o).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
                                declaredField.set(t, bigDecimal);
                            }
                            if (unitConvertType.equals(UnitConvertType.R)) {
                                BigDecimal bigDecimal = ((BigDecimal) o).setScale(2, BigDecimal.ROUND_HALF_UP);
                                declaredField.set(t, bigDecimal);
                            }
                        }
                    } catch (Exception ex) {
                        log.error("處理失敗");
                    }
            }
        }
    }

寫個調(diào)用示例看看效果:

    public static void main(String[] args) {
        
        List<MyYearSumReportDTO> yearsList = getMyYearSumReportList();
        unitAnnotateConvert(yearsList);
        System.out.println("通過注解標(biāo)識的自動轉(zhuǎn)換玩法:"+yearsList.toString());
    }
    
    private static List<MyYearSumReportDTO> getMyYearSumReportList() {
        MyYearSumReportDTO mySumReportDTO = new MyYearSumReportDTO();
        mySumReportDTO.setPayTotalAmount(new BigDecimal(1100000));
        mySumReportDTO.setJcAmountPercentage(BigDecimal.valueOf(0.695));
        mySumReportDTO.setJcCountPermillage(BigDecimal.valueOf(0.7894));
        mySumReportDTO.setLength(BigDecimal.valueOf(1300.65112));
        mySumReportDTO.setWidth(BigDecimal.valueOf(6522.12344));
        MyYearSumReportDTO mySumReportDTO1 = new MyYearSumReportDTO();
        mySumReportDTO1.setPayTotalAmount(new BigDecimal(2390000));
        mySumReportDTO1.setJcAmountPercentage(BigDecimal.valueOf(0.885));
        mySumReportDTO1.setJcCountPermillage(BigDecimal.valueOf(0.2394));
        mySumReportDTO1.setLength(BigDecimal.valueOf(1700.64003));
        mySumReportDTO1.setWidth(BigDecimal.valueOf(7522.12344));
        
        List<MyYearSumReportDTO> list = new ArrayList<>();
        list.add(mySumReportDTO);
        list.add(mySumReportDTO1);
        return list;
    }

效果也是很OK:

拋磚引玉,傳遞‘玩’代碼思想,學(xué)編程,哎我就是玩。

總結(jié)

到此這篇關(guān)于使用Springboot封裝一個自適配的數(shù)據(jù)單位轉(zhuǎn)換工具類的文章就介紹到這了,更多相關(guān)Springboot數(shù)據(jù)單位轉(zhuǎn)換工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 顯示IntelliJ IDEA工具的Run Dashboard功能圖文詳解

    顯示IntelliJ IDEA工具的Run Dashboard功能圖文詳解

    這篇文章主要介紹了顯示IntelliJ IDEA工具的Run Dashboard功能,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • Spring Cloud出現(xiàn)Options Forbidden 403問題解決方法

    Spring Cloud出現(xiàn)Options Forbidden 403問題解決方法

    本篇文章主要介紹了Spring Cloud出現(xiàn)Options Forbidden 403問題解決方法,具有一定的參考價值,有興趣的可以了解一下
    2017-11-11
  • 詳解Spring Security認證流程

    詳解Spring Security認證流程

    這篇文章主要介紹了Spring Security認證流程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • jeefast和Mybatis實現(xiàn)三級聯(lián)動的示例代碼

    jeefast和Mybatis實現(xiàn)三級聯(lián)動的示例代碼

    這篇文章主要介紹了jeefast和Mybatis實現(xiàn)三級聯(lián)動的示例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • 關(guān)于Spring的@Transaction導(dǎo)致數(shù)據(jù)庫回滾全部生效問題(又刪庫跑路)

    關(guān)于Spring的@Transaction導(dǎo)致數(shù)據(jù)庫回滾全部生效問題(又刪庫跑路)

    使用@Transactional一鍵開啟聲明式事務(wù), 這就真的事務(wù)生效了?過于信任框架總有“意外驚喜”。本文通過案例給大家詳解關(guān)于Spring的@Transaction導(dǎo)致數(shù)據(jù)庫回滾全部生效問題,感興趣的朋友一起看看吧
    2021-05-05
  • Java中通過ZipOutputStream類如何將多個文件打成zip

    Java中通過ZipOutputStream類如何將多個文件打成zip

    ZipOutputStream?是Java中用于創(chuàng)建ZIP文件的類,它是?java.util.zip?包中的一部分,通過使用?ZipOutputStream?,可以將多個文件壓縮到一個ZIP文件中,這篇文章主要介紹了Java中(ZipOutputStream)如何將多個文件打成zip,需要的朋友可以參考下
    2023-09-09
  • JAVA中try-catch結(jié)構(gòu)之異常處理的使用方法

    JAVA中try-catch結(jié)構(gòu)之異常處理的使用方法

    Java編程中一個非常重要且實用的概念,可以幫助我們處理代碼運行時發(fā)生的異常情況,下面這篇文章主要給大家介紹了關(guān)于JAVA中try-catch結(jié)構(gòu)之異常處理的使用方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-09-09
  • SpringBoot讀取properties配置文件中的數(shù)據(jù)的三種方法

    SpringBoot讀取properties配置文件中的數(shù)據(jù)的三種方法

    本文主要介紹了SpringBoot讀取properties配置文件中的數(shù)據(jù)的三種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • 解決springboot中mongodb不啟動及Dao不能被掃描到的問題

    解決springboot中mongodb不啟動及Dao不能被掃描到的問題

    這篇文章主要介紹了解決springboot中mongodb不啟動及Dao不能被掃描到的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringBoot從Nacos讀取MySQL數(shù)據(jù)庫配置錯誤:Public Key Retrieval is not allowed的解決方案

    SpringBoot從Nacos讀取MySQL數(shù)據(jù)庫配置錯誤:Public Key Retrieva

    最近的項目,突然都從MySQL5.7升級到8.0了,有些項目能運行成功,有些項目遇到了問題,啟動不成功,顯示數(shù)據(jù)庫方面的異常信息,本文給大家介紹了SpringBoot從Nacos讀取MySQL數(shù)據(jù)庫配置錯誤:Public Key Retrieval is not allowed的解決方案,需要的朋友可以參考下
    2024-04-04

最新評論

土默特左旗| 莱西市| 武功县| 舒兰市| 时尚| 贺州市| 巴塘县| 广安市| 安新县| 华阴市| 凤城市| 桐庐县| 东乡县| 南乐县| 泸溪县| 齐河县| 建始县| 涟源市| 太仓市| 南康市| 英吉沙县| 崇明县| 吉安市| 如东县| 新泰市| 应用必备| 贵德县| 民县| 海兴县| 韶山市| 登封市| 左云县| 加查县| 将乐县| 开化县| 龙川县| 安陆市| 思茅市| 达孜县| 乐都县| 枣强县|