Java工作笔记-类型转换的一种思路(前后端分离、反射)
生活随笔
收集整理的這篇文章主要介紹了
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);} }?
總結
以上是生活随笔為你收集整理的Java工作笔记-类型转换的一种思路(前后端分离、反射)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机网络-RIP与OSPF
- 下一篇: Linux笔记-bash批量启动、停止、