n+1选择问题定义:
The N+1 Selects problem is caused by trying to load child records that are related to a list of parent records.

在ibatis里有三种解决方案

1.Lazy loading
2.避免N+1 Select
3.通过两条select语句分别从两个表中取数据然后组装


1 Lazy loading:

首先要设置 lazyLoadingEnabled="true"
其次 在map中 注意

<resultMap />
 </resultMap>

select语句同一张表的一样。
<select >
<![CDATA[
select
id,
FLIGHTID,
from cabin_price
where FLIGHTID=#value#
and cabin_number!='0'

]]>
</select>


2. 避免N+1 Select

主要在于 修改mapping文件
以及select语句

<resultMap />
</resultMap>

<resultMap />
</resultMap>

<select >
<![CDATA[
select *
from FLIGHT_RESULT a join cabin_price b on   a.id=b.FLIGHTID
where a.DEP_AIRPORT_CODE= #depairport.airportcode#
and  a.ARR_AIRPORT_CODE=#arrairport.airportcode#
order by a.id,b.FLIGHTID
]]>
</select>


3. 通过两条select语句分别从两个表中取数据然后组装
主要是子表必须提供一种 select * from zibiao in () 的方法


比较三种方案各有适用范围:

第一种方案主要用于那些不需要一次取出主表和子表的数据,而是先取出主表数据,子表数据是通过更详细的查询一次次取出.
第二种方案 适合数据量比较少的主从表,一条语句取出所有数据
第三种方案 则通过两条select语句查询 在程序端组装结果

相关文章:

  • 2021-05-19
  • 2022-02-04
  • 2022-12-23
  • 2021-09-28
  • 2022-12-23
  • 2021-05-20
  • 2022-12-23
  • 2021-07-02
猜你喜欢
  • 2021-09-20
  • 2021-10-16
  • 2022-01-05
  • 2022-12-23
  • 1970-01-01
  • 2022-12-23
相关资源
相似解决方案