mybatis中的延迟加载

Mybatis中的延迟加载

  • 概念:
在查询数据时,并不立即发起查询,只有在真正的要使用到数据的时候,才查询. 不用的时候不查询。按需加载(懒加载)适用与多表查询时.
  • 优点:
节省内存,提高程序运行效率.
mybatis的延迟加载可以提高开发效率(代码写的少了)
  • 缺点:
因为只有当需要用到数据时,才会进行数据库查询,这样在大批量数据查询时,因为查询工作也要消耗时间,所以可能造成用户等待时间变长,造成用户体验下降。
  • 配置:
	//在SqlMapConfig.xml配置文件中开启延迟加载.
	<settings>
		<setting name="lazyLoadingEnabled" value="true"/>
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>

实体类代码

public User{
    private Integer id;
    private String username;
    private String address;
    private String sex;
    private Date birthday;
    private List<Account> accounts;
    
    set...get...
}

public Account{
    private Integer id;
    private Integer uid;
    private Double money;
    private User user;  
   
   set...get... 
}

Mybatis中的延迟加载-(1对1,多对1)

  • 1.sql语句
AccountDao.xml

	<select id="findAll" resultMap="accountUserMap">
		select * from account
	</select>
	
UserDao.xml
    <select id="findById" resultMap="user" parameterType="int">
		select * from user where id = #{id}
	</select>
  • 2.AccountDao.xml中的resultMap标签
<resultMap id="accountUserMap" type="account">
	<id property="id" column="id"></id>
	<result property="uid" column="uid"></result>
	<result property="money" column="money"></result>
	<association property="user" column="uid" javaType="user" select="com.huyang.dao.UserDao.findById"></association>
</resultMap>
/**
 *	注意:
 *		1. result标签和id标签,仍然封装"主表"属性
 *		2. association标签仍然指定"从表"的相关信息.
 *				property="user"	:表示把"从表"数据封装到"主表"的user属性中.
 *				select="com.huyang.dao.UserDao.findById"	:表示延迟加载通过UserDao的findById方法获取"从表"数据
 *				column="uid"	:表示调用findById方法时,把"主表"uid属性作为参数,传递给该方法
 *				javaType="user" :表示调用findById方法得到的结果封装为User类型
 *		3. 有了延迟加载后,
 *				不需要自己配置"从表"的映射信息
 *				不需要多表操作语句. 也就是说,即使是多表操作,sql也按照单标操作的语句来写就可以.
 *		
 */

Mybatis中的延迟加载-(1对多,多对多)

  • 1.sql语句
UserDao.xml:

	<select id="findAll" resultMap="userAccountMap">
		select * from user
	</select>
	
AccountDao.xml:
    <select id="findAccountByUid" resultMap="account" parameterType="int">
		select * from account where uid = #{uid}
	</select>
  • 2.UserDao.xml中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>
		<collection property="accounts" ofType="account" select="com.huyang.dao.AccountDao.findAccountByUid" column="id"></collection>
	</resultMap>
	/**
	 *	注意:
	 *		1. result标签和id标签,仍然封装"主表"属性
	 *		2. association标签仍然指定"从表"的相关信息.
	 *				property="accounts"	:表示把"从表"数据封装到"主表"的accounts属性中.
	 *				select="com.huyang.dao.AccountDao.findAccountByUid"	:表示延迟加载"从表"数据时,调用的方法
	 *				column="id"	:表示调用findAccountByUid方法时,把"主表"id属性作为参数,传递给该方法
	 *				ofType="account" :表示调用findAccountByUid方法得到的结果封装为List集合时,集合中的数据的数据类型为account
	 *		3. 有了延迟加载后,
	 *				不需要自己配置"从表"的映射信息
	 *				不需要多表操作语句. 也就是说,即使是多表操作,sql也按照单标操作的语句来写就可以.
	 *		
	 */

Mybatis的缓存

  • 缓存
什么是缓存:

    存在于内存中的临时数据
    
为什么使用缓存:

    减少和数据库的交互次数,提高执行效率
    
什么样的数据能使用缓存,什么样的数据不能使用缓存:

    适用于缓存:
        经常查询并且不经常改变的
        数据的正确与否对最终结果影响不大的
        
    不使用与缓存:
        经常改变的数据
        数据的正确与否对最终结果影响大的
        例如:商品的库存,银行的汇率,股市的牌价
  • mybatis中的缓存
一级缓存:

	默认支持,他是SqlSession级别的缓存. 只要SqlSession不销毁,则缓存就一直有效.
	如果,在SqlSession调用增删改和commit(),close()等方法的时候,缓存会被自动清空.
	
二级缓存(不常用):

	使用步骤:
		第1步:让Mybatis框架支持二级缓存(在SqlMapConfig.xml中配置)
			<settings>
				<setting name="cacheEnabled" value="true"/>
			</settings>
		第2步:让当前的映射文件支持二级缓存(在IUserDao.xml中配置)
			//<!--开启user支持二级缓存,每个类要单独开启 -->
			<cache/>
		第3步:让当前的操作支持二级缓存(在select标签中配置)
			//useCache="true",让当前方法,支持二级缓存,每个方法要单独设置	
			<select id="findById" parameterType="INT" resultType="user" useCache="true">
				select * from user where id = #{uid}
			</select>
结论:
	Mybatis的二级缓存,用起来非常麻烦,而且已经有一级缓存,并且,这种缓存,其实还有redis来实现,所以,这个东西,不用

猜你喜欢

转载自blog.csdn.net/weixin_43343131/article/details/84786853
今日推荐