In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to use JavaBean to set the default value of attributes according to the specified conditions", so the editor summarizes the following contents, detailed contents, clear steps, and a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use JavaBean to set the default value of attributes according to specified conditions" article.
JavaBean sets the default value of the property according to the specified condition to use the scene
When other data has been assembled in the bean data, set the data default value in the logic and the last step of the data conversion; if there are few attributes in bean, you can write the if condition manually. If there are too many attributes in bean and you do not want to write too much if, this method is a very good method.
Scope of use
Only for the current class, you cannot set a value for the parent class
Also refer to the methods of other bloggers, do the optimization and modification, due to the passage of time, can not find the references of the original blogger, please forgive me ~
/ * * @ Description: according to the condition Set the default property for bean to specify the attribute * @ param sourceObject entity class that currently needs to operate * @ param targetString the value of the attribute to be replaced * @ param defaultValue value to be replaced * @ return: void * @ Author: * @ Date: 2021 Date 4Object sourceObject 26 public static void setDefaultValue (Object sourceObject, String targetString) String defaultValue) {if (sourceObject! = null) {try {Class clazz = sourceObject.getClass () Field [] fields = clazz.getDeclaredFields (); Object objectCopy = clazz.getConstructor (new Class [] {}). NewInstance (); org.springframework.beans.BeanUtils.copyProperties (sourceObject, objectCopy); for (Field field: fields) {/ / modifier int mod = field.getModifiers () If (Modifier.isStatic (mod)) {continue;} / / attribute name String fieldName = field.getName (); if (fieldName.equals ("serialVersionUID")) {continue } / / get the first letter of the attribute and convert it to uppercase, corresponding to setXXX String firstLetter = fieldName.substring (0,1). ToUpperCase (); String setMethodName = "set" + firstLetter + fieldName.substring (1) / / set the accessible ID of the property boolean accessible = field.isAccessible (); / / log.info ("accessible ID:" + accessible); field.setAccessible (true); Method setMethod = clazz.getMethod (setMethodName, field.getType ()); Object fieldValueO = field.get (sourceObject) If {if (fieldValueO instanceof String) {String fieldValue = (String) field.get (sourceObject); if (targetString.equals (fieldValue)) {setMethod.invoke (objectCopy, defaultValue) / / call object's setXXX method}} else {Object o = field.get (sourceObject); if (o==null) {setMethod.invoke (objectCopy, defaultValue) / / call object's setXXX method}} field.setAccessible (accessible);} org.springframework.beans.BeanUtils.copyProperties (objectCopy, sourceObject);} catch (Exception e) {throw new RuntimeException (e) } use sample setDefaultValue (sourceObject, null, ""); / / set the default value to empty string setDefaultValue (sourceObject, "null", "") when the attribute value is null; / / set to empty string JavaBean object member variable default value and array default value public class StudentTest {private int id; private String name; int [] arr when the attribute value is "null" string Public int [] getArr () {return arr;} public void setArr (int [] arr) {this.arr = arr;} public int getId () {return id;} public void setId (int id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name }} public class guanxiaokun {public static void main (String [] args) {/ / for member variables of javaBean object, the default value is 0 if the member variable is a basic type, and null / / array object if it is a reference type. If there is no static initialization, the default value of the array object is 0. If the basic type is stored, the default is 0. If the reference type is stored, the default value is null System.out.println (new StudentTest (). GetId ()) / / 0 System.out.println (new StudentTest (). GetName ()); / / null System.out.println (new StudentTest (). GetArr ()); / / null int [] arr = new int [3]; String [] str = new String [3]; System.out.println (arr [0]); / / 0 System.out.println (str [0]) / / null}} above is the content of this article on "how to use JavaBean to set property defaults according to specified conditions". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.