mybatis的多对一
多个学生对一个老师
public interface StudentMapper {
public List<Student> getStudent();
public List<Student> getStudent2();
}
学生接口
<!--第二种-->
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid, s.name sname,t.name tname
from student s ,teacher t
where s.tid = t.id;
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
学生的Mapper.xml配置
注意事项
@Test
public void StudentTeacher(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudent();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
@Test
public void StudentTeacher2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudent2();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
}
测试代码