How to realize one to many query and many to one query in MyBatis
The knowledge of this article "how to achieve one-to-many query and many-to-one query in MyBatis" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve one-to-many query and many-to-one query in MyBatis" article.
1. Many-to-one, 1, 1 environment construction
Database
CREATE TABLE teacher (id INT (10) NOT NULL,NAME VARCHAR (64) DEFAULT NULL,PRIMARY KEY (id),) ENGINE=INNODB DEFAULT CHARSET=utf8;INSERT INTO teacher (id, NAME) VALUES (1Regent 'teacher'); CREATE TABLE student (id INT (10) NOT NULL,NAME VARCHAR (64) DEFAULT NULL,tid INT (10) DEFAULT NULL,PRIMARY KEY (id), KEY fktid (tid), CONSTRAINT fktid FOREIGN KEY (tid) REFERENCES teacher (id) ALTER TABLE student ENGINE=INNODB DEFAULT CHARSET=utf8;INSERT INTO student (id, NAME, tid) VALUES (1Ling 'Xiaoming', 1) INSERT INTO student (id, NAME, tid) VALUES (2) Xiao Hong, 1); INSERT INTO student (id, NAME, tid) VALUES (3) Xiao Zhang, 1); INSERT INTO student (id, NAME, tid) VALUES (5)
MyBatis.xml configuration
1, 2 write entity classes,
Students
@ Datapublic class Student {private int id; private String name; private Teacher teacher;}
Teacher
@ Datapublic class Teacher {private int id; private String name;} 1, 3 write interface methods public interface StudentMapper {List getStudentList (); List getStudentList1 () } 1, 4 write Mapper select * from student select * from teacher select s.id sid,s.name sname,t.name tname from Student Stemo teacher t where s.tid=t.id 1, 5 to implement package com.Google.Dao Import com.Google.pojo.Student;import com.Google.units.sqlSessionFactory;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import java.util.List;public class StudentMapperText {@ Test public void getStudent () {SqlSession sqlSession = sqlSessionFactory.getsqlSession (); StudentMapper mapper = sqlSession.getMapper (StudentMapper.class); List studentList = mapper.getStudentList (); for (Student student: studentList) {System.out.println (student) } sqlSession.close ();} @ Test public void getStudent1 () {SqlSession sqlSession = sqlSessionFactory.getsqlSession (); StudentMapper mapper = sqlSession.getMapper (StudentMapper.class); List studentList = mapper.getStudentList1 (); for (Student student: studentList) {System.out.println (student);} sqlSession.close ();}} 1,6 run results
Student (id=1, name= Xiaoming, teacher=Teacher (id=0, name= teacher Luo)
Student (id=2, name= Xiao Hong, teacher=Teacher (id=0, name= teacher Luo))
Student (id=3, name= Xiao Zhang, teacher=Teacher (id=0, name= teacher))
Student (id=4, name= Xiao Wang, teacher=Teacher (id=0, name= teacher Luo))
Student (id=5, name= Luo, teacher=Teacher (id=0, name= teacher Luo))
2. One-to-many 2, 1 environments are the same as one-to-many entity classes @ Datapublic class Student {private int id; private String name; private int tid;} @ Datapublic class Teacher {private int id; private String name; / / A teacher has multiple students. Create a student collection public interface TeacherMapper {Teacher getTeacher (@ Param ("tid") int id) for the teacher. Teacher getTeacher1 (@ Param ("tid") int id) } 2, 4 write Mapper to configure select s.id sid, s.name sname, t.id tid, t.name tname from student s Teacher t where s.tid = t.id and t.id = # {tid} select * from teacher where id=# {tid} select * from student where tid=# {tid} 2, 5 implement public class TeacherMapperText {@ Test public void getTeacher () {SqlSession sqlSession = sqlSessionFactory.getsqlSession () TeacherMapper mapper = sqlSession.getMapper (TeacherMapper.class); Teacher teacher = mapper.getTeacher (1); System.out.println (teacher); sqlSession.close ();} @ Test public void getTeacher1 () {SqlSession sqlSession = sqlSessionFactory.getsqlSession (); TeacherMapper mapper = sqlSession.getMapper (TeacherMapper.class); Teacher teacher1 = mapper.getTeacher1 (1); System.out.println (teacher1) SqlSession.close ();}} 2, 6 run results
Teacher (id=1, name= Mr. Luo, student= [Student (id=1, name= Xiao Ming, tid=1), Student (id=2, name= Xiao Hong, tid=1), Student (id=3, name= Xiao Zhang, tid=1), Student (id=4, name= Xiao Wang, tid=1), Student (id=5, name= Xiao Luo, tid=1)])
The above is about the content of this article on "how to achieve one-to-many query and many-to-one query in MyBatis". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.