ResultMap mybatis use of complex queries

 
 
Record collection query problem encountered at the mybatis
 

MyBatis ofType and javaType difference

 

mybatis conflict of the same name associated with the query resultMap

 
ResultMap mybatis use of complex queries
 
mybatis <collection> tag type can not be acquired when duplicate data string error
 
ResultMap be used in two ways-many associated with the query:
Method 1, use the left join query all associated data
1.     <resultMap id="peopleResultMap" type="People">  
2.         <id property="id" column="id" />  
3.         <result property="name" column="name" />   
4.         <collection property="qqs" ofType="string" javaType="list">  
5.             <result column="qq" />  
6.         </collection>  
7.     </resultMap>  
8.       
9.     <select id="selectPeopleById" resultMap="peopleResultMap">  
10.         select p.*,pq.qq from  
11.         people p left join  people_qq pq on p.id = pq.people_id   
12.         where p.id = #{id}  
13.     </select>
 
As described above, the left connection relational query table qq qq field, then qq collections by the resultMap List <String> qqs this property into this field.
Pros: query data only need to check it once.
Cons: Because the left join query, people-many table qq tables, queries a duplicate data row id. So you can not use the built-in database paging function.
select p.*,pq.qq from  
         people p left join  people_qq pq on p.id = pq.people_id   
         where p.id = #{id} 
The above sql query out data format
as follows:
 
Method 2 way query using subqueries
1.     <resultMap id="peopleResultMap" type="People">  
2.         <id property="id" column="id" />  
3.         <result property="name" column="name" />   
4.         <collection property="qqs" ofType="string" javaType="list">  
5.             <result column="qq" />  
6.         </collection>  
7.     </resultMap>  
8.       
9.     <select id="selectPeopleById" resultMap="peopleResultMap">  
10.         select p.*,pq.qq from  
11.         people p left join  people_qq pq on p.id = pq.people_id   
12.         where p.id = #{id}  
13.     </select>

 

 
Sub-queries, is to call another query in the set list, the data assignment.
Advantage: own paging function database
Cons: query data over several calls sub-query field data collection query. If a large amount of data query, you will need to call multiple sub-queries sql.
 
mybatis use lazy loading sub-set of queries by default. There is a problem.
You can view the following article: mybatis problem solving
Springmvc + When mybatis, mybatis delay loading configuration, JSON sequence exception 
HTTPS: // ask.csdn.net/questions/344738?sort=comments_count 


entity class attribute has a type other entity class, MyBatis using default lazy loading, resulting in some properties can not be serialized in dealing with this class, thus causing the above-mentioned reasons 
HTTPS: // www.oschina.net/question/2312022_2232071 
during a recent project, using a cascade mybatis query, configure lazy loading mode, the results returned by wrong json Times springMvc. Given as follows: 
HTTPS: // blog.csdn.net/qq_33548914/article/details/79991280

 

 

Guess you like

Origin www.cnblogs.com/gne-hwz/p/11457449.html