14-延迟加载
发布时间:2020-12-15 07:57:33 所属栏目:Java 来源:网络整理
导读:目录 一、Mybatis 中的延迟加载 1.什么是延迟加载 2.什么是立即加载 二、一对一的延迟加载 1.实体类 2.接口 3.SqlMapConfig.xml 4.IAccountDao.xml 5.测试类 三、一对多的延迟加载 1.IUserDao.xml 问题: 在查询用户时,用户下的账户信息应该似乎,什么时候使
目录
问题:
遇到的不懂: mybatis 中javaType和OfType 的区别:
一、Mybatis 中的延迟加载问题:
解析:
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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Java Web 实现QQ登录功能一个帐号同一时间只能一个人登录
- linux配置java环境变量(详细)
- java – 为什么要从float转换为double更改值?
- java – Wait.until()与Webdriver PageFactory元素
- 未清SO关闭处理
- 设计数据库字段或者java中使用boolean型时需谨慎
- java – 通过接口上的注释使@Cachable条目无效?
- 浅谈SpringCache与redis集成实现缓存解决方案
- java – log4j RollingFileAppender与DailyRollingFileAppe
- java – 在Netbeans外部运行jar文件时无法访问映像文件