A problem that you often encounter is that you need to set the property value for a Pojo class, but sometimes it's cumbersome to call over and over again, and you can take advantage of the Java reflection mechanism.
Pojo class:
public class Dotstat implements Serializable {
Private Integer ID;
Private String model;
Private String Adccompany;
Private String ossdk;
Private String av;
Private String phone;
Private Integer Enterpriseid;
Private String Citypinyin;
Private Integer Industryid;
Private Date statdate;
--------------------------------------------
Get and Set method
}
Using the method of reflection, we need to assign a value to the id attribute of the Dotstat class, which is the property name, the value to assign, and the Dotstat object
private void Setattributevalue2dotstat (String attributename, Integer times, Dotstat dot) {
//capitalize the first letter of the property name.
String setattributemethodname = "Set" + character.touppercase (Attributename.charat (0)) + attributename.substring (1);
try {
Method Setattributemethod = DotStat.class.getDeclaredMethod (Setattributemethodname,integer.class);
Setattributemethod.invoke (Dot,st.gettimes ());
catch (SecurityException e) {
catch (Nosuchmethodexception e) {
catch (IllegalArgumentException e) {
catch (Illegalaccessexception e) {
catch (InvocationTargetException e) {
}
}
This is to use the reflection mechanism to assign a value to a property