In the MyBatis series (vii)---Mapper mapping file configuration insert, UPDATE, delete to introduce the use of INSERT, UPDATE, delete, this article will introduce the use of SELECT, Resultmap. Select is undoubtedly our most commonly used and most complex, and MyBatis can help us perform advanced mapping well through RESULTMAP. Here's a look at the use of select and Resultmap:
Let's look at the configuration of select:
<select <!--1. The ID (must be configured) ID is a unique identifier in the namespace that can be used to represent the statement. A namespace (namespace) corresponds to a DAO interface, which should also correspond to a method within DAO (equivalent to the implementation of a method), so the ID should match the method name--id= "Selectperson" <!--2. ParameterType (optional configuration, default MyBatis automatic selection Processing) The fully qualified class name or alias of the parameter that will pass in the statement, and if it is not configured, MyBatis will select the appropriate Typehandl by default based on the parameter type Parameterhandler ER processing parametertype primarily specifies parameter types, which can be either int, short, long, string, or complex type (such as Object)---parametertype= "int" <!--3. Resulttype (Resulttype and Resultmap two choose a configuration) Resulttype to specify the return type, the specified type can be a base type, either a Java container, or a javabean-to-result Type= "HashMap" <!--4. Resultmap (Resulttype and Resultmap two choose a configuration) Resultmap is used to reference the type of mapping we define through the resultmap tag, which is the key to the advanced complex mapping of mybatis components---res ultmap= "Personresultmap" <!--5. Flushcache (optional configuration) sets it to true, which causes both the local cache and the level two cache to be emptied whenever the statement is called, with the default value: false---flushcache= "false" <! --6. UseCache (optional configuration) to set it to true will cause the result of this statement to be cached by level two, the default value: True for the Select element--Usecache= "true" <!--7. Timeout (optional configuration) This setting is the number of seconds that the driver waits for the database to return request results before throwing an exception. The default value is unset (dependent driver)-timeout= "10000" <!--8. Fetchsize (optional configuration) This is the number of result rows that are attempted to affect the driver each time the batch is returned and the value of this setting is equal. The default value is unset (dependent driver)-fetchsize= "9" <!--. StatementType (optional configuration) One of statement,prepared or callable. This will allow MyBatis to use statement,preparedstatement or CallableStatement respectively, with the default:prepared--> statementtype= "PREPARED" <!--10. ResultsetType (optional configuration) One of forward_only,scroll_sensitive or scroll_insensitive, the default is unset (dependent driver)--Resultse Ttype= "Forward_only" >
The configuration always seems so many, but the actual commonly used configuration is just a few, according to their own needs, it has been noted whether it must be configured.
Below or the last demo in time to practice practiced hand it:
------------------------------------------------------------------------The following is for select The practiced hand demo---------------------------------------------------------------------------------------
Database: Two new sheets (T_course, t_student)
T_course:
T_student:
Of these, 1 student can select multiple course for learning.
We still take the demo of the article, continue to write:
When added, the project directory looks like this:
Course.java:
View Code
Student.java:
View Code
Coursedao.java:
View Code
Studentdao.java:
View Code
Coursedao.xml:
<mapper namespace= "Com.dy.dao.CourseDao" > <!-- 1. Set the Resulttype to course directly here, and you'll know that I've set an alias. If no alias is set, then Resulttype = Com.dy.entity.Course. 2. May be careful you will find: Course.java in the property name and database field names are inconsistent, below, I used as in the SQL statement to match, of course, more than one way, after learning Resultmap, You can see a more intuitive way to keep the attributes in JavaBean consistent with the database field names. 3.findCourseById corresponds to the Findcoursebyid method in Coursedao. The parameter names and types that are passed in should also maintain the corresponding relationship. 4. You can see that in an SQL statement, the arguments can be obtained through the #{} expression. 5. What is the actual form of this SQL statement? Remember previously said, mybatis default is PreparedStatement bar, then, in our JDBC code, it is actually: select course_id as ID, course_name as name, Course_ DELETE_FLG as Deleteflag from T_course where course_id=? --<select id= "Findcoursebyid" resulttype= "Course" > select course_id as ID, course_name as Name, COURSE_DELETE_FLG as Deleteflag from T_course where Course_id=#{courseid} </select> </mapper >
Coursedaotest.java:
View Code
The above example, for course, we simply demonstrate the use of select, but there is a question to consider: A student can correspond to multiple course, then, in the MyBatis how to deal with this one-to-many, or even many-to-many, one-on relationship?
Here, you have to mention Resultmap this thing, MyBatis Resultmap function is very powerful, can handle complex relationship mapping, then resultmap How to configure it? Don't worry, it's coming:
Configuration of the Resultmap:
<!--1.type corresponding type, can be JavaBean, or other 2.id must be unique, used to mark the uniqueness of this resultmap, when using Resultmap, is specified by the ID-- <resultmap type= "" id= "> <!--ID, uniqueness, note that this ID is used to indicate the uniqueness of this JavaBean object, not necessarily the primary key of the database (do not interpret it as the primary key of the database table) The property attribute corresponds to the attribute name of the JavaBean, which corresponds to the column name of the database table (so that when the attribute of JavaBean is inconsistent with the column name of the database table, it can be mapped by specifying this to remain normal)- <id property= "" column= ""/> <!--result and ID, corresponding to normal properties--<result Pro Perty= "" column= ""/> <!--constructor for the construction method in JavaBean and <construct Or> <!--idarg the ID parameter in the constructor method--<idarg column= ""/> <!--arg the common parameter in the constructor method ---<arg column= ""/> </constructor> <!--collection, corresponding JAV Abean in the container type, is the implementation of a one-to-many key property for the JavaBean container corresponding field Name column in the database is reflected in the name OfType is the specified JavaBean Type specified by the device-- <collection property= "" column= "" oftype= "" ></collection> <!--Association for Correlation is the key to implement N-to-one. Property for JavaBean the container corresponding field Name column is the type that is represented in the database column name javatype The specified association--<association property= "column=" "javatype=" "></association> </resultMap>
OK, we know how to configure Resutmap, we immediately followed the above demo to practice it:
------------------------------------------------------------------The following is an example of a mapping that handles a one-to-many relationship with Resultmap----------------------------- --------------------------------
A student corresponds to multiple course, a typical one-to-many, let's take a look at MyBatis How to configure this mapping:
Studentdao.xml:
View Code
Studentdaotest.java:
View Code
I believe that through the above demo, you can also use MyBatis's Select and Resultmap usage. The above demo only demonstrates a one-to-many mapping, in fact, many pairs of a, many and many are similar to it, so I did not demonstrate, interested can do their own hands-on.
Well, this time it's written here. (PS, Sick for a week, so until now to update the blog).
Also attached to the demo, the need for children's shoes can be downloaded:
Demo:http://pan.baidu.com/s/1qwjsdza
MyBatis Series (eight)---Mapper mapping file configuration Select, resultmap[turn]