resultMap and resultType (transfer)

 Sourcehttp ://haohaoxuexi.iteye.com/blog/1337009      

      When querying select mapping in MyBatis, the return type can be resultType or resultMap. The resultType directly represents the return type, and the resultMap is a reference to the external ResultMap, but the resultType and the resultMap cannot exist at the same time. When MyBatis performs query mapping, in fact, each attribute queried 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 extract the key-value pair in the Map and assign it to the attribute corresponding to the object specified by resultType. So in fact, the return type of each query map of MyBatis is ResultMap, but when the return type attribute we provide is resultType, MyBatis automatically assigns the corresponding value to the attribute of the object specified by resultType, and when we provide the return type attribute is resultType When the provided return type is resultMap, because Map cannot represent the domain model well, we need to further convert it into the corresponding object, which is often useful in complex queries.

  There is such a Blog.java file

 

import java.util.List;

public class Blog {

	private int id;

	private String title;

	private String content;

	private String owner;
	
	private List<Comment> comments;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}
	
	public List<Comment> getComments() {
		return comments;
	}
	
	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	@Override
	public String toString() {
		return " ----------------博客-----------------\n id: " + id + "\n title: " + title + "\n content: " + content
				+ "\n owner: " + owner;
	}

}

 

The corresponding database table stores id, title, Content, Owner attributes, then when we perform the following query mapping

 

<typeAlias ​​alias="Blog" type="com.tiantian.mybatis.model.Blog"/><!--Configuration file from MyBatis mybatis_config.xml-->
<select id="selectBlog" parameterType="int" resultType="Blog">
  		select * from t_blog where id = #{id}
</select><!--from the SQL mapping file BlogMapper.xml-->

 

MyBatis will automatically create a ResultMap object, and then encapsulate the key-value pair based on the property name found, and then 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.

It is also very useful when the return type is directly a ResultMap, which is mainly used for complex union queries, because simple queries are not necessary. Let's first look at a simple query with a return type of ResultMap, and then look at the usage of complex queries.

Writing a simple query

 

<resultMap type="Blog" id="BlogResult">
		<id column="id" property="id"/>
		<result column="title" property="title"/>

		<result column="content" property="content"/>
		<result column="owner" property="owner"/>
	</resultMap>
	<select id="selectBlog" parameterType="int" resultMap="BlogResult">
		select * from t_blog where id = #{id}
  	</select>

 

The value of resultMap in the select mapping is the id of an external resultMap, indicating which resultMap the returned result is mapped to. The type attribute of the external resultMap indicates what type of result the resultMap is. Here is the Blog type, then MyBatis will put the It is taken 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 attributes. The Column attribute represents the attributes queried from the database, and the Property represents the corresponding attributes of the queried attributes. Which property of the entity object is assigned the value of . This is how the resultMap of a simple query is written. Let's look at a slightly more complex query.

There is a Comment class, which has a reference to a Blog, indicating which Blog is the Comment, then when we query the Comment, we also need to find out the corresponding Blog and assign it to its blog attribute.

 

import java.util.Date;

public class Comment {

	private int id;
	
	private String content;
	
	private Date commentDate = new Date();
	
	private Blog blog;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getCommentDate() {
		return commentDate;
	}

	public void setCommentDate(Date commentDate) {
		this.commentDate = commentDate;
	}

	public Blog getBlog() {
		return blog;
	}

	public void setBlog(Blog blog) {
		this.blog = blog;
	}
	
	public String toString() {
		return blog + "\n ----------------评论-----------------\n id: " + id + "\n content: " + content + "\n commentDate: " + commentDate;
	}
	
}

 

It's written like this

 

<!--From CommentMapper.xml file -->

	<resultMap type="Comment" id="CommentResult">
		<association property="blog" select="selectBlog" column="blog" javaType="Blog"/>
	</resultMap>
	
	<select id="selectComment" parameterType="int" resultMap="CommentResult">
		select * from t_Comment where id = #{id}
	</select>
	
	<select id="selectBlog" parameterType="int" resultType="Blog">

		select * from t_Blog where id = #{id}
	</select>

The access situation is like this. First, the select map with the id of selectComment is requested, and then a ResultMap object with the id of CommentResult is obtained. We can see that the return type of the corresponding resultMap is a Comment object, in which there is only one association node, but no Like the 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, which is the automatic encapsulation function of MyBatis mentioned above, as long as you provide the return type, MyBatis It will use the query result to encapsulate the corresponding object according to its own judgment, so in the previous simple query, if you do not clearly indicate which field the id corresponds to in the resultMap, and which field the title corresponds to, MyBatis will also encapsulate it for you according to its own judgment. , MyBatis's own judgment is to compare the field of the query or its corresponding alias with the properties of the returned object. If it matches and the type also matches, MyBatis will assign it. In the corresponding resultMap above, a blog attribute is associated, and its corresponding JAVA type is Blog. In the above writing method, the associated objects are associated through subqueries, of course, they can also be associated directly through association queries. In the association sub-node above, the Property attribute indicates which associated attribute of the resultMap return type. For the above example, it is the blog attribute managed by Comment; select indicates which select mapping is performed to map the corresponding associated attribute, that is, the request id is select. The select map of the corresponding value is used to query the associated attribute object; Column represents the key-value pair corresponding to the current associated object in the resultMap whose id is CommentResult, and the key-value pair will be used as the parameter of the sub-query on the associated object. The value of the blog property queried in selectComment is about to be passed as a parameter to the parameter of the sub-query selectBlog of the associated object blog; javaType indicates what type of the current associated object in JAVA.

 

The above description is a one-to-one or one-to-many case, the query of the one-to-one association. In practical applications, there is another application that is used more often. The one side is used to find out the corresponding side. When taking out the more side, the one side should also be associated. That is, in the above example, When taking out the Blog object, take out all its corresponding Comments, and when taking out the Comments, you still need to take out its corresponding Blog, which is taken out by one request in JAVA. It is written as follows:

 

<!-- from BlogMapper.xml file-->
	<resultMap type="Blog" id="BlogResult">

		<id column="id" property="id"/>
		<collection property="comments" select="selectCommentsByBlog" column="id" ofType="Comment"></collection>
	</resultMap>

	<resultMap type="Comment" id="CommentResult">
		<association property="blog" javaType="Blog" column="blog" select="selectBlog"/>

	</resultMap>

	<select id="selectBlog" parameterType="int" resultMap="BlogResult">
		select * from t_blog where id = #{id}
  	</select>

<!-- Find Comments through Blog -->
  	<select id="selectCommentsByBlog" parameterType="int" resultMap="CommentResult">

  		select * from t_Comment where blog = #{blogId}
  	</select>

The entry of the above request is the select map with the id of selectBlog, the returned result is the resultMap with the id of BlogResult, and the type of the id of BlogResult is Blog, in which the attributes and fields of the id are specified, and the specified id will have a great effect on the internal structure of MyBatis. There is a comments object associated with it, because a Blog can have many comments, and the comments are a collection, so the collection is used for mapping, and the select in it still indicates which sub-query to query the corresponding comments, and the column indicates that the above checked out Which field value is passed as a parameter to the subquery, ofType also indicates the return type, the return type here is the type inside the collection, the reason why ofType is used instead of type is inside MyBatis to distinguish it from the associated association.

 

 

 

Test code:

 

	@Test
	public void selectCommentsByBlogTest() {
		SqlSession session = Util.getSqlSessionFactory().openSession();
		CommentMapper commentMapper = session.getMapper(CommentMapper.class);
		List<Comment> comments = commentMapper.selectCommentsByBlog(6);
		for (Comment comment : comments)
			System.out.println(comment);
		session.close();
	}

	/**
	 * Query a single record
	 */
	@Test
	public void testSelectOne() {
		SqlSession session = Util.getSqlSessionFactory().openSession();
		BlogMapper blogMapper = session.getMapper(BlogMapper.class);
		Blog blog = blogMapper.selectBlog(6);
		List<Comment> comments = blog.getComments();
		if (comments != null) {
			System.out.println("--------------Comments Size------------" + comments.size());
			for (Comment comment : comments)
				System.out.println(comment);
		}
		session.close();
	}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326629288&siteId=291194637