データ転送の問題を解決するためにJavaのリフレクションメカニズムは空です

免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
このリンク: https://blog.csdn.net/ng_xiaohe/article/details/102738586

BeanUtils.copyProperties(x、y)を解くための小さな二つの方法、問題は、空のソースオブジェクトであります

マップのキー1.エンティティアノテーションデータベースフィールド、非ヌルデータパッケージ値に必要とされる
@Override
パブリックマップは<文字列オブジェクト> setNodeParamItems(DispatchInfoItem dispatchInfoItem)がないNoSuchMethodException、にInvocationTargetException、IllegalAccessExceptionが{スロー
マップの<string、オブジェクト> =地図()> <HashMapの新しい新;
dispatchInfo dispatchInfo新しい新しいdispatchInfo =();
IF(!= NULL dispatchInfoItem){
BeanUtils.copyProperties(dispatchInfoItem、dispatchInfo);
}
メソッド、[] = dispatchInfo.getClass方法()getDeclaredMethods();.
IF( !方法= NULL){
:(メソッド){メソッド、メソッドのために
、文字列methodNameの= method.getName()
){IF(methodName.startsWith( "GET")
カラムカラム= dispatchInfo.getClass()getDeclaredMethod(methodNameの).getAnnotation(Column.class)。
オブジェクト値= method.invoke(dispatchInfo)。
(!= NULLカラム&& StringUtils.isNotBlank(StringHelper.getString(値))){もし
map.put(column.name()、値)。
}
}
}
}
リターンマップ。
}
2.根据获取的值注入;
公共ボイドgetMethods(DispatchInfo dispatchInfo、地図の<string、オブジェクト>マップ)例外{スロー
//获取方法上的注解值
方法[]メソッド= dispatchInfo.getClassを()getDeclaredMethods()。
IF(!メソッド= NULL){
(方法方法:方法)のための{
文字列methodNameの= method.getName()。
{(methodName.startsWith()「GET」)の場合
カラムカラム= dispatchInfo.getClass()getDeclaredMethod(methodNameの).getAnnotation(Column.class)。
(もし!列= NULL){
文字列setMethodName = methodName.replaceFirst( "(取得)"、「セット」);
方法のsetMethod = dispatchInfo.getClass()getMethod(setMethodName、method.getReturnType())。
;
IF(ヌル= map.get(column.name())!){
setMethod.invoke(dispatchInfo、map.get(column.name()))。
}
}
}
}
}
}
3.根据值进行实际的操作

おすすめ

転載: blog.csdn.net/ng_xiaohe/article/details/102738586