Rebuild ibatis to support data columns stored in the file system. How can I add attributes to resultmap?

Source: Internet
Author: User

In this paper, the http://www.cnblogs.com/suyuan/archive/2009/04/17/1438064.html

This articleArticleI want to introduce adding nodes to the XML segment of rusultmap. The specific effect is as follows:

Maybe you will ask why?

First, let me talk about the key. The key is used to specify the object attributes that are used to generate the hash path.The value of the specified attribute must be unique.The ID in the database as the primary key must be a unique value, so we generally use ID to generate the required hash path.

Next, let's describe the datasource. Here, the datasource can have two options: file or db. If file is selected, the corresponding column is stored in the file system according to the hash path, if dB is selected, it is stored in the database. By default, DB can be omitted.

After understanding the functions of the nodes added above, let's modify them step by step.ProgramTo achieve our goal:

Step 1: Modify the DTD verification file:

Here you must modify the information that I have already introduced in the previous article. I will not explain it too much here. Please refer.

The SQL map is parsed through COM. ibatis. sqlmap. engine. builder. XML. sqlmapparser. java, it should be noted that because we have not added a new node, but only added attributes, we just need to add the attribute Resolution on the original parsing method.

Example source code [Http://www.cnblogs.com/suyuan/]
Parser. addnodelet (" /Sqlmap/resultmap ", New Nodelet (){ Public   Void Process (node) Throws Exception {properties attributes = nodeletutils. parseattributes (node, state. getglobalprops (); string id = state. applynamespace (attributes. getproperty (" ID "); String resultclassname = attributes. getproperty (" Class "); String extended = state. applynamespace (attributes. getproperty ("Extends "); String xmlname = attributes. getproperty (" Xmlname "); String groupby = attributes. getproperty (" Groupby "); // Parse the key String key = attributes. getproperty ("Key"); Resultclassname = state. getconfig (). gettypehandlerfactory (). resolvealias (resultclassname); Class resultclass; Try {State. getconfig (). geterrorcontext (). setmoreinfo (" Check the result class. "); Resultclass = resources. classforname (resultclassname );} Catch (Exception e ){ Throw   New Runtimeexception (" Error sorting result. cocould not set resultclass. cause: "+ E, E);} resultmapconfig resultconf = state. getconfig (). newresultmapconfig (ID, resultclass, groupby, extended, xmlname, key); State. setresultconfig (resultconf );}});
 
Note:CodeNewresultmapconfig is called at the end, and the key is also added, like a string. We only need to step by step according to the program and gradually increase the key.
 
 
 
When we follow the following path, the key must have a destination, which is the resultmap object.
 
See:
 
 
 
 
 
Then we will jump to the resultmap class and continue reading
Here, I added three attributes to the resultmap class, so the key does not need to be explained,
 
Filehandlerpath is actually the hash path that I have been talking about. It shows how to generate a hash path.
Example source code [Http://www.cnblogs.com/suyuan/]
  // Generate the hash path    Public   Void Setfilehandlerpath (Object key ){ If (Key! = Null ) {Filehandlerpath = Delegate . Getbasedirectory () + This . Resultclass. getsimplename (); string hashkey = string. valueof (key. hashcode (); string split =" / "; Stringbuilder sb = New Stringbuilder (); For ( Int I = 0; I  If (I + 2) <= hashkey. Length () {sb. append (split). append (hashkey. substring (I, I + 2 ));} Else {Sb. append (split). append (" 0 "+ Hashkey. charat (I);} sb. append (split). append (hashkey). append (" . Data "); Filehandlerpath + = string. Format (" % S ", New Object [] {sb. tostring ()});}}
Another attribute is hasfilestore, which indicates whether the map has the result stored in the file system.
 

The above is the process of adding the key attribute.

 
Next we will continue to explain how to add the datasource attribute in rusult.
 
The resolution method is similar to the key.
 
 
 
The result object in ibatis is resultmapping, so the final destination of datasource is the resultmapping object.
 
Let's take a look at the resultmapping object. Here I have added three attributes. For more information about the functions of these three attributes, see annotations.
 
 
 
For the use of these attributes, see the following code.
Example source code [Http://www.cnblogs.com/suyuan/]
 Public   Void Addresultmapping (string propertyname, string columnname, integer columnindex, class javaclass, string jdbctype, string nullvalue, string notnullcolumn, string statementname, string resultmapname, object impl, string datasource) {errorcontext. setobjectid (propertyname +" Mapping of "+ Resultmap. GETID () +" Result Map "); Typehandler handler; If (Impl! = Null ){ If (Impl Instanceof Typehandlercallback) {handler = New Customtypehandler (typehandlercallback) impl );}Else   If (Impl Instanceof Typehandler) {handler = (typehandler) impl ;} Else { Throw   New Runtimeexception (" The class' "+ Impl +" 'Is not a valid Implementation of typehandler 
 
Or typehandlercallback");}}Else{Handler = config. resolvetypehandler (client. getdelegate (). gettypehandlerfactory (),
Resultmap. getresultclass (), propertyname, javaclass, jdbctype, True );} Resultmapping mapping = New Resultmapping (); mapping. setpropertyname (propertyname); mapping. setcolumnname (columnname); mapping. setjdbctypename (jdbctype); mapping. settypehandler (handler); mapping. setnullvalue (nullvalue); mapping. setnotnullcolumn (notnullcolumn); mapping. setstatementname (statementname); mapping. setnestedresultmapname (resultmapname); mapping. setdatasource (datasource ); If (Resultmap. getkey ()! = Null & Resultmap. getkey (). Equals (propertyname) {mapping. setfilehandler ( True );}If (Resultmapname! = Null & Resultmapname. Length ()> 0) {resultmap. addnestedresultmappings (Mapping);} mapping. setjavatype (javaclass ); If (Columnindex! = Null ) {Mapping. setcolumnindex (columnindex. intvalue ());} Else {Resultmappingindex ++; mapping. setcolumnindex (resultmappingindex);} resultmappinglist. Add (Mapping); // if it is saved in a file, set hasfilestore in the resultmap object to true.
If(Mapping. isfromfile ()){This. Sfresultmappingnames. Add (mapping. getpropertyname ());If(! Resultmap. ishasfilestore () {resultmap. sethasfilestore (True) ;}} Resultmap. setsfresultmappingnames (sfresultmappingnames); resultmap. setresultmappinglist (resultmappinglist );}
 
 
 
OK. This chapter will introduce you here. I don't know if you understand it. It doesn't matter. If you are interested, download a piece of code. Let's learn and discuss it together.
 
Next, we will learn about the analysis and functions of this node.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.