Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use xkbeancomparator to compare javabean to generate operation records

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use xkbeancomparator to compare javabean to generate operation records", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use xkbeancomparator to compare javabean to generate operation records.

Xkbeancomparator is a tool for java bean to compare, modify and output differences. Github address

Applicable scenario: when you edit and submit, you need to record the modified content, compare the values before and after the modification, and generate an operation record. You can select the fields and field descriptions of the record, and customize the operation record.

Features:

Jdk 1.7 +

Do not rely on third-party jar, the size is very small

Using reflection, call the get method to compare field values.

Use example: xkbeancomparator-samples

(1) add pom dependency

Com.github.xkzhangsan xkbeancomparator 0.0.1

(2) java bean class User

Import java.math.BigDecimal;public class User {Integer id; String name; private BigDecimal point; public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} public BigDecimal getPoint () {return point } public void setPoint (BigDecimal point) {this.point = point;}}

复制代码

(3) add auxiliary log class UserLog

Import java.util.HashMap

Import java.util.Map

Import com.xkzhangsan.xkbeancomparator.BeanComparator

Import com.xkzhangsan.xkbeancomparator.CompareResult

Public class UserLog {

Private static final Map propertyTranslationMap = new HashMap (); static {propertyTranslationMap.put ("name", "user name"); propertyTranslationMap.put ("point", "integral");} public static CompareResult getCompareResult (Object source, Object target) {return BeanComparator.getCompareResult (source, target, propertyTranslationMap);}

}

(4) use

@ Testpublic void test1 () {User U1 = new User (); u1.setId (1); u1.setName ("aa"); u1.setPoint (new BigDecimal ("1111111111.12")); User U2 = new User (); u2.setId (1); u2.setName ("aa2"); u2.setPoint (new BigDecimal ("111111111111.15")) CompareResult compareResult = UserLog.getCompareResult (U1, U2); if (compareResult.isChanged ()) {System.out.println (compareResult.getChangeContent ());}}

(5) output result

User name: aa- > aa2, Credit: 1111111111.12-> 1111111111.15

(6) explain instructions

The above is the recommended usage. Using the auxiliary log class can maintain a java bean annotation map uniformly and simplify the call. The recommended usage, above, is to use secondary logging classes to uniformly maintain an annotated map of a Java bean, simplifying invocation.

At this point, I believe you have a deeper understanding of "how to use xkbeancomparator to compare javabean to generate operation records". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report