← 返回学习路线

SYSTEM

延迟加载

Java基础延迟加载system
src/content/notes/system/延迟加载.md

延迟加载

两种加载

立即加载:只要调用方法,马上发起查询

延迟加载:在需要用到数据时才进行加载,不需要用到数据时就不加载数据,延迟加载也称懒加载

优点: 先从单表查询,需要时再从关联表去关联查询,提高数据库性能,因为查询单表要比关联查询多张表速度要快,节省资源

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

核心配置文件:

标签名 描述 默认值
lazyLoadingEnabled 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载,特定关联关系中可通过设置 fetchType 属性来覆盖该项的开关状态。 false
aggressiveLazyLoading 开启时,任一方法的调用都会加载该对象的所有延迟加载属性。否则每个延迟加载属性会按需加载(参考 lazyLoadTriggerMethods) false
<settings> 
	<setting name="lazyLoadingEnabled" value="true"/> 
    <setting name="aggressiveLazyLoading" value="false"/> 
</settings>

assocation

分布查询:先按照身份 id 查询所属人的 id、然后根据所属人的 id 去查询人的全部信息,这就是分步查询

  • 映射配置文件 OneToOneMapper.xml

    一对一映射:

    • column 属性表示给要调用的其它的 select 标签传入的参数
    • select 属性表示调用其它的 select 标签
    • fetchType=“lazy” 表示延迟加载(局部配置,只有配置了这个的地方才会延迟加载)
    <mapper namespace="OneToOneMapper">
        <!--配置字段和实体对象属性的映射关系-->
        <resultMap id="oneToOne" type="card">
            <id column="id" property="id" />
            <result column="number" property="number" />
            <association property="p" javaType="bean.Person"
                         column="pid" 
                         select="one_to_one.PersonMapper.findPersonByid"
                         fetchType="lazy">
                		<!--需要配置新的映射文件-->
            </association>
        </resultMap>
    
        <select id="selectAll" resultMap="oneToOne"> 
            SELECT * FROM card <!--查询全部,负责根据条件直接全部加载-->
        </select>
    </mapper>
  • PersonMapper.xml

    <mapper namespace="one_to_one.PersonMapper">
        <select id="findPersonByid" parameterType="int" resultType="person">
            SELECT * FROM person WHERE id=#{pid}
        </select>
    </mapper>
  • PersonMapper.java

    public interface PersonMapper {
        User findPersonByid(int id);
    }
  • 测试文件

    public class Test01 {
        @Test
        public void selectAll() throws Exception{
            InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = ssf.openSession(true);
            OneToOneMapper mapper = sqlSession.getMapper(OneToOneMapper.class);
            // 调用实现类的方法,接收结果
            List<Card> list = mapper.selectAll();
            
          	// 不能遍历,遍历就是相当于使用了该数据,需要加载,不遍历就是没有使用。
            
            // 释放资源
            sqlSession.close();
            is.close();
        }
    }

collection

同样在一对多关系配置的 结点中配置延迟加载策略, 结点中也有 select 属性和 column 属性

  • 映射配置文件 OneToManyMapper.xml

    一对多映射:

    • column 是用于指定使用哪个字段的值作为条件查询
    • select 是用于指定查询账户的唯一标识(账户的 dao 全限定类名加上方法名称)
    <mapper namespace="OneToManyMapper">
        <resultMap id="oneToMany" type="bean.Classes">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
    
            <!--collection:配置被包含的集合对象映射关系-->
            <collection property="students" ofType="bean.Student"
                        column="id" 
                        select="one_to_one.StudentMapper.findStudentByCid">
            </collection>
        </resultMap>
        <select id="selectAll" resultMap="oneToMany">
          SELECT * FROM classes
        </select>
    </mapper>
  • StudentMapper.xml

    <mapper namespace="one_to_one.StudentMapper">
        <select id="findPersonByCid" parameterType="int" resultType="student">
            SELECT * FROM person WHERE cid=#{id}
        </select>
    </mapper>