One-to-multiple integration in Spring-Mybatis

Source: Internet
Author: User

One-to-multiple integration in Spring-Mybatis

Just like the relationship between two objects in Hibernate, the relationship between the two objects is also described in MyBatis! Let's not talk about it. Go to the subject directly.

Few table fields are used to test data!

Student: id, name, supervisor_id

Teacher: id, name

Project Structure


The <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140909/20140909084138142.png" alt = "\">


Com, iss. model. Student

public class Student {private int id;private String name;private Teacher supervisor;public Teacher getSupervisor() {return supervisor;}public void setSupervisor(Teacher supervisor) {this.supervisor = supervisor;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

Com. iss. model. Teacher

package com.iss.model;import java.util.ArrayList;import java.util.List;public class Teacher {private int id;private String name;// private Set
 
   students = new HashSet
  
   ();private List
   
     students = new ArrayList
    
     ();public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public List
     
       getStudents() {return students;}public void setStudents(List
      
        students) {this.students = students;}// public Set
       
         getStudents() {// return students;// }//// public void setStudents(Set
        
          students) {// this.students = students;// }}
        
       
      
     
    
   
  
 

Com. iss. dao. StudentMapper

package com.iss.dao;import com.iss.model.Student;public interface StudentMapper {public Student getById(int id);public void addStudent(Student student);}

Com. iss. dao. TeacherMapper

package com.iss.dao;import com.iss.model.Teacher;public interface TeacherMapper {public void addTeacher(Teacher teacher);public Teacher getById(int id);}

Com, iss, dao, StudentMapper. xml

 
 
  
  Select s. name, t. name from student s, teacher t wheres. supervisor_id = t. idand s. id =#{ id}
   
  
  
   
   
   
  
  
  
   
Insert into studentvalues (# {id}, # {name}, # {supervisor. id })
  
 
Com. iss. dao, TeacherMapper. xml

 
 
  
  
   
Insert into teacher values (# {id}, # {name });
  
  
  Select t. idt_id, t. name t_name, s. name from student s, teacher t wheres. supervisor_id = t. id andt. id =#{ id}
  
  
   
   
   
  
 

The advantage of placing the ing interface and the ing configuration file in the same directory is that you do not need to use mappers in the core configuration file Mybatis to specify the ing configuration file.

Next, let's take a look at the configuration files integrated with Spring and Mybatis.

Mybatis-config, xml

 
 
  
  
 

ApplicationContext. xml

 
 
  
  
   
    
     
Org. gjt. mm. mysql. Driver
    
   
   
    
     
Jdbc: mysql: /// localhost: 3306/test? UseUnicode = true & characterEncoding = UTF-8
    
   
   
    
     
Root
    
   
   
    
     
Root
    
   
  
  
  
   
   
   
  
  
  
  
   
  
  
   
  
  
  
   
    
    
    
    
    
   
  
  
  
  
 



Finally, let's take a look at com. iss. test. Test to test the data in the table.

package com.iss.test;import java.util.List;import java.util.Set;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.iss.dao.StudentMapper;import com.iss.dao.TeacherMapper;import com.iss.dao.UserMapper;import com.iss.model.Student;import com.iss.model.Teacher;import com.iss.model.User;public class Test {public static void main(String[] args) {ApplicationContext ctx = null;ctx = new ClassPathXmlApplicationContext("applicationContext.xml");StudentMapper studentMapper = (StudentMapper) ctx.getBean("studentMapper");TeacherMapper teacherMapper = (TeacherMapper) ctx.getBean("teacherMapper");Teacher teacher = teacherMapper.getById(1);List
 
   students = teacher.getStudents();System.out.println(students.size());for (Student stu : students) {System.out.println(stu.getSupervisor().getName());}}}
 

Okay! This simple bidirectional one-to-multiple solution is complete!



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.