In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use the collection tag in resultMap". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to use the collection tag in resultMap" can help you solve the problem.
The collection tag collection in the resultMap tag (one to many)
Elements act in much the same way as association elements, in fact, they are very similar and map to a "complex type" attribute of JavaBean, except that this attribute is a list of collections, that is, a complex data type (collection) nested within JavaBean. As with the association element, we use nested queries or nested result sets from joins.
The following is an example to demonstrate the specific application of coeltien, which needs to obtain the relevant information and address list of the specified user.
We have the entity class Employee as follows:
Package com.xyj.entity;import java.util.List;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@NoArgsConstructor@AllArgsConstructorpublic class Employee {all sports participated in by private int eid; private String ename; private String epwd; private String address; private String tel; private List sports;// staff}
A small sports meeting was held among the staff, and the Sport entity classes are as follows:
Package com.xyj.entity;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@NoArgsConstructor@AllArgsConstructorpublic class Sport {private int sportId; private String sportName; private String sportScore;}
You can know that an employee can participate in multiple sports, so a complex data type attribute, sports, is nested inside our Employee object. Next, add a method to the EmpDao API to obtain the list of items that the employee participates in according to the employee id. The code is as follows:
List findSportsInfoByEmpId (@ Param ("eid") Integer id)
Modify the corresponding mapping file. Because the collection objects are nested inside Employee, you need to use collection to implement the result mapping. The example code is as follows:
Select e.recording journal s.* from employee as erecording as s where e.eid=s.eid and e.eid=# {eid}
Finally, test to see if the results are correct:
Org.junit.Test public void test () {SqlSession session = MyBatisUtils.getSqlSession (); EmpDao ed = session.getMapper (EmpDao.class); List list = ed.findSportsInfoByEmpId (1); for (Employee emp: list) {for (Sport e: emp.getSports ()) {System.out.println (e);}
Running result:
The query is successful, but our collection tag is written inside the resultMap tag and cannot be reused. Of course, it can be reused. You only need to modify the code as follows:
Select e.recording journal s.* from employee as e Sport as s where e.eid=s.eid and e.eid=# {eid} So much for the description of the attributes in the collection tag about "how to use the collection tag in resultMap". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.