1. Result MapS
The Resultmap element is one of the most important and powerful elements in MyBatis. (The resultmapelement is the most important and powerful element in MyBatis.) compared to JDBC from resultsets, it can be used to save about 90% of the code. But more said, the following began Resutmap Tour ~
One of the simplest examples
First, the XML defines a resultmap, as follows:
XML code
- <resultmap type="Com.ggg.henushang.entity.Person" id="Personmap">
- <ID column= "id" property="id" />
- <result column="name" property="name" />
- <result column=' age ' property=' age ' />
- </resultmap>
Then you can use it, as follows:
XML code
- <select id= "getpersonmap" resultmap="Personmap">
- SELECT * FROM person
- </Select>
Note that there is no resulttype this attribute, but instead use the Resultmap
Code in DAO:
Java code
- Map<string, object> getpersonmap ();
Test code:
Java code
- @Test
- Public void Testgetpersonmap () {
- Map<string, object> m = Session.selectmap ("Com.ggg.henushang.dao.PersonDao.getPersonMap", "name");
- System.out.println (m);
- Assert.assertnotnull (m);
- }
Printing results:
Java code
- {henushang=id:7 Name:henushang Age:A, Updatename=id:3 name:updatename Age: $
In fact, MyBatis official online surface There is a more complex situation, I do not understand, so it is not here fraught
2. auto-mapping (auto Match)
When the result is automatically matched, MyBatis gets the column name and looks for the same property (ignoring case). This means that when the named ID column and the property named ID are found, MyBatis will assign the value of the column ID to the property ID.
In general, database column names are capitalized and underlined when they are named, and Java properties are often based on the hump naming method. In order to ensure an automatic match between them to set the property Mapunderscoretocamelcase to True.
You can use auto-match even when there is a specific result map. At this time, for each result map, all the columns in the resultset that are not manually matched will be automatically matched before the manually set match will be executed (I note: In fact, the priority of manual configuration is better, the final result will be manually configured to prevail). In the example below, the ID and usename columns will be automatically matched, and the Hashed_password column will be matched.
XML code
- <select id="selectusers" resulttype="User">
- Select
- USER_ID as "id",
- User_name as "UserName",
- Hashed_password
- From some_table
- where id = #{id}
- </Select>
- <resultmap id="Userresultmap" type="User">
- <result property= "password" column="Hashed_password"/>
- </resultmap>
The mybatis has three levels of matching:
- NONE -automatic matching is disabled and only manually configured will be matched.
- PARTIAL -will automatically match all attributes that are nested inside (joins)
- full-automatically matches all
The default is partial. (The official website also explained the reason, no longer detailed introduction)
3. Cache (Cached)
In addition to the partial session cache, you can enhance the monetization and process the cyclic dependencies, which are not enabled by default Mybatis. To turn on level two caching, you need to add a line to your SQL mapping file:
XML code
- <cache/>
Literally, that's it. The effect of this simple statement is as follows:
- All SELECT statements in the map statement file will be cached.
- All insert,update and DELETE statements in the map statement file flush the cache.
- The cache is retracted using the Least recently used (LRU, least recently used) algorithm.
- Depending on the schedule (such as no flush Interval, there is no refresh interval), the cache is not refreshed in any chronological order.
- The cache stores 1024 references to a list collection or object, regardless of what the Query method returns.
- The cache is considered to be a read/write (readable/writable) cache, meaning that object retrieval is not shared and can be safely modified by the caller without interfering with potential modifications made by other callers or threads.
All of these properties can be modified by caching the attributes of the element. Like what:
XML code
- <cache
- eviction="FIFO"
- flushinterval="60000"
- " size= "
- readonly="true"/>
Omit hundreds of words in the middle ..... Refer to the official website's documentation.
Custom cache:
In addition to these custom caches, you can completely override the caching behavior by implementing your own cache or creating adapters for other third-party caching scenarios.
<cache type= "Com.domain.something.MyCustomCache"/>
Reference cache:
Recall from the previous section that the unique cache for this particular namespace will be used or flushed with statements within the same namespace. Perhaps sometime in the future, you will want to share the same cache configuration and instances in the namespace. In this case you can use the Cache-ref element to refer to another cache.
<cache-ref namespace= "Com.someone.application.data.SomeMapper"/>
Result Maps, auto-mapping, cache