Day20JavaWeb [tourism project] query optimization and delayed loading

One-to-one, one-to-many, many-to-many in the database

Insert picture description here

One-to-one, one-to-many in Java classes

Insert picture description here
Such as:

//旅游路线
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

Through resultMap and association and collection

Simplify the query and assignment of special types of member variables

<!--    要对当前这个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>

Simplified service method

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

Lazy loading

  • (1) Global settings can be set in the core configuration file or local settings can be set in the mapping file
 <settings>
        <setting name="lazyLoadingEnabled" value="true" />
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

  • (2) The member variable of the object, execute sql only when it is used
  • (3) fetchType="eager"Query immediately
  • (4) fetchType="lazy"Delayed query If the data is called, the query will be executed, otherwise it will not be executed.
   <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>

Guess you like

Origin blog.csdn.net/u013621398/article/details/108951455