@Setter
@Getter
public class SourceA { private String name;
private String text; public SourceA(String name, String text) {
this.name = name;
this.text = text;
}
}
@Setter
@Getter
@ToString
public class TargetB {
@FieldMap(name = "name")
public LocalDateTime n;
private String text;
}

注解字段名

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FieldMap {
String name() default "";
}

实现,如果字段类型相同可以赋值,但是可能会有不同的时候。

public static void copy(Object source, Object target) throws Exception {
Class<?> sourceClass = source.getClass();
Class<?> bClass = target.getClass(); Field[] soutceFields = sourceClass.getDeclaredFields();
Map<String, Object> sourceMap = new HashMap<>();
for (Field field : soutceFields) {
field.setAccessible(true);
sourceMap.put(field.getName(), field.get(source));
} Field[] targetFields = bClass.getDeclaredFields();
for (Field field : targetFields) {
field.setAccessible(true);
FieldMap annotation = field.getAnnotation(FieldMap.class);
if (annotation != null) {
String name = annotation.name();
Object sourceValue = sourceMap.get(name);
if (field.getType() == sourceClass.getDeclaredField(name).getType()) {
field.set(target, sourceValue);
} else {
/**
* 比如source的date字段类型为String,target接收date字段的类型为LocalDateTime
*/
}
continue;
}
field.set(target, sourceMap.get(field.getName()));
} }

Bean属性复制,字段名可不同,字段类型不同需要自行处理的更多相关文章

  1. ORACLE数据库导出表,字段名,长度,类型,字段注释,表注释语句

    转自:https://www.cnblogs.com/superming/p/11040455.html --数据库导出表,字段名,长度,类型,字段注释,表注释语句 SELECT T1.TABLE_N ...

  2. bean属性复制到另外一个bean

    import org.springframework.beans.BeanUtils; BeanUtils.copyProperties(maker.getBaseInfo(), newBasInfo ...

  3. 在oracle中查询已知表名的表中所有字段名,每个字段是否是主键,是否是外键,是否为空的sql语句

    查询表的所有列及其属性:select t.*,c.COMMENTS from user_tab_columns t,user_col_comments c where t.table_name = c ...

  4. mysql 中查看指定表的字段名 (可根据字段变量生成c#后台代码)

    select DISTINCT data_type  from COLUMNS where table_name='表名' 用ConCat();构造生成代码.....

  5. 用PowerDesigner建立概念模型的问题:不能创建相同字段名的关键字段

    依次点击Tools--->Model Options->Model Settings,在Model Settings中有Data Item组框,取消里面的Unique Code,勾选All ...

  6. mysql命令行执行时不输出列名(字段名),直接显示字段对应的数值

    执行命令时加个-N参数就可以了 -N, –skip-column-names 比如说:mysql -uroot -p1234546 -N -e “select * from user;”

  7. Sql Server中COUNT(字段名)跟COUNT(*)的特殊不同点

    今天有个需求,有2张表: 1.一个“搜索记录”表search,一个“搜索后下载记录”表down 2.映射关系:每一个下载记录对应一条搜索记录,                           第个 ...

  8. sqlserver查询当前库下,一张表的表名,字段名,字段类型,字段长度

    sqlserver版: 查询当前数据库下所有表名: select * from sys.tables; 查询当前库下,一张表的表名,字段名,字段类型,字段长度: select a.name 表名,b. ...

  9. Mysql中用SQL增加、删除、修改(包括字段长度/注释/字段名)总结

    转: Mysql中用SQL增加.删除.修改(包括字段长度/注释/字段名)总结 2018年09月05日 10:14:37 桥Dopey 阅读数:1830   版权声明:本文为博主原创文章,未经博主允许不 ...

随机推荐

  1. Cannot find ./catalina.sh The file is absent or does not have execute permission This file is nee Linux上tomcat无法正常启动

    上传了个tomcat7的压缩包上linux服务器,解压后,想直接启动,发现报错: Cannot find ./catalina.sh The file is absent or does not ha ...

  2. Spring Boot ERROR StatusLogger No Log4j 2 configuration file found

    1. 问题描述 项目之前的 log4j2 配置没问题,把 pom 文件中的 spring-boot-starter-web 依赖删除后,然后启动项目就报错找不到 log4j2.yml 文件. 之前引用 ...

  3. session cookie傻傻分不清

    做了这么多年测试,还是分不清什么是cookie,什么是session?很正常,很多初级开发工程师可能到现在都搞不清什么是session,cookie相对来说会简单很多. 下面这篇文章希望能够帮助大家分 ...

  4. HearthBuddy炉石兄弟 如何调试ai

    Sepefeets's update to botmaker's Silverfish AI This AI is a Custom Class for Hearthranger and Hearth ...

  5. Android即时通讯开发之XMPP (一)初识XMPP协议和asmack

    在讲XMPP和asmck之前 ,我还是先分享一些资源文档,如果你有耐心,可以直接忽略我下面所写的.下面有关XMPP的介绍大部分是摘抄网上的文档,后面我会写一些基于XMPP协议和asmck开源库的聊天室 ...

  6. CentOS / RHEL 配置yum源

    CentOS / RHEL 配置yum源 */--> CentOS / RHEL 配置yum源 Table of Contents 1. 前言 2. 关于yum 2.1. yum是什么 2.2. ...

  7. Node Newbie Error – NPM Refusing to Install Package as a Dependency of Itself

    46 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_ ...

  8. 003-tomcat配置文件-server、tomcat-users

    1.server.xml讲解 位于conf下 <?xml version="1.0" encoding="UTF-8"?> <!-- Serv ...

  9. js手写数组Api--模拟实现常见数组Api

    数组的API经常用,但是api的内部实现还没研究过,于是就研究学习了下. 原文地址: https://www.cnblogs.com/yalong/p/11606865.html 数组的API的具体使 ...

  10. Docker容器时间与主机时间相差8小时

    查看主机时间 [root@localhost ~]# date 2016年 07月 27日 星期三 22:42:44 CST 查看容器时间 root@b43340ecf5ef:/# date Wed ...