Since the May 1st, three
Ibatis.net
of
POST
Once, there has been no update related articles. On the one hand, because of its own ability, it is not confident, and the three convictions have not been confident in the home page. On the other hand, because it is really busy recently, you can make me busy to turn around, there is no time to take time.
Blog
. But think about it, just an excuse, see
DFLYING CHEN
In these two months
CNBLOGS
The achievements made let me be embarrassed, it is impossible to compile with others, but always take "busy" to block the Wrigley, it shouldn't.
After the wine, enter this theme, we will discuss a very important content in iBatis, in my personal opinion, can really use the key to ibatis, this is Resultmap. Field understanding, it is the mapping of the result set, which is to assign a map to the object's properties of the object. In fact, if there is no special needs, we can use ResultClass to replace it, because if the field is exactly the same as the property, querying the data set automatically matches the instance object of the class specified by the resultClass, if the field name is not in the property, then this field will The instance objects that will not be returned are accepted, which is equivalent to not querying this field.
Each ResultMap has an id, if you are not configured with namespace in SQLMap.config, then this ResulTemap ID is a full bureau (this is the same in all IBATIS configuration elements), an important property of Resultmap The class is Class, which will determine the class of the example of this Resultmap, in other words, it is to indicate the data type of the result set. In the Extends property, it can set the resultMap to inherit, if the value is given to him, then it will inherit the map configuration field from the Super Resultmap. Defined as follows:
resultMap>
resultmaps>
If you have correctly configured the ibatis XSD architecture file, then this time you will prompt the definition of ResultMap is incomplete. That's right, next is to define the Result element. Each Result element is a mapping that defines a field with a data class property. There is a relatively large attribute parameter in each Result element, where Property and Column are required, and other parameter properties are optional. So we must define more than more than one Result definition in every Resultmap. Often the following configurations can complete the basic configuration.
resultMap>
resultmaps>
But if you need more requirements, the Result Map can still maximize you. The ColumnIndex property provides the program we map the proprietary item of the data set to the specified data object properties, but this way should be as small as possible, you will find that this will result in the maintenance and readability of us. Very large side effects. The DBTYPE attribute clearly indicates the type of database corresponding to this field, and I rarely use most cases. The Type property clearly indicates the data type of this field to attribute the corresponding data object properties, usually if you want to guarantee type security, setting this property is necessary. The resultMapping property is slightly more complicated, which is used in a scenario, if a data class's attribute itself is not a primitive data type, but a complex data type, then we can't give it a simple The result element is about, but also gives him a complete resultmap. The resultMapping property is to do this. Its attribute value is an ID of an existing resultMap. NullValue properties have nothing to say, it is given when the value of this field is null, how much is its default value. SELECT attributes are more complicated as resultmapping, first talking about its attribute value must be a ID of the query statement that returns a data set, and the data class attribute that can be configured can be a primitive type, composite type, or one Includes a set type of multiple data, these types, no problem. Its important existence is to describe the relationship between different tables. Through this query, you can use the select property when you query the relevant fields from another table, you don't want to query the relevant fields from another table. As follows:
resultMap>
resultmaps>
Select * from children where parentid = # id #
select>
statements>
This allows you to indicate the relationship between different tables and data read problems without the way of programming. But there is something possible, if you have to read data each time, you will find that you will generate more information about the database interaction, and even if you don't need this data every time you meet this. Will not cause waste of data reading? The next Lazyload property provides us with a second problem solution, that is, the delay loading of the data, it's right, delayed loading can greatly improve the performance of data access, it just wants to read these Data, when the main slave table is related, this way may be the best solution.
OK, about the introduction of Resultmap, first, then I want to record, I have encountered some problems during use:
One. When using ResultMap, you have to pay special attention to the configuration fields you give in Resultmap, but when you return the data set, you have not returned this field, and that program will throw an exception. But in contrast, if you return some fields, you don't have a configuration definition in ResultMap, then those fields will not be processed without any prompts, quite do not query these fields. You have to pay special attention to this problem. two. If there is no special demand, I suggest that the properties of the data class be the same as the database field word, so if we don't have to write this resultMap, in fact, if there is no such special requirement, then write This Resultmap is still a very time consuming and is easily erroneous.
three. Pay special attention when using lazyload, not what type of data can be lazyload, only the type of interface interface, and the property of the data class is defined as the IList type field can be lazyload. (About whether only IList type attributes can be discussed by LazyLoad, because there is only this type of experience, even the generic version of ILIST is not supported). And when you use it, you can't convert this IList type attribute into your true data type. Because this property is packaged into a dynamic type when running, this dynamic type still implements the ILIST interface, which is because this dynamic type expands the function of we can lazed. At this time, it is useful in the program, so you can't make a strong type conversion.
The problem did not think of more. If there is a problem with Resultmap, I will update it, I hope everyone will refer to some of my mistakes and shortcomings together. Don't let my misunderstandings of my mistakes, thank you