Could not initialize class com.demo.utils.MybatisUtils
报错原因如图:java名字错误,要和实体类内名字一致
xml 和实体类
运行成功效果
多对一
<!-- 2.获取指定老师下的所有学生及老师的信息-->
<!-- 2-1-按照结果集嵌套查询 -->
<select id="getTeacherAndStudent" resultMap="TeacherStudentMap">
select s.id sid,s.name sname,t.name tname,t.id tid
from mybatis.student s ,mybatis.teacher t
where s.tid = t.id and t.id = #{tid};
</select>
<resultMap id="TeacherStudentMap" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<!-- 单个对象:用association 标签 和 javaType 属性
集合list:用collection 标签 和 ofType属性
泛型内的类型用 ofType
-->
<collection property="studentList" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>