1. Overview
When select mapping in MyBatis, the return type can be used with resultType or resultMap. resultType directly represents the return type, while resultMap is a reference to the external ResultMap, but resultType and resultMap cannot exist at the same time.
When MyBatis is querying mapping, in fact, each attribute query is placed in a corresponding map, where the key is the attribute name and the value is its corresponding value.
① When the provided return type attribute is resultType
MyBatis will take out the key value pairs in the Map and assign them to the corresponding attributes of the object specified by resultType.
So in fact, the return type of each query map of MyBatis is ResultMap. However, when the provided return type attribute is resultType, MyBatis automatically assigns the corresponding value to the attributes of the object specified by resultType.
②When the provided return type is resultMap
Because Map cannot represent the domain model well, it needs to further convert it into the corresponding object itself, which is often very useful in complex queries.
2. ResultsType
public class Blog { private int id; private String title; private String content; private String owner; private List<Comment> comments; }
The corresponding database table stores id, title, content, and owner attributes.
(Using resultType, it is unexpected that the attribute name must be the same as the column name)
<typeAlias alias="Blog" type=""/> <select parameterType="int" resultType="Blog"> select * from t_blog where id = #{id} </select>
MyBatis will automatically create a ResultMap object, and then encapsulate the key value pair based on the found attribute name. Then, you will see that the return type is a Blog object, and then take out the key value pair corresponding to the Blog object from the ResultMap and assign it.
3. ResultMap
It is also very useful when the return type is directly a ResultMap. This is mainly used for complex joint queries, because it is not necessary to conduct simple queries.
First, take a look at a simple query with a return type ResultMap, and then take a look at the usage of complex queries.
①Writing a simple query
<resultMap type="Blog" > <id column="id" property="id"/> <result column="title" property="title"/> <result column="content" property="content"/> <result column="owner" property="owner"/> </resultMap> <select parameterType="int" resultMap="BlogResult"> select * from t_blog where id = #{id} </select>
The value of resultMap in the select map is the id of an external resultMap, indicating which result is mapped to. The type attribute of the external resultMap indicates what type the result of the resultMap is. Here is the Blog type, so MyBatis will take it out as a Blog object.
The child node id of the resultMap node is used to identify the id of the object, while the result child node is used to identify some simple properties. The Column property represents the attributes queried from the database, and the Property represents which property the corresponding value of the queryed property is assigned to the entity object.
This is how the resultMap is written for simple query.
②Complex query
There is a Comment class, which has a Blog reference, indicating which Blog it is for. When querying Comment, the corresponding Blog must also be found and assigned to its blog attribute.
public class Comment { private int id; private String content; private Date commentDate = new Date(); private Blog blog; }
From the file
<resultMap type="Comment" > <association property="blog" select="selectBlog" column="blog" javaType="Blog"/> </resultMap> <select parameterType="int" resultMap="CommentResult"> select * from t_Comment where id = #{id} </select> <select parameterType="int" resultType="Blog"> select * from t_Blog where id = #{id} </select>
First, request the select map with id as selectComment, and then obtain a ResultMap object with id as CommentResult, you can see that the return type of the corresponding resultMap is a Comment object, which has only one association node, and there is no id and result child nodes corresponding to the simple query mentioned above, but it will still assign the corresponding id and other attributes to the Comment object. This is what I mentioned above. MyBatis has automatic encapsulation function. As long as the return type is provided, MyBatis will use the query results to encapsulate the corresponding object according to its own judgment. Therefore, in the previous simple query, if it does not clearly indicate which field corresponds to the id and which field corresponds to the title in the resultMap, MyBatis will also help encapsulate it according to its own judgment. MyBatis's own judgment is to compare the field of the query or its corresponding alias with the attributes of the return object. If it matches and the type also matches, MyBatis will assign it.
A blog attribute is associated in the corresponding resultMap above, and its corresponding java type is Blog.In the above writing method, the associated objects are associated through subqueries, and of course, they can also be associated directly through the associated query.
- In the above association child node, the Property attribute indicates which associated attribute of the resultMap return type is. For the above example, the blog attribute managed by Comment;
- select indicates which select mapping is performed to map the corresponding associated attributes, that is, it will request a select mapping with id id corresponding to select to query the associated attribute object;
- Column represents the corresponding key-value pair of the current associated object in the resultMap with id CommentResult. The key-value pair will be used as a parameter for subquerying the associated object. That is, the value of the blog attribute queryed in selectComment will be passed as a parameter to the parameter of the subquery of the associated object blog that performs the associated object blog;
- javaType indicates what type of the current associated object is in JAVA.
The above introduces a query of one-to-one or one-to-many relationships in the case of one-to-one.
In actual applications, there is another application that uses more frequently. It is to find out the corresponding more parties through one-to-one. When taking out the more parties, it also needs to associate one-to-one: when taking out the Blog object, take out all the corresponding Comments, and when taking out the Comment, it still needs to take out the corresponding Blog, which is taken out in Java through a request.
From the file
<resultMap type="Blog" > <id column="id" property="id"/> <collection property="comments" select="selectCommentsByBlog" column="id" ofType="Comment"> </collection> </resultMap> <resultMap type="Comment" > <association property="blog" javaType="Blog" column="blog" select="selectBlog"/> </resultMap> <select parameterType="int" resultMap="BlogResult"> select * from t_blog where id = #{id} </select> <select parameterType="int" resultMap="CommentResult"> select * from t_Comment where blog = #{blogId} </select>
The entry of the above request is a select map with id selectBlog, and the return result is a resultMap with id BlogResult. The type of id BlogResult is Blog, which specifies the attributes and fields of id. Specifying id will have a great effect on the internal construction of MyBatis.
It is associated with a comments object. Because a Blog can have many comments, which are a collection, so it is mapped with a collection collection. The select still indicates which subquery is performed to query the corresponding comments. column means which field value found above is passed to the subquery as a parameter. ofType also represents the return type. The return type here is the type inside the collection. The reason why ofType is used instead of type is that it is used within MyBatis to distinguish it from the association association.
public void selectCommentsByBlogTest() { SqlSession session = ().openSession(); CommentMapper commentMapper = (); List<Comment> comments = (6); for (Comment comment : comments) (comment); (); } public void testSelectOne() { SqlSession session = ().openSession(); BlogMapper blogMapper = (); Blog blog = (6); List<Comment> comments = (); if (comments != null) { for (Comment comment : comments) (comment); } (); }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.