Java Common Code

Source: Internet
Author: User

1. Record the change of an entity class (table)

① Two entity class comparisons, data changes before and after recording, accurate to the values of each column

②,
  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; Ten import Xs.jszx.systemmanage.domain.Purpose; public class Compareclass {All public static String Compareclass (object from, Object target, Map<str             ING, string> describe) throws exception{if (from.getclass () = = Target.getclass ()) {18 class<? Extends object> C = Target.getclass (); Field field[] = C.getdeclaredfields (); StringBuilder str = new StringBuilder ();    A (Field F:field) {The String type = F.getgenerictype (). toString (); Gets the type of the property, if (Describe.get (F.getname ())!=null) {+ Object FV = i Nvokemethod (from, F.getname (), type);         27            Object TV = InvokeMethod (target, F.getname (), type); if (fv!=null && tv!=null) {if (!fv.equal S (TV)) {Str.append (Describe.get (F.getname ()) + ":" + TV + + "+ FV +"; ");                     (Fv==null && tv==null) {} 35 else{(Fv!=null && fv.tostring (). Trim ( ). Length () >0) {str.append (Describe.get (F.getname ()) + ":" + TV + "+ FV +"; ");                             (Tv!=null && tv.tostring (). Trim (). Length () >0) {41 Str.append (Describe.get (F.getname ()) + ":" + TV + + "+ FV +"; ");           42} 43 44} 45} 46             () str.tostring return (); The else{("Unchecke") is @SuppressWarnings ("         D ") The private static object InvokeMethod (object owner, String methodName, String type) throws Exception {58   @SuppressWarnings ("Rawtypes") ownerclass Class = Owner.getclass ();   MethodName = methodname.substring (0, 1). toUpperCase () + methodname.substring (1);   method = null;   try {n = ownerclass.getmethod ("get" + methodName);   The SecurityException (e) {nosuchmethodexception} catch (e) {n-null; 69} 70//If type is a class type, it is preceded by "class", followed by class name 71//To convert the date type Type.equals ("Class JAV A.util.date ")) {Method.invoke Date date = (date) (owner);= null) {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd", locale.us);  Sdf.format return (date); Method.invoke} (owner); n} bayi, public static void main (string[] args) {Purpose purpose1 = new Purpose (); 8 5 Purpose purpose2 = new Purpose (); Purpose1.setname ("residential"); Purpose2.setname ("Villa"); map<string, string> Map = new hashmap<string, string> (); Map.put ("name", "title"); 94 try {System.out.println (Compareclass (purpose1, PURPOSE2, map)); (Exception e) {//TODO auto-generated catch block 98 e.printstacktrace (); 99}100}101}
Compareclass

Java Common Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.