欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mybatis中一对多查询实现延迟加载

程序员文章站 2022-03-20 10:08:48
...

基于Mybatis中一对一查询实现延迟加载的基础上进行修改

AccountDao接口:

package com.qublog.dao;

import com.qublog.domain.Account;

import java.util.List;

public interface AccountDao {
    //查询所有账户,同时还要获取到当前账户的所属用户信息
    List<Account> findAll();

    //根据用户id查询账户信息
    List<Account> findAccountByUid(Integer uid);
}

UserDao接口:

package com.qublog.dao;


import com.qublog.domain.User;

import java.util.List;

//用户的持久层接口
public interface UserDao {

    //查询所有用户,同时获取到用户下所有账户的信息
    List<User> findAll();

    User findById();
}

AccountDao.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="com.qublog.dao.AccountDao">
    <!-- 定义封装account和user的resultMap -->
    <resultMap id="accountUserMap" type="account">
        <id property="id" column="id"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <!-- 一对一的关系映射,配置封装user的内容
             select属性指定的内容:查询用户的唯一标识
             column属性指定的内容:用户根据id查询时,所需要的参数的值
        -->
        <association property="user" column="uid" javaType="user" select="com.qublog.dao.UserDao.findById"></association>
    </resultMap>

    <!-- 配置查询所有 -->
    <select id="findAll" resultMap="accountUserMap">
        select * from account;
    </select>

    <select id="findAccountByUid" parameterType="Integer" resultType="account">
        select * from account where uid=#{uid}
    </select>
</mapper>

UserDao.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="com.qublog.dao.UserDao">
    <!-- 定义user的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>
        <!-- 配置user对象中accounts集合的映射 -->
        <collection property="accounts" ofType="account" select="com.qublog.dao.AccountDao.findAccountByUid" column="id"></collection>
    </resultMap>

    <!-- 配置查询所有 -->
    <select id="findAll" resultMap="userAccountMap">
        select * from user;
    </select>

    <!-- 根据id查询用户 -->
    <select id="findById" parameterType="Integer" resultType="user">
        select * from user where id=#{id};
    </select>
</mapper>

UserTest类:

package com.qublog.test;

import com.qublog.dao.UserDao;
import com.qublog.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class UserTest {
    private InputStream in = null;
    private SqlSession sqlSession = null;
    UserDao userDao = null;

    @Before //用于在测试方法执行之前执行
    public void init() throws Exception {
        //读取配置文件,生成字节输入流
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //获取SqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        //获取SqlSession对象
        sqlSession = factory.openSession();
        //获取dao代理对象
        userDao = sqlSession.getMapper(UserDao.class);
    }

    @After //用于在测试方法执行之后执行
    public void destroy() throws Exception {
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
        in.close();
    }

    @Test
    public void testFindAll() throws Exception {
        //执行查询所有方法
        List<User> users = userDao.findAll();
        for (User user:users) {
            System.out.println(user);
            System.out.println(user.getAccounts() );
        }
    }

}

输出结果:
Mybatis中一对多查询实现延迟加载
修改后:

    @Test
    public void testFindAll() throws Exception {
        //执行查询所有方法
        List<User> users = userDao.findAll();
//        for (User user:users) {
//            System.out.println(user);
//            System.out.println(user.getAccounts() );
//        }
   


可以看出实现了延迟加载。