Document directory
- Symptom
- The getter and setter Methods automatically generated for this attribute in eclipse
- Beanutils's setproperty and getproperty methods are used to find the rules of the getter and setter methods.
- Ibatis processing rules when querying databases
- Summary
Symptom
The database has a field c_member_id, and the corresponding do class attribute is cmemberid. During database query, the value can be correctly injected into this attribute, however, when the setproperty method in beanutils is called, the set method cannot be found, and an error is returned.
The getter and setter Methods automatically generated for this attribute in eclipse
public String getcMemberId() { return cMemberId;}public void setcMemberId(String cMemberId) { this.cMemberId = cMemberId;}Beanutils's setproperty and getproperty methods are used to find the rules of the getter and setter methods.See methods in propertydescriptor.
Getreadmethod: If (type = Boolean. class | type = NULL) {readmethodname = "is" + getbasename ();} processing in else {readmethodname = "get" + getbasename ();} getbasename: public static string capitalize (string name) {If (name = NULL | Name. length () = 0) {return name;} return name. substring (0, 1 ). touppercase (English) + name. substring (1 );}That is, the name of the get method assembled by the attribute cmemberid is getcmemberid, Which is tragic and cannot be found. In this way, related methods executed through beanutils through reflection will encounter errors.
Ibatis processing rules when querying databasesIn the SQL-map configuration file:
The public void setproperties (Object object, object [] values) method in propertyaccessplan is called when the value is set during the query.
Object [] Arg = new object [1]; for (I = 0; I <propertynames. length; I ++) {Arg [0] = values [I]; try {setters [I]. invoke (object, ARG);} catch (throwable t) {Throw classinfo. unwrapthrowable (t) ;}} source of setters: Private void addmethods (class CLs) {method [] Methods = getallmethodsforclass (CLS); note: obtain all methods for (INT I = 0; I <methods. length; I ++) {string name = methods [I]. getname (); Note: Get method name if (name. star Tswith ("set") & name. length ()> 3) {Note: If it starts with set and the length of the method name is greater than 3 if (methods [I]. getparametertypes (). length = 1) {name = dropcase (name); Note: Get the property name if (setmethods. containskey (name )){...} setmethods. put (name, methods [I]); Note: Put the ing between the attribute and Its setter method into map settypes. put (name, methods [I]. getparametertypes () [0]);} Note: Obtain the property name Private Static string dropcase (string name) {If (name. startswith ("is") {name = Name. Substring (2);} else if (name. startswith ("get") | Name. startswith ("set") {name = Name. substring (3);} else {Throw new probeexception ("error parsing property name'" + name + "'. didn 'tstart with 'is ', 'get' or 'set '. ");} Note: if the method name is removed from the prefix and the length is 1 or greater than 1, and the second letter is not in uppercase, the first letter is changed to lowercase if (name. length () = 1 | (name. length ()> 1 &&! Character. isuppercase (name. charat (1) {name = Name. substring (0, 1 ). tolowercase (locale. us) + name. substring (1) ;}return name ;}That is, the getcmemberid method can correctly correspond to the cmemberid attribute.
SummaryFor the getter attribute named cmemberid, the processing in eclipse and beanutils In the setter method is inconsistent (not only the two parts are inconsistent), while ibatis is compatible with the two processing methods, the processing method can be to replace the bean name with a normal vertex attribute name, such as complaintmemberid, instead of using the cmemberid attribute name, in this case, make sure that the corresponding settings are in the ibatis SQL-map configuration file.