Mybatis(延迟加载,缓存,注解开发)

1. Mybatis 延迟加载策略

Mybatis 中一对一,一对多,多对多关系的配置可以实现对象的关联查询。实际开发过程中很多时候我们并不需要总是在加载用户信息时就一定要加载他的账户信息。此时就是我们所说的延迟加载。

1.1 概念

延迟加载:就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据。延迟加载也称懒加载.


好处:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速度要快。


坏处:因为只有当需要用到数据时,才会进行数据库查询,这样在大批量数据查询时,因为查询工作也要消耗时间,所以可能造成用户等待时间变长,造成用户体验下降。

1.2 使用 assocation 实现延迟加载

账户的持久层映射文件

<?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="org.woster.dao.IAccountDao">
    <!-- 建立对应关系 -->
    <resultMap type="account" id="accountMap">
        <id column="aid" property="id"/>
        <result column="uid" property="uid"/>
        <result column="money" property="money"/>
        <!-- 它是用于指定从表方的引用实体属性的 -->
        <association property="user" javaType="user"
                     select="org.woster.dao.IUserDao.findById"
                     column="uid">
        </association>
    </resultMap>
    <select id="findAll" resultMap="accountMap">
        select * from account
    </select>
</mapper>
  • select: 填写我们要调用的 select 映射的 id
  • column : 填写我们要传递给 select 映射的参数

用户的持久层映射文件

<?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="org.woster.dao.IUserDao">
    <!-- 根据 id 查询 -->
    <select id="findById" resultType="user" parameterType="int" >
        select * from user where id = #{uid}
    </select>
</mapper>

开启 Mybatis 的延迟加载策略

进入 Mybaits 的官方文档,找到 settings 的说明信息:
在这里插入图片描述
我们需要在 Mybatis 的配置文件 SqlMapConfig.xml 文件中添加延迟加载的配置。

<!-- 开启延迟加载的支持 -->
<settings>
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="false"/>
</settings>

1.3 使用 Collection 实现延迟加载

同样我们也可以在一对多关系配置的<collection>结点中配置延迟加载策略。<collection>结点中也有 select 属性,column 属性。

在 User 实体类中加入 List<Account>属性

public class User implements Serializable {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
    private List<Account> accounts;
    //省略getter及setter
}

编写用户持久层映射配置

<resultMap type="user" id="userMap">
    <id column="id" property="id"></id>
    <result column="username" property="username"/>
    <result column="address" property="address"/>
    <result column="sex" property="sex"/>
    <result column="birthday" property="birthday"/>
    <!-- collection 是用于建立一对多中集合属性的对应关系
        ofType 用于指定集合元素的数据类型
        select 是用于指定查询账户的唯一标识(账户的 dao 全限定类名加上方法名称)
        column 是用于指定使用哪个字段的值作为条件查询
    -->
    <collection property="accounts" ofType="account"
                select="org.woster.dao.IAccountDao.findByUid"
                column="id">
    </collection>
</resultMap>
<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">
    select * from user
</select
  • <collection>标签:主要用于加载关联的集合对象
  • select 属性:用于指定查询 account 列表的 sql 语句,所以填写的是该 sql 映射的 id
  • column 属性:用于指定 select 属性的 sql 语句的参数来源,

编写账户持久层映射配置

<!-- 根据用户 id 查询账户信息 -->
<select id="findByUid" resultType="account" parameterType="int">
    select * from account where uid = #{uid}
</select>

2. Mybatis 缓存

像大多数的持久化框架一样,Mybatis 也提供了缓存策略,通过缓存策略来减少数据库的查询次数,从而提高性能


Mybatis 中缓存分为一级缓存,二级缓存。
在这里插入图片描述

2.1 Mybatis 一级缓存

一级缓存是 SqlSession 级别的缓存,只要 SqlSession 没有 flush 或 close,它就存在。

一级缓存的分析

级缓存是 SqlSession 范围的缓存,当调用 SqlSession 的修改,添加,删除,commit(),close()等方法时,就会清空一级缓存。

在这里插入图片描述

  1. 第一次发起查询用户 id 为 1 的用户信息,先去找缓存中是否有 id 为 1 的用户信息,如果没有,从数据库查询用户信息

  2. 得到用户信息,将用户信息存储到一级缓存中。

  3. 如果 sqlSession 去执行 commit 操作(执行插入、更新、删除),清空 SqlSession 中的一级缓存,这样做的目的为了让缓存中存储的是最新的信息,避免脏读。

  4. 第二次发起查询用户 id 为 1 的用户信息,先去找缓存中是否有 id 为 1 的用户信息,缓存中有,直接从缓存中获取用户信息。

2.2 Mybatis 二级缓存

二级缓存是 mapper 映射级别的缓存,多个 SqlSession 去操作同一个 Mapper 映射的 sql 语句,多个SqlSession 可以共用二级缓存,二级缓存是跨 SqlSession 的。

二级缓存结构图

在这里插入图片描述

  1. 首先开启 mybatis 的二级缓存
  2. sqlSession1 去查询用户信息,查询到用户信息会将查询数据存储到二级缓存中。
  3. 如果 SqlSession3 去执行相同 mapper 映射下 sql,执行 commit 提交,将会清空该 mapper 映射下的二级缓存区域的数据。
  4. sqlSession2 去查询与 sqlSession1 相同的用户信息,首先会去缓存中找是否存在数据,如果存在直接从缓存中取出数据。

二级缓存的开启与关闭

在 SqlMapConfig.xml 文件开启二级缓存

<settings>
    <!-- 开启二级缓存的支持 -->
    <setting name="cacheEnabled" value="true"/>
</settings>

因为 cacheEnabled 的取值默认就为 true,所以这一步可以省略不配置。

配置相关的 Mapper 映射文件

<cache>标签表示当前这个 mapper 映射将使用二级缓存,区分的标准就看 mapper 的 namespace 值。

<?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="org.woster.dao.IUserDao">
    <!-- 开启二级缓存的支持 -->
    <cache></cache>
</mapper>

配置 statement 上面的 useCache 属性

<!-- 根据 id 查询 -->
<select id="findById" resultType="user" parameterType="int" useCache="true">
    select * from user where id = #{uid}
</select>

将 UserDao.xml 映射文件中的<select>标签中设置 useCache=”true”代表当前这个 statement 要使用二级缓存,如果不使用二级缓存可以设置为 false。

针对每次查询都需要最新的数据 sql,要设置成 useCache=false,禁用二级缓存。

二级缓存注意事项

当我们在使用二级缓存时,所缓存的类一定要实现 java.io.Serializable 接口,这种就可以使用序列化方式来保存对象。

public class User implements Serializable {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
}

3. Mybatis 注解开发

3.1 mybatis 的常用注解说明

  • @Insert:实现新增
  • @Update:实现更新
  • @Delete:实现删除
  • @Select:实现查询
  • @Result:实现结果集封装
  • @Results:可以与@Result 一起使用,封装多个结果集
  • @ResultMap:实现引用@Results 定义的封装
  • @One:实现一对一结果集封装
  • @Many:实现一对多结果集封装
  • @SelectProvider: 实现动态 SQL 映射
  • @CacheNamespace:实现注解二级缓存的使用

3.2 使用 Mybatis 注解实现基本 CRUD

编写实体类

public class User implements Serializable {
    private Integer userId;
    private String userName;
    private Date userBirthday;
    private String userSex;
    private String userAddress;
    省略getter及setter
}
public interface IUserDao {
    //查询所有用户
    @Select("select * from user")
    @Results(id="userMap",
             value= {
                 @Result(id=true,column="id",property="userId"),
                 @Result(column="username",property="userName"),
                 @Result(column="sex",property="userSex"),
                 @Result(column="address",property="userAddress"),
                 @Result(column="birthday",property="userBirthday")
             })
    List<User> findAll();
    //根据 id 查询一个用户
    @Select("select * from user where id = #{uid} ")
    @ResultMap("userMap")
    User findById(Integer userId);
    //保存操作
    @Insert("insert into user(username,sex,birthday,address)values( {username},#{sex},#{birthday},#{address})")
    @SelectKey(keyColumn="id",keyProperty="id",resultType=Integer.class,before=false, statement = { "select last_insert_id()" })
    int saveUser(User user);
    //更新操作
    @Update("update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id =#{id} ")
    int updateUser(User user);
    //删除用户
    @Delete("delete from user where id = #{uid} ")
    int deleteUser(Integer userId);
    //查询使用聚合函数
    @Select("select count(*) from user ")
    int findTotal();
    //模糊查询
    @Select("select * from user where username like #{username} ")
    List<User> findByName(String name);
}

编写 SqlMapConfig 配置文件

<?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 文件的位置 -->
    <properties resource="jdbcConfig.properties"></properties>
    <!-- 配置别名的注册 -->
    <typeAliases>
        <package name="org.woster.domain"/>
    </typeAliases>
    <!-- 配置环境 -->
    <environments default="mysql">
        <!-- 配置 mysql 的环境 -->
        <environment id="mysql">
            <!-- 配置事务的类型是 JDBC -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源 -->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 配置映射信息 -->
    <mappers>
        <!-- 配置 dao 接口的位置,它有两种方式
            第一种:使用 mapper 标签配置 class 属性
            第二种:使用 package 标签,直接指定 dao 接口所在的包
        -->
        <package name="org.woster.dao"/>
    </mappers>
</configuration>

3.3 使用注解实现复杂关系映射开发

实现复杂关系映射之前我们可以在映射文件中通过配置来实现,在使用注解开发时我们需要借助@Results 注解,@Result 注解,@One 注解,@Many 注解。


@Results 注解代替的是标签<resultMap>该注解中可以使用单个@Result 注解,也可以使用@Result 集合

@Results{@Result(),@Result()})或@Results@Result())

@Resutl 注解代替了 标签和标签

@Result 中的属性:

  • id 是否是主键字段
  • column 数据库的列名
  • property 需要装配的属性名
  • one 需要使用的@One 注解(@Result(one=@One)()))
  • many 需要使用的@Many 注解(@Result(many=@many)())

@One 注解(一对一)代替了标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。

@One 注解的属性:

  • select 指定用来多表查询的 sqlmapper
  • fetchType 会覆盖全局的配置参数 lazyLoadingEnabled。。

@Many 注解(多对一)代替了标签,是是多表查询的关键,在注解中用来指定子查询返回对象集合。

聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,属性的 javaType(一般为 ArrayList)但是注解中可以不定义

使用注解实现一对一复杂关系映射及延迟加载

public class User implements Serializable {
    private Integer userId;
    private String userName;
    private Date userBirthday;
    private String userSex;
    private String userAddress;
    //省略setter及getter
}
public class Account implements Serializable {
    private Integer id;
    private Integer uid;
    private Double money;
    //多对一关系映射:从表方应该包含一个主表方的对象引用
    private User user;
    //省略setter及getter
}

账户的持久层接口并使用注解配置

public interface IAccountDao {
    /**
    * 查询所有账户,采用延迟加载的方式查询账户的所属用户
    * @return
    */
    @Select("select * from account")
    @Results(id="accountMap",
             value= {
                 @Result(id=true,column="id",property="id"),
                 @Result(column="uid",property="uid"),
                 @Result(column="money",property="money"),
                 @Result(column="uid",
                         property="user",
                         one=@One(select="org.woster.dao.IUserDao.findById",
                                  fetchType=FetchType.LAZY)
                        )
             })
    List<Account> findAll();
}

用户的持久层接口并使用注解配置

public interface IUserDao {
    /**
    * 查询所有用户
    * @return
    */
    @Select("select * from user")
    @Results(id="userMap",
             value= {
                 @Result(id=true,column="id",property="userId"),
                 @Result(column="username",property="userName"),
                 @Result(column="sex",property="userSex"),
                 @Result(column="address",property="userAddress"),
                 @Result(column="birthday",property="userBirthday")
             })
    List<User> findAll();
    /**
    * 根据 id 查询一个用户
    * @param userId
    * @return
    */
    @Select("select * from user where id = #{uid} ")
    @ResultMap("userMap")
    User findById(Integer userId);
}

使用注解实现一对多复杂关系映射

User 实体类加入 List<Account>

public class User implements Serializable {
    private Integer userId;
    private String userName;
    private Date userBirthday;
    private String userSex;
    private String userAddress;
    //一对多关系映射:主表方法应该包含一个从表方的集合引用
    private List<Account> accounts;
    //省略setter及getter
}

用户的持久层接口并使用注解配置

public interface IUserDao {
    /**
* 查询所有用户
* @return
*/
    @Select("select * from user")
    @Results(id="userMap",
             value= {
                 @Result(id=true,column="id",property="userId"),
                 @Result(column="username",property="userName"),
                 @Result(column="sex",property="userSex"),
                 @Result(column="address",property="userAddress"),
                 @Result(column="birthday",property="userBirthday"),
                 @Result(column="id",property="accounts",
                         many=@Many(
                             select="org.woster.dao.IAccountDao.findByUid",
                             fetchType=FetchType.LAZY )
                        )
             })
    List<User> findAll();
}

@Many:相当<collection>的配置

select 属性:代表将要执行的 sql 语句

fetchType 属性:代表加载方式,一般如果要延迟加载都设置为 LAZY 的值


账户的持久层接口并使用注解配置

public interface IAccountDao {
    /**
    * 根据用户 id 查询用户下的所有账户
    * @param userId
    * @return
    */
    @Select("select * from account where uid = #{uid} ")
    List<Account> findByUid(Integer userId);
}

3.4 mybatis 基于注解的二级缓存

在 SqlMapConfig 中开启二级缓存支持

<!-- 配置二级缓存 -->
<settings>
    <!-- 开启二级缓存的支持 -->
    <setting name="cacheEnabled" value="true"/>
</settings>

在持久层接口中使用注解配置二级缓存

@CacheNamespace(blocking=true)//mybatis 基于注解方式实现配置二级缓存
public interface IUserDao {
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值