Ibatis typehandler has a typehandler (typehandlercallback should be accurate) in ibatis. This interface is usually used less. Google, most of them are used to convert database values to Java enumerated values or between clob and strings. recently I also used this stuff. however, we use the string stored in the database to be converted to the list type using a certain separator. I didn't know the typehandler stuff at first, so I defined two attributes in JavaBean: A string type and a list type,
There is a drawback of internal conversion. In fact, there are two interfaces when performing operations on an attribute. On the one hand, they are confusing to the user and the other is inconvenient to maintain, therefore, the conversion process is completely encapsulated, and an access interface is provided externally, which becomes a necessary choice.
Let's take a look at the definition of typehandlercallback: Java code
- Public interface typehandlercallback {
- Public void setparameter (parametersetter setter, object parameter)
- Throws sqlexception;
- Public object getresult (resultgetter getter)
- Throws sqlexception;
- Public object valueof (string S );
Public interface typehandlercallback {public void setparameter (parametersetter setter, object parameter) throws sqlexception; public object getresult (resultgetter getter) throws sqlexception; public object valueof (string S );
The code is easy to understand, and in order to illustrate how to use it, the author does not hesitate to give an example in watching.
The code is easy to understand. The setparameter () method mainly assigns values to the preparestatement. Therefore, when performing insert, update, and delete operations, it specifies the parameter used for passing.
The getresult () method is used to convert the content in the resultset result set to the corresponding attribute in the JavaBean.
Valueof () is mainly used to specify the default value when no value is specified. it is mainly used for conversion between resultset and JavaBean. it is best not to return a null value, which is related to nullvalue.
The following is an implementation of conversion between strings separated by ";" in the database and List objects in do: Java code
- Public class propertiestypehandlercallback implements typehandlercallback {
- Private Static final string list_split_flag = ";";
- Public object getresult (resultgetter getter) throws sqlexception {
- String properties = getter. getstring ();
- Return collectionutils. stringtolist (properties, list_split_flag, new
- Stringconvertor <property> (){
- Public property convert (string Str ){
- Return property. valueof (STR );
- }
- });
- }
- @ Suppresswarnings ("unchecked ")
- Public void setparameter (parametersetter setter, object parameter) throws sqlexception {
- List <string> propertylist = (list <string>) parameter;
- Setter. setstring (collectionutils. listtostring (propertylist, list_split_flag ));
- }
- Public object valueof (string s ){
- Return collections. empty_list;
- }
- }
Public class propertiestypehandlercallback implements typehandlercallback {Private Static final string list_split_flag = ";"; public object getresult (resultgetter getter) throws sqlexception {string properties = getter. getstring (); Return collectionutils. stringtolist (properties, list_split_flag, new stringconvertor <property> () {public property convert (string Str) {return property. valueof (STR) ;}}) ;}@ suppresswarnings ("unchecked") Public void setparameter (parametersetter setter, object parameter) throws sqlexception {list <string> propertylist = (list <string>) parameter; setter. setstring (collectionutils. listtostring (propertylist, list_split_flag);} public object valueof (string s) {return collections. empty_list ;}}
Next, configure the sqling file of sqlmap.
For SQL return result conversion, You need to specify the corresponding resultmap, such as writing: XML Code
- <ResultProperty = "propertylist" column = "properties" typehandler = "com. mysoft. Dao. ibatis. Support. propertiestypehandlercallback"/>
<Result property = "propertylist" column = "properties" typehandler = "com. mysoft. Dao. ibatis. Support. propertiestypehandlercallback"/>
There are two ways to set the parameters for value assignment. One is to set the typehandler attribute to the corresponding callback in the parameter tag of parametermap, different value assignment maps must be defined separately, which increases the workload. after parametermap is used, the original value name specified in # In the SQL statement needs to be changed ?, The value assignment follows the defined order in parametermap. This method is cumbersome and has poor maintainability. Therefore, we recommend using inlineparameter, for example:
SQL code
- # Propertylist, Handler = com. Taobao. item. Dao. ibatis. Support. itemverticalpropertiestypehandlercallback #
# Propertylist, Handler = com. Taobao. item. Dao. ibatis. Support. itemverticalpropertiestypehandlercallback #
Another way to configure typehandler is to define a type in sqlmap using the typehandler tag. For example, write the following code in Java:
- <Typehandler javatype = "" Callback = ""/>
<Typehandler javatype = "" Callback = ""/>
Note that this should be defined under the sqlmapconfig tag.