In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the relationship between resultType and resultMap in Mybatis and the use scenario". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
1. summary
Mybatis ORM semi-automatic mapping framework should be one of the must-know frameworks for Java developers. Its benefits are not the focus of our discussion here. What confuses many Java developers who have just entered the industry is the use of resultType and resultMap. Let's explore this issue today.
2. resultType and resultMap
Next, let's talk about the relationship between resultType and resultMap in Mybatis and the usage scenarios.
2.1 resultType
The fully qualified name or alias of the class of the expected type returned from the select statement. Note that if a collection is returned, it should be set to the type contained in the collection, not the collection itself. You can use either resultType or resultMap, but not both.
2.2 resultMap
The fully qualified name or alias of the class of the expected type returned from this statement. Note that if a collection is returned, it should be set to the type contained in the collection, not the collection itself. You can use either resultType or resultMap, but not both.
2.3 common ground
ResultType and resultMap are first used to summarize the result set of query type sql, and describe the structure you need for query results. Both cannot coexist in the same select tag. This is understandable to many people. What matters is the different points and the corresponding usage scenarios.
2.4 Use scenarios for both
For simple pojo queries that the query structure needs to return, the results can be mapped to consistent hashmaps, in other words database column names that exactly match pojo attributes. Usually resultType is used. There's actually an implicit construction mechanism here. The results mapped to resultTypes are all ones MyBatis automatically creates a resultMap behind the scenes to process. In short, resultMap can do anything resultType can do. The two positions are:
ResultType is used to handle very simple result sets, that is, result sets whose column names match pojo attributes. If you only need a simple query about a class, then this is the perfect one.
@Data public class Grade{ private String gradeId private String gradeName; private Integer studentCount; }
We can easily use the following operations:
select gradeId,gradeName, studentCount from grade where gradeId = #{gradeId}
Of course, you can also use resultMap as mentioned above:
Then convert the resultType above to resultMap. Please note that I have two hump styles that need to be processed. Of course, you can set whether Mybatis uses humps to avoid them.
ResultMap is better at handling complex mapping result sets. For example, one-to-one and one-to-many complex relationships. If you want to inquire not only about a class, but also about the school where the class is located, the details of the students in the class, or even the profile of the male and female students in the class. You must use resultMap to describe these mappings. For example, let's write:
We define a DTO describing the relationship above:
@Data public class GradeDTO { private String gradeId private String gradeName; private Integer studentCount; private School school; private List teachers private List boyStudents; private List girlStudents; }
Corresponding mapping processing:
Of course, resultMap can also be inherited like java classes. Anyway, if you want, you can play with this stuff. But pay attention to performance issues, try not to nest too much. Try to configure lazyLoadingEnabled for on-demand loading.
3. summary
This article mainly through the simple analysis resultType and resultMap the same point and different point to clarify their respective use scenarios. More detailed instructions can be found in Mybatis's official documentation. Hope that through the explanation of this article let you in the actual development work no longer confused more clear.
The content of "The relationship between resultType and resultMap in Mybatis and the usage scenario" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.