zoukankan      html  css  js  c++  java
  • 14-延迟加载

    问题:

    1. 在查询用户时,用户下的账户信息应该似乎,什么时候使用,什么时候查询的。
    2. 在查询账户时,账户的所属用户信息应该是随着账户查询时一起查询出来的。

    遇到的不懂:

    mybatis 中javaType和OfType 的区别:

    JavaType和ofType都是用来指定对象类型的,但是JavaType是用来指定pojo中属性的类型,而ofType指定的是映射到list集合属性中pojo的类型。

    一、Mybatis 中的延迟加载

    问题:

    1. 在一对多中,当我们有一个用户,他有一百个账户。
    2. 在查询用户的时候,要不要把关联的账户查出来?
    3. 在查询账户的时候,要不要把关联的用户查出来?

    解析:

    1. 在查询用户时,用户下的账户信息应该似乎,什么时候使用,什么时候查询的。
    2. 在查询账户时,账户的所属用户信息应该是随着账户查询时一起查询出来的。

    1.什么是延迟加载

    在真正使用数据时才发起的查询,不用的时候不查询。按需加载(懒加载)

    2.什么是立即加载

    不管用不用,只要一调用方法,马上发起查询

    在对应的四种表关系中:一对多,多对一,一对一,多对多。下面按关联对象的(多 or 一)分组

    一对多,多对多:通常情况下我们都是采用延迟加载。

    多对一,一对一:通常情况下我们都是采用立即加载。

    二、一对一的延迟加载

    一个账户对应一个用户

    1.实体类

    public class Account implements Serializable {
        private Integer id;
        private Integer uid;
        private double money;
    
        private User user;
    }
    
    public class User implements Serializable {
        private Integer id;
        private String username;
        private String password;
        private Date birthday;
        private String address;
    }
    

    2.接口

    public interface IUserDao {
        List<User> findAll();
    
        User findById(Integer uid);
    }
    
    public interface IAccountDao {
        List<Account> findAll();
    }
    

    3.SqlMapConfig.xml

    多了个 settings 的配置

    具体可见 Mybatis 的 settings

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration  PUBLIC
            "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
    <configuration>
        <properties resource="jdbcConfig.properties"/>
    
        <!--配置参数-->
        <settings>
            <!--开启Mybatis支持延迟加载-->
            <setting name="lazyLoadingEnabled" value="true"/>
            <!--当开启时,任何方法的调用都会加载该对象的所有属性。 否则,每个属性会按需加载-->
            <setting name="aggressiveLazyLoading" value="false"/>
        </settings>
    
        <typeAliases>
            <package name="domain"/>
        </typeAliases>
    
        <environments default="mysql">
            <environment id="mysql">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${driver}"/>
                    <property name="url" value="${url}"/>
                    <property name="username" value="${username}"/>
                    <property name="password" value="${password}"/>
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
            <package name="dao"/>
        </mappers>
    </configuration>
    

    4.IAccountDao.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="dao.IAccountDao">
        <resultMap id="accountUserMap" type="account">
            <id property="id" column="id"/>
            <result property="uid" column="uid"/>
            <result property="money" column="money"/>
            <!--
            一对一的关系映射,配置封装 user 的内容
            select 属性指定的内容,查询用户的唯一标识
            column属性指定的内容:用户根据 id 查询时,所需要的参数的值
            -->
            <association property="user" column="uid" javaType="user" select="dao.IUserDao.findById"/>
        </resultMap>
    
        <select id="findAll" resultMap="accountUserMap">
            select *from account;
        </select>
    </mapper>
    

    5.测试类

        @Test
        public void testFindAll(){
            List<Account> accounts=accountDao.findAll();
            for (Account account:accounts){
                System.out.println("-------------------");
                System.out.println(account.toString());
                System.out.println(account.getUser());
            }
        }
    

    三、一对多的延迟加载

    一个用户对应多个账户

    1.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="dao.IUserDao">
        <resultMap id="userAccountMap" type="User">
            <id property="id" column="id"/>
            <result property="username" column="username"/>
            <result property="password" column="password"/>
            <result property="address" column="address"/>
            <result property="birthday" column="birthday"/>
            <collection property="account" ofType="Account" select="dao.IAccountDao.findAccountById" column="id"/>
        </resultMap>
    
        <select id="findAll" resultType="user">
            select *from user;
        </select>
    
        <select id="findById" parameterType="integer" resultMap="userAccountMap">
            select *from user where id=#{uid}
        </select>
    </mapper>
    
  • 相关阅读:
    GCD实现多个定时器,完美避过NSTimer的三大缺陷(RunLoop、Thread、Leaks)
    iOS适配UIViewView/WKWebView,H5生成长图,仿微信进度条
    翻译jquery官方的插件制作方法
    javascript引用和赋值
    薯片公司真实JS面试题(乐视TV)
    caller、call、apply、callee的用法和意思
    常用javascript类型判断
    Git 常用命令笔记(不定期持续记录)
    sublime text2 emmet 安装
    hash"#"
  • 原文地址:https://www.cnblogs.com/zuiren/p/11406147.html
Copyright © 2011-2022 走看看