@Results 注解
- **作用:**用于数据库字段名与实体类对应的属性名不一致时,可以使用@Results映射来将其对应起来。
- @Result 属性:
- column为数据库字段名
- porperty为实体类属性名
- jdbcType为数据库字段数据类型
- javaType为Java类的类型
- id为是否为主键
- 举例展示:
@Select("select * from student where sid = 1 ")
@Results({
@Result(property = "sid",column = "sid"),
@Result(property = "name",column = "name"),
@Result(property = "sex",column = "sex"),
@Result(property = "age",column = "age"),
})
Student getStudentInfo();
@One 注解 与 @Many注解
- 注解作用:
- @One:用于实现一对一关系的联合查询,可以将某个查询中的字段当做新的条件,去进行子查询并返回单一对象。
- @Many:用于实现一对多关系的联合查询,也可以将某个查询字段当做子查询的条件,但允许子查询返回多个对象。
- 常用共同属性:
- select:用于指定用于子查询的是哪个查询,其后无需跟属性参数以及类型,包含方法名即可。
- fetchType:有三个值,分别对用立即加载,延迟加载和默认。
- @One注解 举例说明:
@Select("select * from borrow where id = #{id}")
@Results({
@Result(id = true,property = "id",column = "id"),
@Result(property = "student",column = "sid",one = @One(select = "getStudentInfo")),
@Result(property = "book",column = "bid",one = @One(select = "getBookInfo")),
@Result(property = "date",column = "date")
})
Borrow getBorrowInfo(int id);
// student 子查询
@Select("select * from student where sid = #{sid}")
@Results({
@Result(property = "sid",column = "sid"),
@Result(property = "name",column = "name"),
@Result(property = "sex",column = "sex"),
@Result(property = "age",column = "age"),
})
Student getStudentInfo(int sid);
// book 子查询
@Select("select * from book where bid = #{bid}")
@Results({
@Result(property = "bid",column = "bid"),
@Result(property = "title",column = "title"),
@Result(property = "desc",column = "desc"),
@Result(property = "price",column = "price"),
})
Book getBookInfo(int bid);