packagecom.itheima.one_to_one;importcom.itheima.bean.Person;importorg.apache.ibatis.annotations.Select;publicinterfacePersonMapper{// 根据id查询@Select("SELECT * FROM person WHERE id = #{id}")publicabstractPersonselectById(Integer id);}
packagecom.itheima.one_to_many;importcom.itheima.bean.Classes;importorg.apache.ibatis.annotations.Many;importorg.apache.ibatis.annotations.Result;importorg.apache.ibatis.annotations.Results;importorg.apache.ibatis.annotations.Select;importjava.util.List;publicinterfaceClassesMapper{@Select("SELECT * FROM classes")@Results({@Result(column ="id",property ="id"),@Result(column ="name",property ="name"),@Result(
property ="students",
javaType =List.class,
column ="id",
many =@Many(select ="com.itheima.one_to_many.StudentMapper.selectByCid"))})// 查询全部publicabstractList<Classes>selectAll();}
StudentMapper
packagecom.itheima.one_to_many;importcom.itheima.bean.Student;importorg.apache.ibatis.annotations.Select;importjava.util.List;publicinterfaceStudentMapper{// 根据cid查询student表@Select("SELECT * FROM student WHERE cid = #{cid}")publicabstractList<Student>selectByCid(Integer cid);}
packagecom.itheima.many_to_many;importcom.itheima.bean.Course;importorg.apache.ibatis.annotations.Select;importjava.util.List;publicinterfaceCourseMapper{// 根据学生Id查询所选课程@Select("SELECT c.id,c.name FROM stu_cr sc,course c WHERE sc.cid = c.id AND sc.sid = #{id}")publicabstractList<Course>selectBySid(Integer id);}