zoukankan      html  css  js  c++  java
  • mybatis中的延迟查询思想

    1、一对一延迟加载

    延迟加载:

    就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据。延迟加载也称懒加载.
    好处:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速
    度要快。

    坏处:

    因为只有当需要用到数据时,才会进行数据库查询,这样在大批量数据查询时,因为查询工作也要消耗
    时间,所以可能造成用户等待时间变长,造成用户体验下降。

    进入 Mybaits 的官方文档,找到 settings 的说明信息:

    我们需要在 Mybatis 的配置文件 SqlMapConfig.xml 文件中添加延迟加载的配置。

    <settings> <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    
    配置
    <resultMap id="accountMap" type="com.itheim.domain.Account">
            <id property="id" column="aid"></id>
            <result property="money" column="money"></result>
            <result property="uid" column="uid"></result>
            <!-- 一对一的关系映射 封装user的内容
             select 属性指定的内容,查询用户的唯一标识
             column 属性指定的内容,用户根据id查询时,所需要的参数的值
             -->
            <association property="user" column="uid" javaType="user" select="com.itheim.dao.IUserDao.findByid"><!-- javatype用于提示封装到那个对象 --></association>
    </resultMap>
     sql语句
    
    <!--一对一的查询-->
        <select id="findAccount" resultMap="accountMap"> <!--指定包装类型 -->
            select * from account;
        </select>
    
       <select id="findByid" resultType="user" parameterType="INT">
            select * from user where id=#{id};
        </select>
    
     @Test
        public void findAccoutUser(){
            List<Account> accountUser = userDao.findAccount();
            for (Account account : accountUser) {
                System.out.println(account);
                System.out.println(account.getUser());
            }
        }
    

    一对多查询

    <!--一对多查询 -->
        <resultMap id="resultaccountMap" type="user">
            <id property="id" column="id"></id>
            <result property="username" column="username"></result>
            <result property="address" column="address"></result>
            <result property="sex" column="sex"></result>
            <result property="birthday" column="birthday"></result>
            <collection property="account" ofType="account" column="id" select="com.itheim.dao.IUserDao.findByidaccount"> <!-- ofType每个元素的类型 -->
            </collection>
        </resultMap>
        <!--一对多查询 延迟方法-->
        <select id="findAccountByUser" resultMap="resultaccountMap">
              select * from user ;
        </select>
    <!--一对多延迟方法根据id查询account表-->
        <select id="findByidaccount" resultType="account">
            select * from account where uid=#{id};
        </select>
    
    、//测试类
       @Test
        public void findAccountByUser(){
            List<User> accountByUser = userDao.findAccountByUser();
            for (User user : accountByUser) {
                System.out.println(user);
                List<Account> accounts = user.getAccount();
    //            for (Account account : accounts) {
    //                System.out.println(account);
                }
            }
    
  • 相关阅读:
    Flink基础(57):FLINK-SQL函数(20) 内置函数(15)日期函数(二)
    Flink基础(56):FLINK-SQL函数(19)内置函数(14)字符串函数(五)
    位示图算法实现大数据的存储
    位示图算法实现大数据的存储
    JobTracker作业启动过程分析
    mysql 查询优化~ 分页优化讲解
    mysql 原理~ index的详解
    指纹识别开发1.0
    java8 base64编码和解码
    Cow Bowling POJ
  • 原文地址:https://www.cnblogs.com/zgrey/p/13330596.html
Copyright © 2011-2022 走看看