In the introduction of the database, you will hear a few words: The database is divided into a pair of one or one-to-many, many-to-many. For the knowledge learned in school is almost forgotten, here simply mention the relationship between the database. This is an introduction to how MyBatis implements a one-to-one relationship in a database, and we'll start with a first-to-once relationship. The so-called one to one relationship in life is very common, such as a student has and only a student card belonging to him. The following is our hypothetical database physical model.
In this database model, student ID and student table are 1 to 1 relationship. So based on this, we will have two Pojo objects in the Java code Pojo package, student and Selfcard. So in student this student information class should not only contain 3 fields, it should be associated with Studentcard. For example, the student class:
1 Public class Student {2 Private int ID; 3 Private String name; 4 Private String sex; 5 Private Selfcard Selfcard; 6 // omit Get/set method 7 }
Selfcard class:
1 Public class Selfcard {2 Private int ID; 3 Private int StudentID; 4 Private String Note; 5 }
Now the need is to find out the student information (including the student ID information) according to the ID. I still remember how to deal with this situation in the school, first, according to the ID of the student table in the basic information, and then according to StudentID in the Selfcard student ID card of the student information, will be found in the student card information assigned to Studentpojo class. Now it is almost impossible to think of this way. We do not need to use this low way, the ID of the students to query information (including student ID information) is actually a database of 1 to 1 cascade relationship, we can use INNER JOIN SQL statement to query, Of course we can also use MyBatis to provide us with a association pair of cascade.
Inevitably we will always query the student ID information according to StudentID, so we will still have Selfcardmapper.java and selfcardmapper.xml.
1 PackageDay_8_mybatis.mapper;2 3 ImportDay_8_mybatis.pojo.SelfCard;4 5 /**6 * @authorTurbo7 *8 * November 2, 20169 */Ten Public InterfaceSelfcardmapper { OneSelfcard Findselfcardbystudentid (intStudentID); A}
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE Mapper3 Public "-//mybatis.org//dtd Mapper 3.0//en"4 "Http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <Mappernamespace= "Day_8_mybatis.mapper.SelfCardMapper">6 <Resultmaptype= "Day_8_mybatis.pojo.SelfCard"ID= "Studentselfcardmap">7 <ID Property= "id"column= "id"/>8 <result Property= "StudentID"column= "student_id"/>9 <result Property= "Note"column= "Note"/>Ten </Resultmap> One <SelectID= "Findselfcardbystudentid"ParameterType= "int"Resultmap= "Studentselfcardmap"> A Select ID, student_id, note from t_student_selfcard where student_id = #{id} - </Select> - </Mapper>
Of course, our DAO interface to Studentmapper.java is also very simple, but also very common, at this time there is nothing special, according to the normal ID query returned student instance can be.
1 PackageDay_8_mybatis.mapper;2 3 Importday_8_mybatis.pojo.Student;4 5 /**6 * @authorTurbo7 *8 * November 2, 20169 */Ten Public InterfaceStudentmapper { OneStudent Getstudent (intID); A}
The next studentmapper.xml is the key.
1<?xml version= "1.0" encoding= "UTF-8"?>2<!DOCTYPE Mapper3Public "-//mybatis.org//dtd Mapper 3.0//en"4"Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >5<mapper namespace= "Day_8_mybatis.mapper.StudentMapper" >6<resultmap type= "day_8_mybatis.pojo.Student" id= "Studentmap" >7<id property= "id" column= "id"/>8<result property= "name" column= "name"/>9<result property= "Sex" column= "sex"/>Ten <association property= "Selfcard" column= "id" select= "day_8_ Mybatis.mapper.SelfCardMapper.findSelfCardByStudentId "/> One</resultMap> A<select id= "getstudent" parametertype= "int" resultmap= "Studentmap" > -Select ID, name, sex from t_student where id =#{id} -</select> the</mapper>
Remember that there is a reference to the Selfcard class in the student class, which is a one-to-a-kind cascade relationship, in the 10th line of code we use the Assocation keyword provided by MyBatis to indicate that they are a single-to-one relationship. Finally, don't forget to register the Mapper mapper in the configuration file. Well, so far, we've implemented a one-to-one relationship in the database. Next is a pair of multilevel in the database.
The cascade of MyBatis--a pair of relations