开启懒加载配置
config.xml
<settings>
<!-- 配置log4j日志信息 -->
<setting name="logImpl" value="LOG4J"></setting>
<!-- 懒加载 配置 -->
<setting name="lazyLoadingEnabled" value="true"></setting>
<setting name="aggressiveLazyLoading" value="true"></setting>
</settings>
实体bean
BorrowBean
public class BorrowBean {
private int id;
private Date date;
private int num;
// 书籍
private BookBean book;
// 用户
private UserBean user;
// ... get set 方法
}
BooKBean
public class BookBean {
private int id;
private String name;
private String picture;
private String author;
private String press;
private Date time;
private int price;
private int stock;
private String describe;
// ... get set 方法
}
UserBean
public class UserBean {
private int id;
private String name;
private String pwd;
// 一个用户借多本图书
private List<BookBean> booklist;
// ... get set 方法
}
配置 Mapper.xml
BorrowMapper.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.project.dao.IBorrowDao">
<select id="findByBookId" parameterType="int" resultMap="borrowResultMap">
SELECT * FROM t_ub WHERE ub_b_id = #{id};
</select>
<!-- 将查询结果字段,映射成BorrowBean 属性 -->
<resultMap id="borrowResultMap" type="BorrowBean">
<!-- 主键字段 -->
<id property="id" column="ub_id" />
<!-- 其它字段 -->
<result property="date" column="ub_date"/>
<result property="num" column="ub_num"/>
<!--
懒加载 --通过 ub_u_iu 用 select 属性指定的 sql 查询数据库,
将结果封装成UsreBean对象,赋给 user
注意:column指定的字段类型要与指定sql接收参数一样
JavaType要与sql语句返回的实体bean要一样
-->
<association property="user" javaType="UserBean" column="ub_u_id"
select="com.project.UserMapper.findById">
</association>
</resultMap>
</mapper>