首页 > yabo亚搏 > TZ_02MyBatis_lazySqlMapConfig.xml

TZ_02MyBatis_lazySqlMapConfig.xml

2019-09-13

Mybatis的延迟加载又称为懒加载

mybatis在一对多的查询中,例如查询一个用户时需要查询这个用户下的所有账户信息,如果一次性的select * from user u left join account a on u.id=a.UID 会占用大量的内存 这里就可以使用延迟加载。

延迟加载配置:

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.hdh.dao.UserDao">

    
    <resultMap id="userAccountMap" type="user">
        <id property="id" column="id">id>
        <result property="username" column="username">result>
        <result property="birthday" column="birthday">result>
        <result property="address" column="address">result>
        <result property="sex" column="sex">result>
        
        <collection property="accounts" ofType="account"
            select="com.hdh.dao.AccountDao.findAccountByUid" column="id">
        collection>
    resultMap>

    
    <select id="findAll" resultMap="userAccountMap">
        select * from user;
    select>
mapper>

?

UserDao.java

package com.hdh.dao;

import java.util.List;

import com.hdh.domain.User;

/**
 * 用户的持久层接口
 * 
 * @author Administrator
 *
 */
public interface UserDao {
    // 查询所有用户
    List findAll();

}

?

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.hdh.dao.AccountDao">

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

mapper>

AccountDao.java

package com.hdh.dao;

import java.util.List;

import com.hdh.domain.Account;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 */
public interface AccountDao {
    /**
     * 根据用户id查询账户信息
     * @param uid
     * @return
     */
    List findAccountByUid(Integer uid);

}

UserTest.java

package com.hdh.test;

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 com.hdh.dao.UserDao;
import com.hdh.domain.User;

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

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 */
public class UserTest {

    private InputStream in;
    private SqlSession sqlSession;
    private UserDao userDao;

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

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

    /**
     * 测试查询所有
     */
    @Test
    public void testFindAll(){
        List users = userDao.findAll();
        /*for(User user : users){
            System.out.println("-----每个用户的信息------");
            System.out.println(user);
            System.out.println(user.getAccounts());
        }*/
    }



}

?

原文链接:https://www.cnblogs.com/asndxj/p/11308646.html

文章标签: