Mybatis-延迟加载

无情的你无情的雨 2020-03-08

立即加载:只要一调用就立即发起加载。举例:一个用户有100个账户,查询账户时有必要把用户信息也显示出来。

延迟加载机制是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作;

可以简单理解为,只有在使用的时候,才会发出sql语句进行查询;延迟加载的有效期是在session打开的情况下,当session关闭后,会报异常。

当调用load方法加载对象时,返回代理对象,等到真正用到对象的内容时才发出sql语句

package bean;

public class student {
    private Integer s_id;//id
    private Integer c_id;//关联id
    private String s_name;//name
    private classes classes;//班级对象
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Integer getCid() {
        return cid;
    }
    public void setCid(Integer cid) {
        this.cid = cid;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public classes getClasses() {
        return classes;
    }
    public void setClasses(classes classes) {
        this.classes = classes;
    }
}
package bean;

import java.util.List;

public class classes {
    private Integer c_id;//班级id
    private String c_name;//班级name
    private List<student> student;//学生集合对象
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getCname() {
        return cname;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }
    public List<student> getStudent() {
        return student;
    }
    public void setStudent(List<student> student) {
        this.student = student;
    }
}
<!-- classes接口xml--> 
<!-- resultMap手动定义 -->
    <resultMap type="classes" id="claMap">
        <!-- id标签,主键 -->
        <id column="id" property="id" />
        <!-- result标签,普通列 -->
        <result column="cname" property="cname" />
        <collection property="student" ofType="student"
            select="dao.studao.selbyid" column="id">
        </collection>
    </resultMap>

    <!-- select标签用于查询 -->
    <select id="selallC" resultMap="claMap">
        select * from classes
    </select>

    <!-- student接口xml-->
        <select id="selbyid" resultType="student">
        select * from student where
        cid=#{cid}
    </select>

全局配置xml

<settings>
        <!-- 开启延迟加载支持 -->
        <setting name="lazyLoadingEnabled" value="true" />
        <!-- 将积极加载改为消极加载 按需加载 -->
        <setting name="aggressiveLazyLoading" value="false" />
    </settings>

测试类

package Test;

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

import javax.xml.ws.Action;
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 bean.classes;
import bean.student;
import dao.cladao;
import dao.studao;



public class MybatisyTest {    
    InputStream inputstream;
    SqlSessionFactory factory;
    SqlSession session;
    cladao cdao;
    studao sdao;
    @Before
    public  void init() throws IOException{
        //读取配置文件
        inputstream=Resources.getResourceAsStream("mybatis-config.xml");
        factory =new SqlSessionFactoryBuilder().build(inputstream);
        session =factory.openSession();
        cdao=session.getMapper(cladao.class);

    }
    @Test
    public void selallcla() throws IOException{
        List<classes> cla =cdao.selallC();
        for(classes c:cla) {
            System.out.println("班级id:"+c.getId()+",班级名称:"+c.getCname());
            if(c.getId()==2) {
                List<student> stu=c.getStudent();
                for(student s:stu) {
                    System.out.println("学生id:"+s.getId()+",学生姓名:"+s.getSname());
                }
            }
        }
    }
    @After
    public void destory()  throws IOException {
        //关闭
        session.commit();
        session.close();
        inputstream.close();
    }

}

执行@Test注解

只查询班级表中的所有数据,当你点击想查询的班级(传入id值),再执行sql语句查询学生表符合条件的学生信息

Mybatis-延迟加载

相关推荐