首先pojo类
dept.java
package com.husky.pojo;
import java.util.List;
public class Dept {
private int id;
private String name;
private List<Emp> emps;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Emp> getEmps() {
return emps;
}
public void setEmp(List<Emp> emps) {
this.emps = emps;
}
@Override
public String toString() {
return "Dept{" +
"id=" + id +
", name='" + name + '\'' +
", emps=" + emps +
'}';
}
}
emp.java
package com.husky.pojo;
public class Emp {
private int id;
private String firstName;
private double salary;
private Emp manager;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Emp getManager() {
return manager;
}
public void setManager(Emp manager) {
this.manager = manager;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", salary=" + salary +
", manager=" + manager +
'}';
}
}
下面是EmpMapper.xml
<!--namespace="emp"-->
<select id="findByDept" parameterType="int" resultType="com.husky.pojo.Emp">
select id,first_name name,salary from s_emp where dept_id = #{id}
</select>
DeptMapper.xml
<!--lazy load-->
<resultMap id="lazyDept" type="com.husky.pojo.Dept">
<id column="id" property="id"/>
<!--
对dept中的emp信息延迟加载
在collection中的
select :表示关联的查询,此处如果的findByDept不是DeptMapper.xml中,则要加另一个mapper的namespace,如emp.findByDept
column:是另外一个查询所需要的参数
-->
<collection property="emps" ofType="com.husky.pojo.Emp"
select="emp.findByDept" column="id"></collection>
</resultMap>
<select id="findDeptById" parameterType="int" resultMap="lazyDept">
select id,name from s_dept where id = #{id}
</select>
当然要开启懒加载要在mybatis 配置文件中加上lazyload的配置
sqlMapConfig.xml
<settings>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="true"/>
<!-- Sets the number of seconds the driver will wait for a response from the database -->
<setting name="defaultStatementTimeout" value="3000"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
<setting name="useGeneratedKeys" value="true"/>
<!--开启懒加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--关闭懒加载的对象可能被任何懒属性全部加载-->
<setting name="aggressiveLazyLoading" value="false"/>
<!--消除equals,hashcode,toString等方法对懒加载的影响-->
<setting name="lazyLoadTriggerMethods" value=""/>
</settings>
测试
package com.husky.test;
import com.husky.pojo.Dept;
import com.husky.pojo.Emp;
import com.husky.pojo.Region;
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.Before;
import java.io.IOException;
import java.io.InputStream;
public class Test {
SqlSessionFactory factory;
@Before
public void before() throws IOException {
String res = "com/husky/sqlMapConfig.xml";
InputStream is = Resources.getResourceAsStream(res);
factory = new SqlSessionFactoryBuilder().build(is);
}
@org.junit.Test
public void lazyTest(){
SqlSession session = factory.openSession();
Dept d = session.selectOne("findDeptById",42);
System.out.println(d);
System.out.println(d.getEmps());
}
}
输出结果
Dept{id=42, name='Operations', emps=null}
[Emp{id=7, firstName='null', salary=1250.0, manager=null}, Emp{id=18, firstName='null', salary=1200.0, manager=null}, Emp{id=19, firstName='null', salary=795.0, manager=null}]