我在MyBatis关联模型中找到了这个概念:
我有两张桌子:“人”和“书”,每个人都可以有很多书,
@Select("SELECT * FROM person")
@Results(value = {
@Result(property = "personId", id=true,column = "personId"),
@Result(property="books", column="personId", javaType=ArrayList.class, many=@Many(select="getAllBooks")),
})
ArrayList<Person> getAllPersons ();
@Select("SELECT * FROM book where personId=#{personId}")
ArrayList<Book> getAllBooks(int personId);因此,假设我们有1000人,这意味着这个查询将执行1000次:
@Select("SELECT * FROM book where personId=#{personId}")问题是如何(或者如果可能的话)在一个查询中获取所有书籍,并使用PK/FK将它们映射到所有人。我相信这发生在JPA。
发布于 2013-10-08 17:40:43
我在N+1文档中发现了MyBatis问题:
虽然这种方法很简单,但对于大型数据集或列表,它的性能并不好。这个问题被称为"N+1选择问题“。简而言之,导致N+1选择问题的原因如下:
You execute a single SQL statement to retrieve a list of records (the "+1").
For each record returned, you execute a select statement to load details for each (the "N")这是链接:http://mybatis.github.io/mybatis-3/sqlmap-xml.html
https://stackoverflow.com/questions/19253238
复制相似问题