编程人 cdmana.com

In mybatis one to many query, only one piece of data is encapsulated in the set

describe :

One to many queries , One message corresponds to multiple data , We use collections to store data from multiple parties . Use resultMap Tag to define the format of the encapsulation , In the test today , Only one piece of data is encapsulated in the set .

reason :

After consulting the information, we know that , Because both tables have id Field , But they didn't explain why , I didn't go into it , But the guess should be because the result set of the query has two id Field ,mybatis Put the main table's id Values are also stored in the id Field
 Insert picture description here When reading the first line of data, because I'm not sure if there is this one behind id Line return for ,mybatis The first line will exist Cahce Class , When reading the second line ,mybatis Judge sub table id The values are found to be the same , You think it's the same data .

Solution :

To one of them id Add an alias to the field
 Insert picture description here Running results
 Insert picture description here

Scroll to Top