Day20JavaWeb [관광 프로젝트] 쿼리 최적화 및 로딩 지연

데이터베이스에서 일대일, 일대 다, 다 대다

여기에 사진 설명 삽입

Java 클래스에서 일대일, 일대 다

여기에 사진 설명 삽입
예 :

//旅游路线
public class Route {
    
     //A
    private int rid;//线路id,必输
    private String rname;//线路名称,必输
    ...
    //分类数据
    private Category category; //B  一对一 association
    //商家数据
    private Seller  seller;//C 一对一 association
    //图片数据
    private List<RouteImg> imgList; //集合  一对多 collection

resultMap 및 연관 및 수집을 통해

특수 유형의 멤버 변수에 대한 쿼리 및 할당 단순화

<!--    要对当前这个route内部的其他的成员变量进行查询与赋值
select 指定接口方法使用到的语句
property指定需要查询的数据
column 指定select方法需要的参数
select 指定需要调用的dao方法
-->
    <resultMap id="routeMap" type="route" autoMapping="true">
            <association property="category" column="cid" select="com.wzx.dao.CategoryDao.findOneByCid"  autoMapping="true"/>
            <association property="seller" column="sid" select="com.wzx.dao.SellerDao.findOneBySid"  autoMapping="true"/>
            <collection property="imgList" column="rid"  select="com.wzx.dao.RouteImgDao.findAllImgByRid"  autoMapping="true"/>
    </resultMap>

단순화 된 서비스 방법

 public Route findRouteById(int rid) {
    
    
        //数据来自四个表,执行四个查找方法
        //路线数据
        RouteDao routeDao = MySessionUtils2.getMapper(RouteDao.class);
        Route route =routeDao.findOneByRid(rid);
        //通过assocication标签 与collection标签可以让mybatis帮我查询其他三个数据
        //商家的数据 分类的数据,与图片集合的数据
        return route;
    }

지연 로딩

  • (1) 글로벌 설정은 코어 구성 파일에서 설정하거나 로컬 설정은 매핑 파일에서 설정할 수 있습니다.
 <settings>
        <setting name="lazyLoadingEnabled" value="true" />
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

  • (2) 객체의 멤버 변수, 사용시에만 sql 실행
  • (3) fetchType="eager"즉시 쿼리
  • (4) fetchType="lazy"지연된 질의 데이터가 호출되면 질의가 수행되고 그렇지 않으면 수행되지 않습니다.
   <resultMap id="routeMap" type="route" autoMapping="true">
            <association property="category" column="cid" select="com.wzx.dao.CategoryDao.findOneByCid"  autoMapping="true" fetchType="eager"/>
            <association property="seller" column="sid" select="com.wzx.dao.SellerDao.findOneBySid"  autoMapping="true" fetchType="eager"/>
            <collection property="imgList" column="rid"  select="com.wzx.dao.RouteImgDao.findAllImgByRid"   autoMapping="true" fetchType="eager" />
    </resultMap>

추천

출처blog.csdn.net/u013621398/article/details/108951455