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 realize the conversion and assignment of entity classes in java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail how to realize the conversion and assignment of entity classes in java. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Package com.yzw.manager.util;import java.lang.reflect.Field;import java.lang.reflect.Modifier / * yangzhiwei * two entity classes convert each other to assign values () * the type and name of the field name must be exactly the same, and the inherited parent field cannot be assigned * instead of set and get repeat code * / public class ReflectToBean {public static Object transformToBean (Object source, Object target) {/ / get the class object Object object null; Class sourceCla = source.getClass () Field [] sourceFields = sourceCla.getDeclaredFields (); Class targetCla = target.getClass (); Field [] targetFields = targetCla.getDeclaredFields (); try {o = targetCla.newInstance (); for (int I = 0; I < targetFields.length; iTunes +) {Field ft = targetFields [I] / / set some properties to be accessible boolean isStaticFt = Modifier.isStatic (ft.getModifiers ()); if (isStaticFt) {continue;} / / set some properties to be accessible ft.setAccessible (true); for (int j = 0) J < sourceFields.length; jacks +) {Field fs = sourceFields [j]; / / set some properties to be accessible boolean isStaticFs = Modifier.isStatic (fs.getModifiers ()); if (isStaticFs) {continue } / / set some properties to be accessible fs.setAccessible (true) If (fs.getName (). Equals (ft.getName ()) & & fs.getType (). ToString (). Equals (ft.getType (). ToString ()) {/ / get the value of this attribute Object val = fs.get (source); ft.set (o, val) Break;} catch (Exception e) {e.printStackTrace ();} return o }} on how to achieve entity class conversion assignment in java to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.

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