Mybatis learning tutorial (V) – detailed explanation of the query method of association table
1、 One to one Association
1.1. Propose requirements
Query class information according to class ID (with teacher information)
1.2. Create tables and data
Create a teacher table and class table. Here, we assume that a teacher is only responsible for teaching one class, so the relationship between teachers and classes is a one-to-one relationship.
The relationship between the tables is as follows:
1.3. Define entity class
1. Teacher class. Teacher class is the entity class corresponding to the teacher table.
2. Classes class, which is the entity class corresponding to the class table
1.4. Define the SQL mapping file classmapper xml
Register classmapper.xml in the conf.xml file xml
1.5. Write unit test code
1.6. Mybatis one-to-one association query summary
The association tag is used in mybatis to solve one-to-one association queries. The available attributes of the association tag are as follows:
• property: the name of the object property
• javatype: type of object attribute
• column: the corresponding foreign key field name
• select: encapsulates the result with another query
2、 One to many Association
2.1 putting forward requirements
Query the corresponding class information according to the CLassID, including students and teachers
2.2. Create tables and data
In the above one-to-one association query demonstration, we have created class table and teacher table, so here we create another student table
2.3. Defining entity classes
1. Student class
2. Modify the classes class, add a list < student > students attribute, and use a list < student > set attribute to represent the students in the class, as follows:
2.4. Modify the SQL mapping file classmapper xml
Add the following SQL mapping information
2.5. Write unit test code
2.6. Mybatis one to many association query summary
The collection tag is used in mybatis to solve one to many association queries, and the oftype attribute specifies the object type of the elements in the collection.
About mybatis learning tutorial (V) - the related content of realizing association table query will be introduced to you. I hope it will be helpful to you!