Mybatis框架-第三篇

第一章:Mybatis延迟加载策略

1.1-什么是延迟加载?

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

  • 好处:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速 度要快。
  • 坏处:因为只有当需要用到数据时,才会进行数据库查询,这样在大批量数据查询时,因为查询工作也要消耗 时间,所以可能造成用户等待时间变长,造成用户体验下降。

1.2-需求

  • 需求1:查询所有账户数据,若使用账户数据时(打印、遍历等),再去查询账户所属的用户数据。
  • 需求2:查询所有用户表数据,若使用用户数据时(打印表里),再去查询该用户所拥有的所有账户数据。

1.3-解决方案

先配置 Mybatis 的延迟加载策略

在Mybatis的核心配置文件中(如:SqlMapConfig.xml),设置以下信息

    <!--延迟加载配置-->
    <settings>
        <!--延迟加载的全局开关,true表示开启延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--当启用时,对任意延迟属性的调用会使带有延迟加载属性的对象完整加载;反之,每种属性将会按需加载。-->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

更多配置信息,可参考文档

子配置文件配置

在写具体sql所在的映射配置文件中,可以通过 association、collection 实现延迟加载

association 关联(association)元素处理“有一个”类型的关系

<resultMap>
        <!--延迟加载关联表数据-->
        <!--
            property    关联的属性(实体类属性名)   
            column      关联的属性(数据库表列名)
            javaType    关联的表的实体类型全限定类名
            select      指定关联查询的全限定类名及方法名
        -->
        <association property="user" column="uid" javaType="User" select="cn.lpl666.dao.IUserDao.findOneById"></association>
    </resultMap>

collection 是用于建立一对多中集合属性的对应关系

<resultMap >
        <!--
            ofType          用于指定集合元素的数据类型
            select          指定关联查询的全限定类名及方法名如:cn.lpl666.dao.IAccountDao.方法名
            column          关联的属性(数据库表列名)
            property        关联的属性(实体类属性名)
            -->
        <collection property="accountList" ofType="Account" column="id" select="cn.lpl666.dao.IAccountDao.findAllByUid">
        </collection>
</resultMap>

1.4-代码实现

1.4.1-数据库脚本

参照:数据库脚本

1.4.2-实体类

User类

public class User implements Serializable {
  private int id;
  private String username;
  private String birthday;
  private String sex;
  private String address;
  private List<Account> accountList;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getBirthday() {
    return birthday;
  }

  public void setBirthday(String birthday) {
    this.birthday = birthday;
  }

  public String getSex() {
    return sex;
  }

  public void setSex(String sex) {
    this.sex = sex;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  @Override
  public String toString() {
    return "User{" +
            "id=" + id +
            ", username='" + username + '\'' +
            ", birthday=" + birthday +
            ", sex='" + sex + '\'' +
            ", address='" + address + '\'' +
            '}';
  }

  public List<Account> getAccountList() {
    return accountList;
  }

  public void setAccountList(List<Account> accountList) {
    this.accountList = accountList;
  }
}

Account类

public class Account implements Serializable {
  private int id;
  private int uid;
  private double money;
  private User user;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public int getUid() {
    return uid;
  }

  public void setUid(int uid) {
    this.uid = uid;
  }

  public double getMoney() {
    return money;
  }

  public void setMoney(double money) {
    this.money = money;
  }

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }

  @Override
  public String toString() {
    return "Account{" +
            "id=" + id +
            ", uid=" + uid +
            ", money=" + money +
            ", user=" +
            '}';
  }
}

1.4.3-Dao持久层接口

IUserDao接口

public interface IUserDao {
  /**
   * 根据id查询用户
   * @param id
   * @return
   */
  User findOneById(int id);
  /**
   * 查询所有用户信息(包含用户所拥有的账户列表)
   * @return
   */
  List<User> findAll();
}

IAccount接口

public interface IAccountDao {
  /**
   * 查询所有账户,同时获取账户的所属用户名称以及它的地址信息
   * @return
   */
  List<Account> findAll();

  /**
   * 查询指定用户的所有账户信息
   * @param uid
   * @return
   */
  List<Account> findAllByUid(int uid);
}

1.4.4-Mybatis核心配置文件

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>
    <!--resources,指定外联数据库配置文件-->
    <properties resource="jdbc.properties">
        <!--<property name="driver" value="com.mysql.jdbc.Driver"/>-->
        <!--<property name="url" value="jdbc:mysql://localhost:3306/db7"/>-->
        <!--<property name="username" value="root"/>-->
        <!--<property name="password" value="root"/>-->
    </properties>
    <!--【延迟加载配置】-->
    <settings>
        <!--延迟加载的全局开关,true表示开启延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--当启用时,对任意延迟属性的调用会使带有延迟加载属性的对象完整加载;反之,每种属性将会按需加载。-->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    <!--配置实体类映射,后期在dao配置文件中,可以不用写全限名称,而直接使用实体类名-->
    <typeAliases>
        <!--<typeAlias type="cn.lpl666.domain.User" alias="user"></typeAlias>-->
        <package name="cn.lpl666.domain"></package>
    </typeAliases>
    <!--配置mybatis环境-->
    <environments default="mysql">
        <!--配置mysql环境-->
        <environment id="mysql">
            <!--配置事务类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置数据库连接信息,连接池(数据源)-->
            <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>

    <!--告诉mybatis映射配置的位置-->
    <mappers>
        <!--IUserDao-->
        <!--<mapper resource="cn/lpl666/dao/IUserDao.xml"/>-->
        <!--可以映射该位置下的所有dao.xml配置-->
        <package name="cn.lpl666.dao"></package>
    </mappers>
</configuration>

1.4.5-映射配置文件

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="cn.lpl666.dao.IUserDao">
    <resultMap id="UserMap" type="User">
        <id property="id" column="id"></id>
        <result property="sex" column="sex"></result>
        <result property="username" column="username"></result>
        <result property="address" column="address"></result>
        <result property="birthday" column="birthday"></result>
        <result property="address" column="address"></result>
        <!--延迟加载关联表数据-->
        <collection property="accountList" ofType="Account" column="id" select="cn.lpl666.dao.IAccountDao.findAllByUid">
        </collection>
    </resultMap>
    <!--查询所有用户-->
    <select id="findAll" resultMap="UserMap">
        select * from user;
    </select>
    <!--根据id查询用户-->
    <select id="findOneById" parameterType="int" resultType="User">
        select * from user where id=#{id}
    </select>
</mapper>

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="cn.lpl666.dao.IAccountDao">
    <!-- 建立对应关系 -->
    <resultMap id="AccountMap" 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="cn.lpl666.dao.IUserDao.findOneById"></association>
    </resultMap>
    <!--查询所有账户信息-->
    <select id="findAll" resultMap="AccountMap">
       select * from account
    </select>
    <!--查询指定用户的账户信息(根据uid查询)-->
    <select id="findAllByUid" resultType="Account" parameterType="int">
        select * from account where uid=#{uid}
    </select>
</mapper>

1.4.6-测试类

UserTest

public class UserTest {
  private static InputStream is;
  private static SqlSession sqlSession;
  private static IUserDao dao;
  @Before
  public void init(){
    // 读取配置文件
    try {
      is = Resources.getResourceAsStream("SqlMapConfig.xml");
      // 创建工厂构建者对象
      SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
      // 创建SqlSessionFactory工厂对象
      SqlSessionFactory sqlSessionFactory = builder.build(is);
      // 创建SqlSession对象
      sqlSession = sqlSessionFactory.openSession();
      // 创建dao代理对象
      dao = sqlSession.getMapper(IUserDao.class);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  @After
  public  void destroy() throws IOException {
    sqlSession.commit();
    is.close();
    sqlSession.close();
  }
  @Test
  public void getOne(){
    User user = dao.findOneById(1);
    System.out.println(user);
  }

  /**
   * 查询
   */
  @Test
  public void getAll() throws IOException {
    List<User> all = dao.findAll();
    // System.out.println(all);
    // 若使用all时,则会再去查询关联的账户表数据,可通过log4j日志跟踪观察

  }
}

AccountTest

public class AccountTest {
  private static InputStream is;
  private static SqlSession sqlSession;
  private static IAccountDao dao;
  @Before
  public void init(){
    // 读取配置文件
    try {
      is = Resources.getResourceAsStream("SqlMapConfig.xml");
      // 创建工厂构建者对象
      SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
      // 创建SqlSessionFactory工厂对象
      SqlSessionFactory sqlSessionFactory = builder.build(is);
      // 创建SqlSession对象
      sqlSession = sqlSessionFactory.openSession();
      // 创建dao代理对象
      dao = sqlSession.getMapper(IAccountDao.class);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  @After
  public  void destroy() throws IOException {
    sqlSession.commit();
    is.close();
    sqlSession.close();
  }

  /**
   * 查询所有账户
   */
  @Test
  public void getAll() throws IOException {
    List<Account> all = dao.findAll();
    System.out.println(all);
   // 若使用all时,则会再去查询关联的账户表数据,可通过log4j日志跟踪观察
  }
}

第二章:Mybatis缓存

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

2.1-Mybatis一级缓存

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

2.1.1-验证一级缓存的存在

持久层Dao接口

public interface IUserDao {
  /**
   * 根据id查询用户
   * @param id
   * @return
   */
  User findOneById(int id);
}

持久层映射文件

<?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="cn.lpl666.dao.IUserDao">
    <!--根据id查询用户-->
    <select id="findOneById" parameterType="int" resultType="User">
        select * from user where id=#{id}
    </select>
</mapper>

测试

public class UserTest {
  private static InputStream is;
  private SqlSessionFactory sqlSessionFactory;
  private static SqlSession sqlSession;
  private static IUserDao dao;
  @Before
  public void init(){
    // 读取配置文件
    try {
      is = Resources.getResourceAsStream("SqlMapConfig.xml");
      // 创建工厂构建者对象
      SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
      // 创建SqlSessionFactory工厂对象
      sqlSessionFactory = builder.build(is);
      // 创建SqlSession对象
      sqlSession = sqlSessionFactory.openSession();
      // 创建dao代理对象
      dao = sqlSession.getMapper(IUserDao.class);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  @After
  public  void destroy() throws IOException {
    sqlSession.commit();
    is.close();
    sqlSession.close();
  }
  @Test
  public void getOne(){
    User user1 = dao.findOneById(1);

    User user2 = dao.findOneById(1);

    System.out.println(user1==user2); // 结果:true
  }

}

执行结果:

虽然在上面的代码中我们查询了两次,但最后只执行了一次数据库操作,这就是 Mybatis 提 供给我们的一级缓存在起作用了。因为一级缓存的存在,导致第二次查询 id 为1 的记录时,并没有发出 sql 语句 从数据库中查询数据,而是从一级缓存中查询。

2.1.2-分析一级缓存

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

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

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

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

2.1.3-清空一级缓存

public class UserTest {
  private static InputStream is;
  private SqlSessionFactory sqlSessionFactory;
  private static SqlSession sqlSession;
  private static IUserDao dao;
  @Before
  public void init(){
    // 读取配置文件
    try {
      is = Resources.getResourceAsStream("SqlMapConfig.xml");
      // 创建工厂构建者对象
      SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
      // 创建SqlSessionFactory工厂对象
      sqlSessionFactory = builder.build(is);
      // 创建SqlSession对象
      sqlSession = sqlSessionFactory.openSession();
      // 创建dao代理对象
      dao = sqlSession.getMapper(IUserDao.class);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  @After
  public  void destroy() throws IOException {
    sqlSession.commit();
    is.close();
    sqlSession.close();
  }
  @Test
  public void getOne(){
    User user1 = dao.findOneById(1);

    //sqlSession.close();
    //sqlSession = sqlSessionFactory.openSession();
    //dao=sqlSession.getMapper(IUserDao.class);
    // 或
    sqlSession.clearCache();

    User user2 = dao.findOneById(1);

    System.out.println(user1==user2);  // 结果为false
  }

}

执行结果:

当执行sqlSession.close()或清空缓存后后,再次获取sqlSession并查询id=41的User对象时,又重新执行了sql 语句,从数据库进行了查询操作

2.2-Mybatis二级缓存

2.2.1-概述

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

首先开启 mybatis 的二级缓存。

sqlSession1 去查询用户信息,查询到用户信息会将查询数据存储到二级缓存中。

如果 SqlSession3 去执行相同 mapper 映射下 sql,执行 commit 提交,将会清空该 mapper 映射下的二 级缓存区域的数据。

sqlSession2 去查询与 sqlSession1 相同的用户信息,首先会去缓存中找是否存在数据,如果存在直接从 缓存中取出数据。

2.2.2-二级缓存的开启和关闭

第一步:在 SqlMapConfig.xml 文件开启二级缓存

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

因为 cacheEnabled 的取值默认就为 true,所以这一步可以省略不配置。为 true 代表开启二级缓存;为 false 代表不开启二级缓存。

第二步:配置相关的 Mapper 映射文件

<?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">
<!--<cache>标签表示当前这个 mapper 映射将使用二级缓存,区分的标准就看 mapper 的 namespace 值。-->
<mapper namespace="cn.lpl666.dao.IUserDao">
    <!-- 开启二级缓存的支持 -->
    <cache/>
</mapper>

第三步:配置 statement 上面的 useCache 属性

    <select id="findOneById" parameterType="int" resultType="User" useCache="true">
        select * from user where id=#{id}
    </select>

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

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

2.2.3-测试二级缓存

public class UserTest {
  private static InputStream is;
  private SqlSessionFactory sqlSessionFactory;
  private static SqlSession sqlSession;
  private static IUserDao dao;
  @Before
  public void init(){
    // 读取配置文件
    try {
      is = Resources.getResourceAsStream("SqlMapConfig.xml");
      // 创建工厂构建者对象
      SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
      // 创建SqlSessionFactory工厂对象
      sqlSessionFactory = builder.build(is);
      // 创建SqlSession对象
      sqlSession = sqlSessionFactory.openSession();
      // 创建dao代理对象
      dao = sqlSession.getMapper(IUserDao.class);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  @After
  public  void destroy() throws IOException {
    sqlSession.commit();
    is.close();
    sqlSession.close();
  }
  @Test
  public void getOne(){
    User user1 = dao.findOneById(1);

    sqlSession.close();
    sqlSession = sqlSessionFactory.openSession();
    dao=sqlSession.getMapper(IUserDao.class);


    User user2 = dao.findOneById(1);

    System.out.println(user1==user2);  // 结果为false(每次从缓存中读取时,会创建一个新的User对象包装数据),但从数据库中查询了一次
  }

}

执行结果:

猜你喜欢

转载自www.cnblogs.com/lpl666/p/12286994.html