Map與Bean互轉(下劃線轉大寫)
原創:點選開啟連結import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; /** * Map與Bean互轉 * * * 當把Person類作為BeanUtilTest的內部類時,程式出錯<br> * java.lang.NoSuchMethodException: Property '**' has no setter method<br> * 本質:內部類 和 單獨檔案中的類的區別 <br> * BeanUtils.populate方法的限制:<br> * The class must be public, and provide a public constructor that accepts no arguments. <br> * This allows tools and applications to dynamically create new instances of your bean, <br> * without necessarily knowing what Java class name will be used ahead of time */ public class MapBeanConvertUtil { /** * <p class="detail"> * 功能:Map --> Bean 2: 利用org.apache.commons.beanutils 工具類實現 Map --> Bean * 推薦使用 * </p> * @author wuxw * @param map * @param obj * @throws */ public static void transMap2Bean(Map<String, Object> map, Object obj) { if (map == null || obj == null) { return; } try { BeanUtils.populate(obj, map); } catch (Exception e) { System.out.println("transMap2Bean2 Error " + e); } } // Map --> Bean 1: 利用Introspector,PropertyDescriptor實現 Map --> Bean public static void transMap2Bean2(Map<String, Object> map, Object obj) { try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (map.containsKey(key)) { Object value = map.get(key); // 得到property對應的setter方法 Method setter = property.getWriteMethod(); setter.invoke(obj, value); } } } catch (Exception e) { System.out.println("transMap2Bean Error " + e); } return; } /** * <p class="detail"> * 功能: Map --> Bean 1: 利用Introspector,PropertyDescriptor實現 Map --> Bean * 固定hibernateBean型別轉換: user_name 轉 userName ,注意嚴格驗證引數 * * </p> * @author wuxw * @param map * @param obj * @throws */ public static void transMap2HiberanteBean(Map<String, Object> map, Object obj) { try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); //將key中大寫字元轉為_大寫 key = underscoreName(key); if (map.containsKey(key)) { Object value = map.get(key); // 得到property對應的setter方法 Method setter = property.getWriteMethod(); setter.invoke(obj, value); } } } catch (Exception e) { System.out.println("transMap2Bean Error " + e); } return; } // Bean --> Map 1: 利用Introspector和PropertyDescriptor 將Bean --> Map public static Map<String, Object> transBean2Map(Object obj) { if(obj == null){ return null; } Map<String, Object> map = new HashMap<String, Object>(); try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); // 過濾class屬性 if (!key.equals("class")) { // 得到property對應的getter方法 Method getter = property.getReadMethod(); Object value = getter.invoke(obj); map.put(key, value); } } } catch (Exception e) { System.out.println("transBean2Map Error " + e); } return map; } /** * <p class="detail"> * 功能:將大寫轉為下劃線,且小寫 如: userName > user_name * </p> * @author wuxw * @param name * @return * @throws */ static String underscoreName(String name) { StringBuilder result = new StringBuilder(); if ((name != null) && (name.length() > 0)) { result.append(name.substring(0, 1).toLowerCase()); for (int i = 1; i < name.length(); ++i) { String s = name.substring(i, i + 1); if (s.equals(s.toUpperCase())) { result.append("_"); result.append(s.toLowerCase()); } else { result.append(s); } } } return result.toString(); } }
相關推薦
Map與Bean互轉(下劃線轉大寫)
import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method;
Java的xml與map,與Bean互轉
dde ppr XmlWriter while 信息 XML map.entry war 銀行 xml與map互轉,主要使用dom4j import org.dom4j.Document; import org.dom4j.DocumentException; i
Json與bean互轉的例子
第一種: public class JsonToJavaBean { public static void main(String[] args) { String str="{\"student\
表字段名與關鍵字衝突問題與mybatis通用mapper查詢時欄位名駝峰轉下劃線
表字段名與關鍵字衝突問題引發的一系列修改欄位名的風波。 因為建表的時候沒有注意欄位名,使用了關鍵字,加上了飄號,所以與關鍵字衝突分時候查詢的時候必須加上飄號進行查詢,但是在使用mybatis通用mapper進行查詢的時候,它自動生成的sql語句中國是不帶飄號的
Map與JavaBean互轉原理(內省機制)
1.javabean 轉 map 定義一個Person的javabean public class Person { private String name
Json與Bean互轉,Timestamp型別的問題
Json與Java Bean互相轉換時,Bean中的Timestamp欄位是無法直接處理的,需要實現兩個轉換器。 DateJsonValueProcessor的作用是Bean轉換為Json時將Timepstamp轉換為指定的時間格式。 import java.text.D
js對象屬性名駝峰式轉下劃線
首字母 字符 {} com 存放位置 light 通過 slice 分享 一、題目示例: 思路: 1、匹配屬性名字符串中的大寫字母和數字 2、通過匹配後的lastIndex屬性獲取匹配到的大寫字母和數字的位置 3、判斷大寫字母的位置是否為首位置以及lastIndex
js物件屬性駝峰式命名(帶數字)轉下劃線命名
將類似於 info 格式轉化為 info2 var info = { id: 1, id1: 2, userName1: '劉玄德', userName2: '劉玄德', userName3: '大哥',
springboot中介面駝峰自動轉下劃線以及跨域程式碼塊
@Configuration public class AdditionalConfig { /**介面中,自動轉換的有:駝峰轉換為下劃線,空值輸出null*/ @Bean public Jackson2ObjectMapperBuilderCustomizer cus
java字串下劃線轉駝峰工具使用
java字串下劃線轉駝峰工具使用 public class StringUtil { private static final char UNDERLINE='_'; /** * 下劃線 轉 駝峰 * @param param * @return
spring boot 自定義引數解析器實現form表單型別請求或位址列請求引數下劃線轉駝峰屬性
一、定義引數解析註解 @Target(value = ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) public @interface ParameterModel { } 二、定義抽象類AbstractCustomiz
js物件屬性名駝峰式轉下劃線
一、題目示例: 思路: 1、匹配屬性名字串中的大寫字母和數字 2、通過匹配後的lastIndex屬性獲取匹配到的大寫字母和數字的位置 3、判斷大寫字母的位置是否為首位置以及lastIndex是否為0,為0則表示匹配結束 4、將存放位置的陣列進行從小到大排序,排序後將屬性名按照字串的slice方法切割
java駝峰轉下劃線,下劃線轉駝峰
package com.starry.test; import java.util.regex.Matcher; import java.util.regex.Pattern; public cl
Mybatis 結果對映下劃線轉駝峰
mybatis 結果對映下劃線轉駝峰 Spring Boot 配置: #下劃線轉駝峰 mybatis.configuration.map-underscore-to-camel-case=true mybatis XML 配置 <configuration> <!-- 全
mybatis 下劃線轉駝峰
MybatisConfiguration configuration = new MybatisConfiguration(); configuration.setMapUnderscoreToCamelCase(true); //開啟下劃線轉駝峰
命名更改小結:下劃線轉駝峰,或駝峰轉下劃線
1、下劃線轉駝峰 const toCamel = str =>str.replace(/([^_])(?:_+([^_]))/g, (_,p1, p2)=>p1+p2.toUpperCas
java 下劃線轉駝峰
/** * 將駝峰式命名的字串轉換為下劃線大寫方式。如果轉換前的駝峰式命名的字串為空,則返回空字串。</br> * 例如:HelloWorld->HELLO_WORLD * @param name 轉換前
下劃線轉駝峰,3種實現方式效率對比
一時興起想要比較一下效率如何,也順便貼上程式碼。總體而言,無論是駝峰轉下劃線還是下劃線轉駝峰,一般而言都不會超過5個毫秒,但是這個功能畢竟簡單,積少成多之下,也會顯得有些慢。鑑於毫秒的精度不夠,我採用納秒來量化執行效率。結論是:1.在使用java自帶的正則表示式工具Patte
Java 駝峰字串轉下劃線連線
程式碼: @Test public void test8() { String str = "userLevelAndAge"; String res = String.join("_", str.repla
java常用工具類(一)—— Map 與 Bean 之間的互相轉換
import net.bytebuddy.implementation.bytecode.Throw; import org.springframework.cglib.beans.BeanMap; import java.beans.PropertyDescriptor; import java.lang