Spring + SpringMVC + mybatis + easyui integrated instance (6) mybatis one-to-one and one-to-multiple association query

Source: Internet
Author: User

Spring + SpringMVC + mybatis + easyui integrated instance (6) mybatis one-to-one and one-to-multiple association query

We know that multi-table queries will be used when processing business query databases.
Today, we will sort out how mybatis performs one-to-one and one-to-many Association queries in annotation and non-annotation methods.
In fact, in dealing with this problem, mybatis provides two methods: one is the nested result set method, and the other is the nested select, because the second method requires n + 1 queries each time, the system performance will be affected, so we will not introduce it here. Let's look at the nested result set query.

1. One-to-one Association

The method is not annotated.
Here we use the class-teacher one-to-one relationship as an example.
First, create the teacher and classes.

Public class Teacher {private int id; private String name; // get set Method omitted}
Public class Classes {private int id; private String name; private Teacher teacher; // set get method omitted ...}

OneToOneTestDao. java:

public interface OneToOneTestDao {        public List
  
    listClasses();}
  

OneToOneDaoIF-mapper.xml

 
           
           select c.id class_id,c.name class_name, t.id teacher_id ,t.name teacher_name from classes c ,teacher t where c.id = t.id;            
           
            
                         
       
        
       
          
     
    
   
  

Associate the resultMap by using an association tag, and the ing of one-to-one relationship is completed.
What about the annotation method?
However, the annotation method does not provide annotations for one-to-one and one-to-many correlated nested result sets. Therefore, when using annotations, we also need to configure the same resultMap in the configuration file.

Annotation method:

public interface OneToOneTestDao {    @Select(select c.id class_id,c.name class_name, t.id teacher_id ,t.name teacher_name +             from classes c ,teacher t  +            where c.id = t.id)    @ResultMap(com.etoak.dao.OneToOneTestDao.classes)    public List
  
    listClasses();}
  

Add the annotation in the interface method, and remove the query statement in the oneToOneDaoIF-mapper.xml file.

2. One-to-Multiple Association

Now let's look up an example of querying the information of the entire class, including the information of the class, the teacher and all the students.
A class corresponds to a teacher, and a class corresponds to multiple students.

Modify the classes Class

Public class Classes {private int id; private String name; private Teacher teacher; private List
  
   
Students; // set get method omitted}
  

Non-annotation method:

public interface OneToManyTestDao {    public List
  
    listStudents();}
  
 
           
           select c.id class_id,c.name class_name, t.id teacher_id ,t.name teacher_name ,s.id student_id,s.name student_name,s.password student_password    from classes c ,teacher t,student s     where c.id = t.id and c.id = s.class_id            
           
            
                                  
       
        
       
                               
       
        
         
          
         
        
       
          
     
    
   
  

Note: You can see that one-to-many collection settings can be implemented through resultMap.

Annotation method:

Here we also need to use the resultMap configured in xml, and then add the Annotation on the method to point to this resultMap.

public interface OneToManyTestDao {    @Select(select c.id class_id,c.name class_name, t.id teacher_id ,t.name teacher_name ,s.id student_id,s.name student_name,s.password student_password +             from classes c ,teacher t,student s  +             where c.id = t.id and c.id = s.class_id)    @ResultMap(com.etoak.dao.OneToManyTestDao.classes)    public List
  
    listStudents();}
  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.