Project scenario:
One-to-many pagination query, query the main table and find out the sub-table data at the same time
Problem Description
Mybatis-plus pagination plug-in multi-table connection query number is wrong, the
total number of records is 8,
actually only 7 records are queried
, and one piece of data is lost, and it will also cause the loss of sub-data in the main data
– (if a certain record of A If the data has 3 pieces of sub-data of B, only 1 or 2 pieces may be detected)
Cause Analysis:
一对一的情况下不会出现一下数据丢失问题
For example, in the figure,
resultMap
wecollection
specify the mapping of one-to-many subclass data through the label in
The query results in sql are as follows:
A:
the homework tableB:
is the intermediate table (without demonstration)C:
and the student table We can see that (3 historical assignments, 2 biological assignments)
are queried. This is because there are , aside from repeated data , only one piece of data is queried If the sports job is the first , the sports job will be abandoned because of the setting ~ Mybatis-plus uses direct mapping, the number of data is wrong, and only 7 data are queried out of 8 data.重复数据
历史作业(A表)
3位学生(B表)
7
11条
分页数量为10
Summarize:
其实A表一共才8条数据,是可以全部查询出来的,因为被重复数据占用了,导致数据丢失。
solution:
Query the data of the child table separately
①: Because the intermediate table (B table) field is not in the mapped entity, you need to add the corresponding field in the mapped entity, as shown in the figure below ②: The
name of the sub-table data set in the mapped entity
③: The associated field is the sub-table Field required for query
④: the id name of the select tag of the sub-table query statement
⑤: the parameter passed in by the main table query, which is the value of ③
Notice:
1. In the main table, query the fields that need to be associated with subtables in the intermediate table and take the aliases corresponding to the fields added by the mapping entity as shown in Figure ①. 2.
Delete the mapping fields of the subtables under the collection, and map them through separate sql
3. This way of writing you请求条件参数
cannot pass to the sql statement of the sub-table query, the condition parameter of the sub-table sql can only be主表column属性
the field