There are two main ways to use annotations MyBatis query:
First, the query twice, and then associate integration, without the use of contingency tables query.
1, Mapper file:
1 the @Select ( "the SELECT stu.id, stu.stu_key, stu.stu_no, stu.stu_name, stu.tel, stu.email_add, stu.birthdate, stu.gender, stu.school_id, stu.college_id, stu.major_id, stu.class_id, stu.description, stu.remark, stu.create_date, the WHERE stu.stu_key stu.update_date the FROM stu_info STU stuKey} = {# " ) 2 @Results ({ . 3 . 4 @Result ( . 5 // important: column: associated foreign key property: entity class attributes, not the field, StuInfo inside . 6 column = "school_id", property = " schoolInfo " , . 7 One @One = (SELECT = "com.ahu.mapper.StuInfoMapper.getSchoolInfo" ) . 8 ) 9 }) 10 StuInfo showCompInfoByStuKey(String stuKey); 11 12 @Select("SELECT school_info.id, school_info.school_id, school_info.school_name, school_info.create_time, school_info.update_time, school_info.remark FROM school_info WHERE school_info.school_id= #{school_info.school_id}") 13 SchoolInfo getSchoolInfo();
2, StuInfo entity class
. 1 @Data 2 public class StuInfo the implements the Serializable { . 3 . 4 // @TableId (type = IdType.AUTO) // primary key database need to set the self-energizing self-energizing a primary key . 5 Private Integer ID; . 6 // stuKey used as the primary key, not update operations using the strategy . 7 @TableField (updateStrategy = FieldStrategy.NEVER) . 8 Private String stuKey; . 9 Private String stuno; 10 Private String stuname; . 11 Private String password; 12 is Private String Tel; 13 is Private String emailAdd; 14 / ** 15 * @Description: annotation is converted back to the main @JsonFormat reception time format 16 * Notes @DataFormAT mainly front-to-back time format conversion . 17 * / 18 is @JsonFormat (pattern = "the MM-dd-YYYY", TimeZone = "GMT +. 8" ) . 19 @DateTimeFormat (pattern = "the MM-dd-YYYY" ) 20 is Private a Date BIRTHDATE; 21 is Private String Gender; 22 is Private String schoolId; 23 is Private String collegeId; 24- Private String majorId; 25 Private String classId; 26 private String description; 27 private String remark; 28 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 29 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 30 private Date createDate; 31 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 32 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 33 private Date updateDate; 37 [) to false @TableField (= exist36the field does not exist in the database, using @TableField annotation process, with the use of table lookup//3534 is private ClassInfo classInfo; 38 @TableField(exist = false) 39 private CollegeInfo collegeInfo; 40 @TableField(exist = false) 41 private MajorInfo majorInfo; 42 @TableField(exist = false) 43 private SchoolInfo schoolInfo; 44 }
Second, the query again, using the linked table query.
1 @Select("SELECT stu.id, stu.stu_key, stu.stu_no, stu.stu_name, stu.tel, stu.email_add, stu.birthdate, stu.gender, stu.school_id, sch.school_name, stu.college_id, col.college_name, stu.major_id, maj.major_name, stu.class_id, cla.class_name, stu.description, stu.remark, stu.create_date, stu.update_date FROM stu_info stu LEFT JOIN school_info sch ON stu.school_id = sch.school_id LEFT JOIN college_info col ON stu.college_id = col.college_id LEFT JOIN major_info maj ON stu.major_id = maj.major_id LEFT JOIN class_info cla ON stu.class_id = cla.class_id WHERE stu.stu_key = #{stuKey}") 2 @Results({ 3 //@Result(column="school_id",property="schoolId"), 4 @Result( 5 //Important: column: foreign key associated property: entity class attributes, not the field, StuInfo stuInfo inside . 6 column = "school_id", property = "schoolInfo.schoolId" ) , . 7 @Result ( . 8 // master mapping table is also done , or is empty null . 9 column = "school_id", Property = "schoolId" ) , 10 @Result (column = "school_name", Property = "schoolInfo.schoolName" ), . 11 @Result (column = "college_id", Property = "collegeInfo.collegeId" ), 12 is @Result (column = "college_id", Property = "collegeId" ), 13 @Result(column = "college_name",property = "collegeInfo.collegeName"), 14 @Result(column = "major_id",property = "majorInfo.majorId"), 15 @Result(column = "major_id",property = "majorId"), 16 @Result(column = "major_name",property = "majorInfo.majorName"), 17 @Result(column = "class_id",property = "classInfo.classId"), 18 @Result(column = "class_id",property = "classId"), 19 @Result(column = "class_name",property = "classInfo.className"), 20 }) 21 StuInfo showCompInfoByStuKey(String stuKey);
reference:
1、https://blog.csdn.net/qq_36228916/article/details/93881786