当前位置: 首页 > news >正文

显示网站建设精美页面wordpress工具包

显示网站建设精美页面,wordpress工具包,中企动力科技是国企吗,网站策划制作公司工具类|将Entity对象转为Vo/Bo对象#xff0c;并指定字段绑定 实体类#xff1a;People和Student,Student的三个字段和People意义一样#xff0c;但是字段名不完全一样#xff0c;要实现对象拷贝可使用如下工具类#xff0c;用到了反射。 People.java Data AllArgsConst…工具类|将Entity对象转为Vo/Bo对象并指定字段绑定 实体类People和Student,Student的三个字段和People意义一样但是字段名不完全一样要实现对象拷贝可使用如下工具类用到了反射。 People.java Data AllArgsConstructor NoArgsConstructor public class People {private Integer id;private String name;private Integer age;private String sex;private String classNum;private String health;private String height;private String weight; }Student.java Data AllArgsConstructor NoArgsConstructor public class Student {private Integer stuId;private String stuName;private Integer age; }CommonBeanUtils.java package cn.yto.hbd.utils;import lombok.extern.slf4j.Slf4j; import org.springframework.beans.*; import org.springframework.util.*; import javax.xml.datatype.XMLGregorianCalendar; import java.beans.PropertyDescriptor; import java.lang.reflect.*; import java.util.*;/*** Description: Bean对象转化Vo对象工具类*/ Slf4j public abstract class CommonBeanUtils extends org.springframework.beans.BeanUtils {/*** 对象赋值:将source对象与target对象按照匹配的字段一对一复制** param source 源Entity* param target 目标Vo对象* throws BeansException*/public static void copyProps(Object source, Object target) throws BeansException {Assert.notNull(source, Source must not be null);Assert.notNull(target, Target must not be null);Class? actualEditable target.getClass();PropertyDescriptor[] targetPds getPropertyDescriptors(actualEditable);for (PropertyDescriptor targetPd : targetPds) {if (targetPd.getWriteMethod() ! null) {PropertyDescriptor sourcePd getPropertyDescriptor(source.getClass(), targetPd.getName());if (sourcePd ! null sourcePd.getReadMethod() ! null) {try {Method readMethod sourcePd.getReadMethod();if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {readMethod.setAccessible(true);}Object value readMethod.invoke(source);// 这里判断以下value是否为空 当然这里也能进行一些特殊要求的处理 例如绑定时格式转换等等if (value ! null) {Method writeMethod targetPd.getWriteMethod();Type targetParameterType writeMethod.getGenericParameterTypes()[0];// 特殊类型不再执行copy XMLGregorianCalendarif (!(targetParameterType.equals(XMLGregorianCalendar.class))) {if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {writeMethod.setAccessible(true);}writeMethod.invoke(target, value);}}} catch (Throwable ex) {log.error(ex.getMessage());throw new FatalBeanException(Could not copy properties from source to target, ex);}}}}}/*** 对象赋值:将source对象与target按照匹配的字段一一复制** param source 源Entity* param target 目标Vo对象* param props 对应字段0或多个* throws BeansException*/public static void copyProps(Object source, Object target, final String... props) throws BeansException {Assert.notNull(source, Source must not be null);Assert.notNull(target, Target must not be null);Class? actualEditable target.getClass();PropertyDescriptor[] targetPds getPropertyDescriptors(actualEditable);for (PropertyDescriptor targetPd : targetPds) {if (targetPd.getWriteMethod() ! null) {String prop_1;String prop_2;String targetPdNameTemp ;for (int i 0; i props.length; i) {String[] params props[i].split();prop_1 params[0];prop_2 params[1];if (Objects.equals(targetPd.getName(), prop_1)) {targetPdNameTemp prop_2;break;}if (Objects.equals(targetPd.getName(), prop_2)) {targetPdNameTemp prop_1;break;}}//1.将字段转换实现字段绑定PropertyDescriptor sourcePd;//指定了的字段绑定的按绑定字段赋值if (!.equals(targetPdNameTemp) null ! targetPdNameTemp) {sourcePd getPropertyDescriptor(source.getClass(), targetPdNameTemp);} else {//没有指定字段的自动匹配字段名sourcePd getPropertyDescriptor(source.getClass(), targetPd.getName());}if (sourcePd ! null sourcePd.getReadMethod() ! null) {try {Method readMethod sourcePd.getReadMethod();if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {readMethod.setAccessible(true);}Object value readMethod.invoke(source);// 这里判断以下value是否为空 当然这里也能进行一些特殊要求的处理 例如绑定时格式转换等等if (value ! null) {Method writeMethod targetPd.getWriteMethod();Type targetParameterType writeMethod.getGenericParameterTypes()[0];// 特殊类型不再执行copy XMLGregorianCalendarif (!(targetParameterType.equals(XMLGregorianCalendar.class))) {if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {writeMethod.setAccessible(true);}writeMethod.invoke(target, value);}}} catch (Throwable ex) {log.error(ex.getMessage());throw new FatalBeanException(Could not copy properties from source to target, ex);}}}}}/*** 集合对象转化赋值如ListPeople 转为ListStudent实体Student的字段是People字段的子集** param sources 源集合对象* param voClass vo类型* param T* return*/public static T ListT copyListProps(List? extends Object sources, final ClassT voClass) {Assert.isTrue(!CollectionUtils.isEmpty(sources), Source must not be null);ListT targets new ArrayList();sources.forEach(source - {try {T target voClass.newInstance();copyProperties(source, target);targets.add(target);} catch (InstantiationException | IllegalAccessException e) {log.error(e.getMessage());}});return targets;}/*** 集合对象转化赋值如ListPeople 转为ListStudent实体Student的字段是People字段的子集** param sources 源Entity集合对象* param voClass vo类型.class* param T* return*/public static T ListT copyListProps(List? extends Object sources, final ClassT voClass, final String... props) {Assert.isTrue(!CollectionUtils.isEmpty(sources), Source must not be null);ListT targets new ArrayList();sources.forEach(source - {try {T target voClass.newInstance();//调用带参数绑定的拷贝方法copyProps(source, target, props);targets.add(target);} catch (InstantiationException | IllegalAccessException e) {log.error(e.getMessage());}});return targets;} }测试类 package cn.yto.hbd.utils;public class Test0 {public static void main(String[] args) {People people new People();Student student new Student();people.setId(4);people.setAge(10);people.setName(张三 );people.setClassNum(3);people.setHealth(健康000);people.setHeight(180cm);people.setWeight(60kg);people.setSex(男);CommonBeanUtils.copyProps(people,student,stuIdid,namestuName);System.out.println(people);System.out.println(student);} }运行结果 运行结果 package cn.yto.hbd.utils;import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map;public class BeanConvertUtils {/*** 普通转换* param source 源* param targetClass* param T* return*/public static T T copy(Object source,ClassT targetClass){return copy(source ,targetClass,null);}/*** 普通转换* param sourceObj 源* param targetObj 目标* param T* return*/public static T T copy(Object sourceObj,T targetObj){return copy(sourceObj ,targetObj,null);}/*** 普通转换* param source 源* param targetClass* param keyMap 源字段目标字段* return*/public static T T copy(Object source,ClassT targetClass,String ...keyMap){T targetObj null;try {targetObj targetClass.getConstructor(null).newInstance(null);} catch (Exception e) {throw new RuntimeException(e);}return copy(source,targetObj,keyMap);}/*** 普通转换* param sourceObj 源* param targetObj 目标* param keyMap 源字段目标字段* return*/public static T T copy(Object sourceObj,T targetObj,String ...keyMap){Class? sourceClass sourceObj.getClass();Class? targetClass targetObj.getClass();MapString, String keyMaps new HashMap();if (keyMap!nullkeyMap.length0){for (String keyItem : keyMap) {String[] split keyItem.split();keyMaps.put(split[0],split[1]);}}Field[] sourceFieldFields sourceObj.getClass().getDeclaredFields();for (Field sourceField : sourceFieldFields) {String sourceFieldName sourceField.getName();String targetFieldNamekeyMaps.getOrDefault(sourceFieldName,sourceFieldName);String sourceMethodName getGetMethodName(sourceFieldName);String targetMethodName getSetMethodName(targetFieldName);try {Method sourceGetMethod sourceClass.getDeclaredMethod(sourceMethodName);Method targetSetField targetClass.getDeclaredMethod(targetMethodName,sourceField.getType());Object value sourceGetMethod.invoke(sourceObj);targetSetField.invoke(targetObj,value);} catch (Exception e) {//找不到字段}}return targetObj;}/*** 首字母大写* param str* return*/private static String strFirstUpper(String str){return str.substring(0,1).toUpperCase()str.substring(1);}/*** 获取get方法名* param fieldName* return*/private static String getGetMethodName(String fieldName){return getstrFirstUpper(fieldName);}private static String getSetMethodName(String fieldName){return setstrFirstUpper(fieldName);} }
http://www.zqtcl.cn/news/245322/

相关文章:

  • 徐州市建设局招投标网站谷歌网站的主要内容
  • 门户网站建设工作情况汇报花店网站建设课程设计论文
  • 长春绿园网站建设哪里制作企业网站
  • 建设网站计划ppt模板核酸二维码
  • 宁波网络推广制作seo关键词推广公司
  • 东莞市网站推广西安推广公司无网不胜
  • 全国网站建设有实力建筑人才网123
  • 海安网站设计公司网站开发好学嘛
  • 网站建设深圳公司上海贸易公司注册条件
  • 深圳市坪山新区建设局网站给别人做网站去掉版权
  • 怎么做监测网站的浏览量有没有专业做股指的评论网站
  • 济南微信网站开发网上效果代码网站可以下载吗
  • 门户网站的设计常见的管理信息系统有哪些
  • 网站添加悬浮二维码成都游戏网站开发
  • 用jquery做网站百度seo排名规则
  • 免备案手机网站室内设计说明
  • 网站被做站公司贩卖怎样将qq空间建设为个人网站
  • 网站开发有哪几类淮安app开发公司
  • 营销网站建设公司哪家好兵团第二师建设环保局网站
  • 做推广最好的网站是哪个深圳办公室装修招标
  • 郑州高端网站制作wordpress那个版本好
  • wordpress屏蔽右键f12奉化首页的关键词优化
  • cn域名做犯法网站做电影网站需要哪些证
  • 官方网站有哪些韶关做网站的公司
  • 商城网站设计公司怎么样网站制作预算
  • 在济南什么人想做网站网站建设后怎么做主页
  • 联合年检怎么做网站上国家备案查询
  • 社交网站wap模板wordpress网址导航插件
  • 沈阳快速建站公司有哪些国外做二手服装网站
  • 手机如何建立网站平台seo比较好的优化