List相互转换对象转换
有了bean⼯具类,List相互转换 对象转换就简单多了:
列如:
-- List相互转换
//查询数据
List<RightsCoupon> fileList=this.query().eq("id",id).eq("active_flag",EnumConstant.Value()).orderBy(true, true, "sort").list();
//定义⼀个实体类 RightsCouponDto
List<RightsCouponDto> rightsCouponList = vertListToList(rightsCouponDtoList, new TypeReference<List<RightsCouponDto>>() {}); -- 对象相互转换
//查询出数据
TMemberBasic basic = ById(id);
//定义⼀个实体类(字段和类型跟TMemberBasic⾥要⼀样)
UseDto useDto = new UseDto();
bean⼯具类:
package com.oolbox.utils;
import com.alibaba.fastjson.JSON;
import com.ype.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
slf4j.Slf4j;
import flect.Field;
import flect.ParameterizedType;
import java.util.Arrays;
import java.util.List;
import Matcher;
import Pattern;
/**
* bean⼯具类
*/
@Slf4j
public class BeanUtil {
/
**
* 复制 2个bean的属性值
* @param source
* @param target
* @param propertiesNames
*/
public static void copyProperties(Object source,Object target,List<String> propertiesNames){
// 如果属性列表不存在,将把 source的所有属性设置为属性列表
if (propertiesNames==null || propertiesNames.size() == 0){
Arrays.Class().getDeclaredFields()).forEach(it->{
try {
}catch (Exception e){
log.Message());
}
});
}else {
object topropertiesNames.forEach(it->{
try {
Field fd = Class().getDeclaredField(it);
}catch (Exception e){
log.Message());
}
});
}
}
/**
* 判断当前属性是否存在此对象中
* @param obj
* @param propName
* @return
*/
public static Boolean checkProperties(Object obj,String propName){
try {
return true;
}catch (Exception e){
return false;
}
}
/**
* 设置对象中属性的值
* @param obj
* @param propName
* @param val
*/
public  static void setProperties(Object obj,String propName,Object val){
try {
Field Class().getDeclaredField(propName);
filed.setAccessible(true);
GenericType() instanceof List||GenericType() instanceof ParameterizedType){                filed.set(obj,JSON.String(),Type()));
}else {
filed.set(obj,val);
}
}catch (Exception e){
log.Message());
}
}
/**
* 转换对象名称,去掉第⼀个"T" 字符
* 例如 convertBeanName(TUserBasic.class,":") TUserBasic -> user:basic
* @param obj
* @param separator
* @return
*/
public static String convertBeanName(Object obj,String separator){
String className = Class().getSimpleName();
// 获取第⼀个字符,判断是否是"T"
String firstChar = className.substring(0,1);
if (firstChar.equals("T")){
if (firstChar.equals("T")){
className = className.substring(1,className.length());
}
StringBuilder resString = new StringBuilder();
Pattern pattern = Patternpile("[A-Z]([a-z\\d]+)?");
Matcher matcher = pattern.matcher(className);
while (matcher.find()) {
String word = up();
resString.UpperCase());
resString.d() == className.length() ? "" : separator);
}
String();
}
/**
* 对象转换
*/
public static <T> T ObjectToObject(Object source,Class<T> target){
try {
return JSON.JSONString(source),target);
}catch (Exception e){
return null;
}
}
/*
*列表对象转换
*/
public static <T> List<T> convertListToList(List bList, TypeReference typeRef) {
if (bList == null) return null;
ObjectMapper objectMapper = new ObjectMapper();
List<T> list = null;
try {
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static Field getDeclaredField(Object object, String fieldName){
Field field = null ;
Class<?> clazz = Class() ;
for(; clazz != Object.class ; clazz = Superclass()) {
try {
field = DeclaredField(fieldName) ;
return field ;
} catch (Exception e) {
//这⾥甚么都不能抛出去。
//如果这⾥的异常打印或者往外抛,则就不会进⼊
}
}
return null;
}
/**
/**
* 直接读的属性值, 忽略 private/protected 修饰符, 也    * @param object : ⼦类对象    * @param fieldName : ⽗类中    * @return : ⽗类中    */
public static Object getFieldValue(Object object, String fieldName){
//根据对象和属性名通过取 Field对象
Field field = getDeclaredField(object, fieldName) ;
//抑制Java对其的检查
field.setAccessible(true) ;
try {
//获的属性值
(object) ;
} catch(Exception e) {
e.printStackTrace() ;
}
return null;
}
}