How to implement multi-table joint query in MyBatis

Table of contents

1. What is cascade?

2. How to implement joint query of multiple tables

         Method 1: Through xml configuration file

        1. One-to-one cascading steps

         2. One-to-many linkage steps

        Method 2: Use mapper annotation method

        1. One-to-one mapping

        2. One-to-many mapping

 3. Cascading defects


1. What is cascade?

        Cascade refers to the mapping relationship between multiple objects. Establishing cascade relationships between data improves management efficiency.

  • One-to-one : one object corresponds to a unique object
  • One-to-many : one object corresponds to multiple objects
  • Many-to-many : Multiple objects correspond to multiple objects

2. How to implement joint query of multiple tables

 Method 1: Through xml configuration file

         Example: The relationship between students and classes. Obviously, one student corresponds to one class, which is a one-to-one relationship.

        1. One-to-one cascading steps:

              1.1 Create associated POJO: when one-to-one cascading, store the association relationship in the form of objects

              1.2 Create the corresponding mapper

              1.3 Use the <association> element to complete one-to-one cascading

  • Contain a Banji object in the Student class


  • property is mapped to the field or property of the column result. If the JavaBean has a property  with this name, that property will be used first.
  • columnThe column name in the database, or the alias of the column

             1.4 Write a test class to verify the cascading relationship. The test results are as follows:


           Example: The relationship between classes and students. There are many students in a class, which is a one-to-many relationship.

         2. One-to-many cascading steps:

              2.1 Create "one" party POJO to cascade "many" party objects in the form of collections

              2.2 Create "multiple" POJO objects

              2.3 Create the corresponding mapper

              2.4 Use collection elements to implement one-to-many cascading

             2.5 Write test files to test one-to-many cascade. The test results are as follows:

* It should be noted that in multi-table joint query, fields that are not mapped will not be assigned a value, so they are null.

 

Method 2: Use mapper annotation method

 It’s still the example of the two tables above, but the method has changed.

        1. One-to-one mapping:

                Syntax: @One(Select = one-to-one query method, fetchType = FetchType.EAGER)

                Note: FetchType.lazy is delayed loading , FetchType.EAGER is instant loading

One-to-one use one attribute, @one annotation


        2. One-to-many mapping:

                Syntax:  @Many(select = one-to-many query method, fetchType = FetchType.EAGER)

                Note: FetchType.lazy is delayed loading , FetchType.EAGER is instant loading

One-to-many uses the many attribute and @many annotation

 3. Cascading defects:

  1. Performance defects : Cascading operations will reduce performance and increase program execution time;
  2. Complexity defects : More associations lead to an increase in complexity, which is not conducive to others’ understanding and maintenance;

   Suggestions for use: Add cascade relationships according to actual conditions; multi-layer associations. When there are more than three levels of associations, it is recommended to use cascades as little as possible;

Guess you like

Origin blog.csdn.net/m0_70314224/article/details/125380467