如果不设置懒加载,数据能读出来,
设置懒加载后,报错
Cause: org.apache.ibatis.executor.ExecutorException: Error creating lazy proxy. Cause: java.lang.NullPointerException
mybatis版本3.46
配置文件 Order
<?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">
<!-- ============= 懒加载 ==================-->
<resultMap id="orderLazyloadingRslMap" type="orders">
<id column="id" property="id"/>
<id column="user_id" property="user_id"></id>
<result column="note" property="note"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<!--配置查询-->
<association property="user" javaType="com.gyf.model.User" select="com.gyf.mapper.UserMapper.findUserById"
column="user_id"/>
</resultMap>
<select id="findOrderAndUserByLazyloading" resultMap="orderLazyloadingRslMap">
SELECT * FROM orders
</select>
配置文件 User
<?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">
<select id="findUserById" parameterType="int" resultType="user">
SELECT * FROM user WHERE id = #{id}
</select>
sqlMapConfig:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<properties resource="db.properties"/>
<!--配置允许懒加载-->
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
<!--配置别名-->
<typeAliases>
<!--指定包名,别名就是类名,第一个小写 User 别名就是user-->
<package name="com.gyf.model"></package>
<package name="com.gyf.vo"></package>
</typeAliases>
<!-- 配置mybatis的环境信息 -->
<environments default="development">
<environment id="development">
<!-- 配置JDBC事务控制,由mybatis进行管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源,采用dbcp连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${driverClass}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--mybatis加载映射文件-->
<mappers>
<package name="com.gyf.mapper"></package>
</mappers>
模型:
public class Orders {
private Integer id;
private Integer user_id;
private String note;//备注
private String number;
private Date createtime;//写意的创建时间
private User user;//定单所属的用户
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
....get 和 set 方法
}
public class User implements Serializable {
private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
....get 和 set 方法
}
测试
public class Demo01 {
SqlSession session;
@Before
public void before() throws IOException {
System.out.println("before.....获取session");
// a)读取配置文件;
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
//b)通过SqlSessionFactoryBuilder创建SqlSessionFactory会话工厂。
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
session = sessionFactory.openSession();
}
@After
public void after(){
session.close();
}
/**
* 懒加载
* @throws IOException
*/
@Test
public void test10() throws IOException {
OrderMapper mapper = session.getMapper(OrderMapper.class);
List<Orders> list = mapper.findOrderAndUserByLazyloading();
for (Orders order : list){
System.out.println("订单信息:");
System.out.println(order);
System.out.println("订单所属的客户:");
System.out.println(order.getUser());
}
}
}