博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java常用代码
阅读量:7138 次
发布时间:2019-06-28

本文共 3890 字,大约阅读时间需要 12 分钟。

 

1、记录某个实体类(表)的变化

 

②、
1 package gwm.xtkf.tool;  2   3 import java.lang.reflect.Field;  4 import java.lang.reflect.Method;  5 import java.text.SimpleDateFormat;  6 import java.util.Date;  7 import java.util.HashMap;  8 import java.util.Locale;  9 import java.util.Map; 10  11 import xs.jszx.systemmanage.domain.Purpose; 12  13 public class CompareClass { 14      15     public static String compareClass(Object from, Object target, Map
describe) throws Exception{ 16 17 if (from.getClass() == target.getClass()){ 18 Class
c = target.getClass(); 19 Field field[] = c.getDeclaredFields(); 20 21 StringBuilder str = new StringBuilder(); 22 for (Field f: field){ 23 String type = f.getGenericType().toString(); //获取属性的类型 24 25 if (describe.get(f.getName())!=null){ 26 Object fv = invokeMethod(from, f.getName(), type); 27 Object tv = invokeMethod(target, f.getName(), type); 28 29 if (fv!=null && tv!=null){ 30 if (!fv.equals(tv)){ 31 str.append(describe.get(f.getName()) + ":" + tv + "->" + fv + ";"); 32 } 33 } 34 else if(fv==null && tv==null){} 35 else{ 36 37 if (fv!=null && fv.toString().trim().length()>0){ 38 str.append(describe.get(f.getName()) + ":" + tv + "->" + fv + ";"); 39 } 40 if (tv!=null && tv.toString().trim().length()>0){ 41 str.append(describe.get(f.getName()) + ":" + tv + "->" + fv + ";"); 42 } 43 44 } 45 } 46 47 } 48 49 return str.toString(); 50 } 51 else{ 52 return null; 53 } 54 } 55 56 @SuppressWarnings("unchecked") 57 private static Object invokeMethod(Object owner, String methodName, String type) throws Exception { 58 @SuppressWarnings("rawtypes") 59 Class ownerClass = owner.getClass(); 60 methodName = methodName.substring(0, 1).toUpperCase() 61 + methodName.substring(1); 62 Method method = null; 63 64 try { 65 method = ownerClass.getMethod("get" + methodName); 66 } catch (SecurityException e) { 67 } catch (NoSuchMethodException e) { 68 return null; 69 } 70 //如果type是类类型,则前面包含"class ",后面跟类名 71 //对日期类型进行转换 72 if(type.equals("class java.util.Date")){ 73 Date date = (Date) method.invoke(owner); 74 if(date != null){ 75 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.US); 76 return sdf.format(date); 77 } 78 } 79 return method.invoke(owner); 80 } 81 82 public static void main(String[] args){ 83 84 Purpose purpose1 = new Purpose(); 85 Purpose purpose2 = new Purpose(); 86 87 88 purpose1.setName("住宅"); 89 purpose2.setName("别墅"); 90 91 Map
map = new HashMap
(); 92 map.put("name", "名称"); 93 94 try { 95 System.out.println(compareClass(purpose1, purpose2, map)); 96 } catch (Exception e) { 97 // TODO Auto-generated catch block 98 e.printStackTrace(); 99 }100 }101 }
CompareClass

 

 

 

转载于:https://www.cnblogs.com/douglas0126x/p/5006297.html

你可能感兴趣的文章
使用JPA中@Query 注解实现update 操作
查看>>
判断一个枚举值是否属于某个枚举类
查看>>
Spring MVC+Mybatis 多数据源配置
查看>>
hdu 4287 Intelligent IME
查看>>
NYOJ15-括号匹配(二)-区间DP
查看>>
JDBC基本应用
查看>>
header中Content-Disposition的作用
查看>>
SSAS知识回放之订单数据分析
查看>>
Apache+jboss群集部署
查看>>
JAVAWEB开发之HttpServletResponse和HttpServletRequest详解(下)(各种乱码、验证码、重定向和转发)...
查看>>
《从零開始学Swift》学习笔记(Day 46)——下标重写
查看>>
一个屌丝程序猿的人生(六十九)
查看>>
【block第四篇】实现
查看>>
mysql学习笔记之mysql数据库的安装
查看>>
hdu 5371 Hotaru's problem【manacher】
查看>>
MySQL 5.6的一个bug引发的故障
查看>>
(转) Eclipse通过HibernateTools实现逆向生成Hibernate实体类
查看>>
编写轻量ajax组件02-AjaxPro浅析
查看>>
搭建Git本地服务器
查看>>
windows下redis 和 hiredis的编译与使用
查看>>