MyBatis立即加载和延迟加载

深藏阁楼爱情的钟 2023-06-26 10:56 63阅读 0赞

查询订单表的时候,可以关联查出订单的详情,也可以需要订单详情的时候,再去订单详情表查询。前者对应的就是立即加载,后者对应的就是懒加载。

延迟加载:在真正使用数据的时候才发起查询,不用的时候不查询关联的数据,延迟加载又叫按需查询(懒加载)

立即加载:不管用不用,只要一调用方法,马上发起查询。

举例前的数据准备
dao层:
IUserDao:

  1. /** * 用户的持久层接口 */
  2. public interface IUserDao {
  3. //同时获取用户的所用账户
  4. List<User> findAll();
  5. User findUserById(Integer id);
  6. }

IAccountDao:

  1. public interface IAccountDao {
  2. List<Account> findAll();
  3. //查询所有账户,包含对应的用户名称和地址-通过写account的子类方式查询
  4. List<AccountUser> findAllAccount();
  5. }

domain层:
Account:

  1. public class Account implements Serializable {
  2. //windows下的mysql 和 实体对应不区分大小写
  3. private Integer id;
  4. private Integer uid;
  5. private Double money;
  6. //一对一
  7. private User user;
  8. //生成getter()和setter()、toString()
  9. }

User :

  1. public class User implements Serializable {
  2. private Integer id;
  3. private String username;
  4. private Date birthday;
  5. private String sex;
  6. private String address;
  7. //一对多
  8. private List<Account> accounts;
  9. //生成getter()和setter()、toString()
  10. }

立即加载

查询用户以及该用户下的所用账户
IUserDao.xml

  1. <mapper namespace="cn.ith.dao.IUserDao">
  2. <resultMap id="userAccountMap" type="user">
  3. <id property="id" column="id"></id>
  4. <result property="username" column="username"></result>
  5. <result property="birthday" column="birthday"></result>
  6. <result property="sex" column="sex"></result>
  7. <result property="address" column="address"></result>
  8. <!--ofType:集合中元素的类型-->
  9. <collection property="accounts" ofType="account">
  10. <id property="id" column="aid"></id>
  11. <result property="uid" column="uid"></result>
  12. <result property="money" column="money"></result>
  13. </collection>
  14. </resultMap>
  15. <select id="findAll" resultMap="userAccountMap">
  16. select a.id as aid, a.uid, a.money, u.* from `user` u left join account a on u.id=a.uid
  17. </select>
  18. </mapper>

测试类:

  1. public class IAccountDaoTest {
  2. private InputStream in;
  3. private SqlSession session;
  4. private IAccountDao accountDao;
  5. @Before
  6. public void init() throws IOException {
  7. //1读取配置文件
  8. in = Resources.getResourceAsStream("SqlMapConfig.xml");
  9. //2创建SqlSessionFactory工厂
  10. SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  11. SqlSessionFactory factory = builder.build(in);
  12. //3使用工厂生产SqlSession对象
  13. session = factory.openSession();
  14. //4使用SqlSession创建Dao接口的代理对象
  15. accountDao = session.getMapper(IAccountDao.class);
  16. }
  17. @After
  18. public void destory() throws IOException {
  19. session.commit();
  20. //6释放资源
  21. session.close();
  22. in.close();
  23. }
  24. @Test
  25. public void findAll(){
  26. List<User> users = userDao.findAll();
  27. for (User user : users) {
  28. System.out.println(user);
  29. List<Account> accounts = user.getAccounts();
  30. for (Account account : accounts) {
  31. System.out.println(account);
  32. }
  33. System.out.println("-----------------------");
  34. }
  35. }
  36. }

结果:
在这里插入图片描述

延迟加载

在MyBatis的主配置文件中,一般是SqlMapConfig.xml文件,加入如下配置:

  1. <configuration>
  2. ......
  3. <settings>
  4. <!--开启mybatis支持延迟加载-->
  5. <setting name="lazyLoadingEnabled" value="true"/>
  6. <setting name="aggressiveLazyLoading" value="false"/>
  7. </settings>
  8. ......
  9. </configuration>

在这里插入图片描述
IAccountDao.xml

  1. <mapper namespace="cn.ith.dao.IAccountDao">
  2. <!--定义封装account和user的resultMap-->
  3. <resultMap id="accountUserMap" type="account">
  4. <id property="id" column="id"></id>
  5. <result property="uid" column="uid"></result>
  6. <result property="money" column="money"></result>
  7. <!--select:查询用户的唯一标识 column:findUserById的id参数-->
  8. <association property="user" column="uid" javaType="user" select="cn.ith.dao.IUserDao.findUserById">
  9. </association>
  10. </resultMap>
  11. <select id="findAll" resultMap="accountUserMap">
  12. SELECT * FROM account
  13. </select>
  14. <select id="findAccountByUid" resultType="account">
  15. select * from account where uid = #{uid}
  16. </select>
  17. </mapper>

IUserDao.xml

  1. <mapper namespace="cn.ith.dao.IUserDao">
  2. <resultMap id="userAccountMap" type="user">
  3. <id property="id" column="id"></id>
  4. <result property="username" column="username"></result>
  5. <result property="birthday" column="birthday"></result>
  6. <result property="sex" column="sex"></result>
  7. <result property="address" column="address"></result>
  8. <!--ofType:集合中元素的类型-->
  9. <collection property="accounts" column="id" ofType="account" select="cn.ith.dao.IAccountDao.findAccountByUid">
  10. </collection>
  11. </resultMap>
  12. <!--配置查询所有-->
  13. <select id="findAll" resultMap="userAccountMap">
  14. select * from user
  15. </select>
  16. <select id="findUserById" parameterType="Integer" resultType="user">
  17. select * from user where id=#{id}
  18. </select>
  19. </mapper>

测试,当只需要用户信息的时候,只执行了select * from user

  1. public class IAccountDaoTest {
  2. private InputStream in;
  3. private SqlSession session;
  4. private IAccountDao accountDao;
  5. @Before
  6. public void init() throws IOException {
  7. //1读取配置文件
  8. in = Resources.getResourceAsStream("SqlMapConfig.xml");
  9. //2创建SqlSessionFactory工厂
  10. SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  11. SqlSessionFactory factory = builder.build(in);
  12. //3使用工厂生产SqlSession对象
  13. session = factory.openSession();
  14. //4使用SqlSession创建Dao接口的代理对象
  15. accountDao = session.getMapper(IAccountDao.class);
  16. }
  17. @After
  18. public void destory() throws IOException {
  19. session.commit();
  20. //6释放资源
  21. session.close();
  22. in.close();
  23. }
  24. @Test
  25. public void findAll(){
  26. List<User> users = userDao.findAll();
  27. }
  28. }

在这里插入图片描述
测试,当需要用户下的账户信息的时候,又去分别执行了select * from account where uid = ?

  1. @Test
  2. public void findAll(){
  3. List<User> users = userDao.findAll();
  4. for (User user : users) {
  5. System.out.println(user);
  6. List<Account> accounts = user.getAccounts();
  7. for (Account account : accounts) {
  8. System.out.println(account);
  9. }
  10. System.out.println("-----------------------");
  11. }
  12. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,63人围观)

还没有评论,来说两句吧...

相关阅读