标签:head cell except com loader 查询 resultmap config soc
mybatis(3.4.1之后版本默认lazyLoadingEnabled为false,之前为true,哪怕之前为true,能 起到延迟加载的作用 是两码事)
实体:public class Emp{
/****/
private Integer empno;
/****/
private String empName;
/****/
private Integer deptno;
private Dept dept;
mapper.xml:
<resultMap id="fenEmpmappper" type="Emp">
<id column="empno" property="empno"></id>
<result column="empName" property="empName"></result>
<association property="dept" javaType="Dept" select="getmapper" column="deptno"/>
</resultMap>
<select id="fenmanytoone" resultMap="fenEmpmappper">
select * from emp where empno=#{empno}
</select>
mybetis-config.xml配置:
<setting name="lazyLoadingEnabled" value="false"></setting>
测试: public void fenmanytoone() throws Exception {
SqlSession session= UtilMybatis.getsession();
EmpDAO dao= session.getMapper(EmpDAO.class);
Emp emp = dao.fenmanytoone(1);
}
结果:执行两条sql语句
实体同第一种
测试: public void fenmanytoone() throws Exception {
SqlSession session= UtilMybatis.getsession();
EmpDAO dao= session.getMapper(EmpDAO.class);
Emp emp = dao.fenmanytoone(1);
}
mybetis-config.xml配置:
<setting name="lazyLoadingEnabled" value="true"></setting>
结果:执行一条sql语句
主对象属性的时候,加载关联对象(方式SQL语句) lazyLoadingEnabled=true agreesiveLazyLoading=true
mybetis-config.xml 配置:
<setting name="lazyLoadingEnabled" value="true"></setting>
<setting name="aggressiveLazyLoading" value="true"></setting>
测试:
public void fenmanytoone() throws Exception {
SqlSession session= UtilMybatis.getsession();
EmpDAO dao= session.getMapper(EmpDAO.class);
Emp emp = dao.fenmanytoone(1);
System.out.println(emp.getEmpName());
}
结果:注解:只要用到主对象(emp.getEmpName()),关联对象也进行加载
关联对象数据的SQL lazyLoadingEnabled=true agreesiveLazyLoading=false
mybetis-config.xml 配置:
<setting name="lazyLoadingEnabled" value="true"></setting>
<setting name="aggressiveLazyLoading" value="false"></setting>
测试:
public void fenmanytoone() throws Exception {
SqlSession session= UtilMybatis.getsession();
EmpDAO dao= session.getMapper(EmpDAO.class);
Emp emp = dao.fenmanytoone(1);
System.out.println(emp.getEmpName());
}
结果:注解:访问主对象,不进行关联对象的加载,什么时候用到关联对象,什么时候加载
.配置延迟加载
mybatis默认没有开启延迟加载,需要在SqlMapConfig.xml中setting配置。
在mybatis核心配置文件中配置:
lazyLoadingEnabled、aggressiveLazyLoading
在mybetis-config.xml中配置:
|
|
|
|
|
|
|
|
|
|
|
|
标签:head cell except com loader 查询 resultmap config soc
原文地址:https://www.cnblogs.com/Java60-123456/p/10678499.html