一对一
public interface PersonMapper { //依据id查问 @Select("SELECT * FROM person WHERE id=#{id}") public abstract Person selectById(Integer id);}
public interface CardMapper { //查问全副 @Select("SELECT * FROM card") @Results({ @Result(column = "id",property = "id"), @Result(column = "number",property = "number"), @Result( property = "p", // 被蕴含对象的变量名 javaType = Person.class, // 被蕴含对象的理论数据类型 column = "pid", // 依据查问出的card表中的pid字段来查问person表 /* one、@One 一对一固定写法 select属性:指定调用哪个接口中的哪个办法 */ one = @One(select = "com.itheima.one_to_one.PersonMapper.selectById") ) }) public abstract List<Card> selectAll();}
@Results:封装映射关系的父注解。
Result[] value():定义了 Result 数组
@Result:封装映射关系的子注解。
column 属性:查问出的表中字段名称property 属性:实体对象中的属性名称javaType 属性:被蕴含对象的数据类型one 属性:一对一查问固定属性
@One:一对一查问的注解。
select 属性:指定调用某个接口中的办法
一对多
public interface StudentMapper { //依据cid查问student表 @Select("SELECT * FROM student WHERE cid=#{cid}") public abstract List<Student> selectByCid(Integer cid);}
public interface ClassesMapper { //查问全副 @Select("SELECT * FROM classes") @Results({ @Result(column = "id",property = "id"), @Result(column = "name",property = "name"), @Result( property = "students", // 被蕴含对象的变量名 javaType = List.class, // 被蕴含对象的理论数据类型 column = "id", // 依据查问出的classes表的id字段来查问student表 /* many、@Many 一对多查问的固定写法 select属性:指定调用哪个接口中的哪个查询方法 */ many = @Many(select = "com.itheima.one_to_many.StudentMapper.selectByCid") ) }) public abstract List<Classes> selectAll();}
@Results:封装映射关系的父注解。
Result[] value():定义了 Result 数组
@Result:封装映射关系的子注解。
column 属性:查问出的表中字段名称property 属性:实体对象中的属性名称javaType 属性:被蕴含对象的数据类型many 属性:一对多查问固定属性
@Many:一对多查问的注解。
select 属性:指定调用某个接口中的办法
多对多
public interface CourseMapper { //依据学生id查问所选课程 @Select("SELECT c.id,c.name FROM stu_cr sc,course c WHERE sc.cid=c.id AND sc.sid=#{id}") public abstract List<Course> selectBySid(Integer id);}
public interface StudentMapper { //查问全副 @Select("SELECT DISTINCT s.id,s.name,s.age FROM student s,stu_cr sc WHERE sc.sid=s.id") @Results({ @Result(column = "id",property = "id"), @Result(column = "name",property = "name"), @Result(column = "age",property = "age"), @Result( property = "courses", // 被蕴含对象的变量名 javaType = List.class, // 被蕴含对象的理论数据类型 column = "id", // 依据查问出student表的id来作为关联条件,去查问两头表和课程表 /* many、@Many 一对多查问的固定写法 select属性:指定调用哪个接口中的哪个查询方法 */ many = @Many(select = "com.itheima.many_to_many.CourseMapper.selectBySid") ) }) public abstract List<Student> selectAll();}
@Results:封装映射关系的父注解。
Result[] value():定义了 Result 数组
@Result:封装映射关系的子注解。
column 属性:查问出的表中字段名称property 属性:实体对象中的属性名称javaType 属性:被蕴含对象的数据类型many 属性:一对多查问固定属性
@Many:一对多查问的注解。
select 属性:指定调用某个接口中的办法