mapper文件:
<mapper namespace="com.dao">
<resultMap type="Student" id="StudentResultMap">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<association property="classInfo" column="classInfo_id" javaType="ClassInfo">
<id property="id" column="id" />
<result property="name" column="name" />
</association>
</resultMap>
<select id="select1" resultMap="StudentResultMap">
select s.*,c.* from student s left join classInfo c on s.classInfo_id = c.id
</select>
</mapper>
JTest测试:
@Test
public void select(){
SqlSession session = factory.openSession();
List<Student> stuList = session.selectList("com.dao.select1");
for (Student stu : stuList) {
System.out.println(stu.getId()+":"+stu.getName()+","+stu.getAge());
System.out.println(stu.getClassInfo().getId()+":"+stu.getClassInfo().getName());
}
session.close();
}
输出结果:
2013-11-16 20:34:07,656 [main] DEBUG [com.dao.select1] - ooo Using Connection [com.mysql.jdbc.Connection@1dee400]
2013-11-16 20:34:07,656 [main] DEBUG [com.dao.select1] - ==> Preparing: select s.*,c.* from student s left join classInfo c on s.classInfo_id = c.id
2013-11-16 20:34:07,718 [main] DEBUG [com.dao.select1] - ==> Parameters:
2013-11-16 20:34:07,796 [main] DEBUG [com.dao.select1] - <== Total: 1
5:Wang,20
5:Wang
别名都确定没错,config配置文件也没问题,
输出stu.getClassInfo().getId()+":"+stu.getClassInfo().getName());
竟然也是学生编号和姓名,应该是班级编号和姓名,不知道到底是哪里的问题?
数据库也没问题,字段和属性都没写错,约束也没问题。