Java工作笔记-类型转换的一种思路(前后端分离、反射)

这里主要是一种思路,前端可能会发送ajax请求。这个内容是json数据。这里面的数据可能全部是以字符串为主。

通常可以通过反射进行改变,如下的代码:

如下要转换成这些类型

不然当时候赋值的时候就麻烦了。

程序运行截图如下:

程序结构如下:

ClassOne.java

package com.it1995.object;

import java.math.BigDecimal;
import java.sql.Timestamp;

public class ClassOne {

    private String stringType;
    private Integer integerType;
    private BigDecimal bigDecimalType;
    private int intType;
    private double doubleType;
    private Timestamp timestampType;

    public String getStringType() {
        return stringType;
    }

    public void setStringType(String stringType) {
        this.stringType = stringType;
    }

    public Integer getIntegerType() {
        return integerType;
    }

    public void setIntegerType(Integer integerType) {
        this.integerType = integerType;
    }

    public BigDecimal getBigDecimalType() {
        return bigDecimalType;
    }

    public void setBigDecimalType(BigDecimal bigDecimalType) {
        this.bigDecimalType = bigDecimalType;
    }

    public int getIntType() {
        return intType;
    }

    public void setIntType(int intType) {
        this.intType = intType;
    }

    public double getDoubleType() {
        return doubleType;
    }

    public void setDoubleType(double doubleType) {
        this.doubleType = doubleType;
    }

    public Timestamp getTimestampType() {
        return timestampType;
    }

    public void setTimestampType(Timestamp timestampType) {
        this.timestampType = timestampType;
    }
}

EntityAnalyse.java

package com.it1995.tool;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class EntityAnalyse {

    public static Map<String, Object> mapConvertDataType(String tableName, Map<String, Object> map) throws ClassNotFoundException, NoSuchFieldException{

        Map<String, Object> ret = new HashMap<>();

        String packageName = "com.it1995.object.";
        String entityName = packageName + tableName;
        Class entityClass = Class.forName(entityName);

        Set<String> colKeys = map.keySet();

        for(String string : colKeys){

            Field declaredField = entityClass.getDeclaredField(string);
            if(declaredField.getType() == String.class){

                ret.put(string, map.get(string).toString());
            }
            else if(declaredField.getType() == Integer.class){

                ret.put(string, Integer.valueOf(map.get(string).toString()));
            }
            else if(declaredField.getType() == BigDecimal.class){

                ret.put(string, new BigDecimal(map.get(string).toString()));
            }
            else if(declaredField.getType() == int.class){

                ret.put(string, Integer.parseInt(map.get(string).toString()));
            }
            else if(declaredField.getType() == double.class){

                ret.put(string, Double.parseDouble(map.get(string).toString()));
            }
            else if(declaredField.getType() == Timestamp.class){

                ret.put(string, Timestamp.valueOf(map.get(string).toString()));
            }
        }

        return ret;
    }
}

Main.java

package com.it1995;

import com.it1995.tool.EntityAnalyse;

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws NoSuchFieldException, ClassNotFoundException {

        String className = "ClassOne";
        Map<String, Object> original = new HashMap<>();
        original.put("stringType", "呵呵_呵呵_呵呵");
        original.put("integerType", "10");
        original.put("bigDecimalType", "10.08");
        original.put("intType", "20");
        original.put("doubleType", "24.585");
        original.put("timestampType", "2020-04-03 14:21:12.123");

        Map<String, Object> stringObjectMap = EntityAnalyse.mapConvertDataType(className, original);
        System.out.println(stringObjectMap);
    }
}
发布了1327 篇原创文章 · 获赞 5627 · 访问量 221万+

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/105309650
今日推荐