mybatis黑马:延迟加载

[Mybatis中的延迟加载]

  • 问题: 在一对多中,当我们有一个用户,它有100个账户。
    在查询用户的时候,要不要把关联的账户查出来?
    在查询账户的时候,要不要把关联的用户查出来?
    在查询用户时,用户下的账户信息应该是,什么时候使用,什么时候查询的。
    在查询账户时,账户的所属用户信息应该是随着账户查询时一起查询出来。
  • 什么是延迟加载
    在真正使用数据时才发起查询,不用的时候不查询。按需加载(懒加载)
  • 什么是立即加载
    不管用不用,只要一调用方法,马上发起查询。
  • 在对应的四种表关系中:一对多,多对一,一对一,多对多
    一对多,多对多:通常情况下我们都是采用延迟加载
    多对一,一对一:通常情况下我们都是采用立即加载

基本的程序还是沿用之前的表格操作程序,这里只写出关键代码

1.SqlMapConfig.xml的延迟加载配置

    <!--配置参数-->
    <settings>
        <!--开启Mybatis支持延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"></setting>
    </settings>

注意:settings标签配置要在configuration标签里且要按照顺序
以下是各个标签在configuration标签里的排列顺序:
"configuration(按照顺序写,否则报错)" 的内容必须匹配 "(properties, settings,typeAliases, typeHandlers, objectFactory, objectWrapperFactory, reflectorFactory, plugins, environments ,databaseIdProvider, mappers)


2. IAccountDao.xml和IUserDao.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jh.dao.IAccountDao">
<!--一对一映射测试(延迟加载):一个用户(user)对一个账户(account)-->

    <!--定义封装account和user的resultMap -->
    <resultMap id="accountUserMap" type="account">
        <id property="id" column="id"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <!--一对一的关系映射:配置封装user的内容:以uid这个字段来获取关系映射
        (延迟加载属性)select属性指定的内容:查询用户的唯一标识
        column属性指定的内容:用户根据id查询时,所需要的参数的值
        -->
        <association property="user" column="uid" javaType="user" select="com.jh.dao.IUserDao.findById"></association>
    </resultMap>

    <!--配置查询所有-->
    <select id="findAll" resultMap="accountUserMap">
        select * from account
    </select>

    <!--根据用户id查询账户列表-->
    <select id="findAccountByUid" resultMap="accountUserMap">
        select * from account where uid=#{uid}
    </select>

    <!--了解的内容:抽取重复的sql语句-->
    <sql id="defaultAccount">
        select * from account
    </sql>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jh.dao.IUserDao">
<!--一对多关系映射:一个用户(user)对多个账户(account)-->

    <!-- 定义User的resultMap-->
    <resultMap id="userAccountMap" 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>
        <!-- 配置user对象中accounts集合的映射 -->
        <collection property="accounts" ofType="account" select="com.jh.dao.IAccountDao.findAccountByUid" 
        column="id"></collection>
    </resultMap>

    <!--配置查询所有-->
    <select id="findAll" resultMap="userAccountMap">
         select * from user
    </select>

    <!--根据id查询用户-->
    <select id="findById" parameterType="INT" resultType="com.jh.domain.User">
        select * from user where id=#{Uid}<!--这里id命名无所谓-->
    </select>

    <!--了解的内容:抽取重复的sql语句-->
    <sql id="defaultUser">
        select * from user
    </sql>
</mapper>
发布了89 篇原创文章 · 获赞 14 · 访问量 4953

猜你喜欢

转载自blog.csdn.net/JH39456194/article/details/104371307