首页 技术 正文
技术 2022年11月18日
0 收藏 620 点赞 2,781 浏览 2079 个字

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);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,944
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,469
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,283
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,099
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,729
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,766