本篇文章小编给大家分享一下在MyBatis中实现一对多查询和多对一查询代码方式解析,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1、多对一
1、1环境搭建
数据库
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 (1,'罗老师'); 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 (1,'小明',1); INSERT INTO student (id ,NAME ,tid) VALUES (2,'小红',1); INSERT INTO student (id ,NAME ,tid) VALUES (3,'小张',1); INSERT INTO student (id ,NAME ,tid) VALUES (5,'小罗',1);
MyBatis.xml配置
1、2编写实体类、
学生·
@Data public class Student { private int id; private String name; private Teacher teacher; }
老师·
@Data public class Teacher { private int id; private String name; }
1、3编写接口方法
public interface StudentMapper { ListgetStudentList(); List getStudentList1(); }
1、4编写Mapper
1、5实现
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); ListstudentList = 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运行结果
Student(id=1, name=小明, teacher=Teacher(id=0, name=罗老师))
Student(id=2, name=小红, teacher=Teacher(id=0, name=罗老师))
Student(id=3, name=小张, teacher=Teacher(id=0, name=罗老师))
Student(id=4, name=小王, teacher=Teacher(id=0, name=罗老师))
Student(id=5, name=小罗, teacher=Teacher(id=0, name=罗老师))
2、一对多
2、1环境搭建和一对多一样
2、2编写实体类
@Data public class Student { private int id; private String name; private int tid; }
@Data public class Teacher { private int id; private String name; //一个老师拥有多个学生,给老师创建一个学生集合 private Liststudent; }
2、3编写接口的方法
public interface TeacherMapper { Teacher getTeacher(@Param("tid") int id); Teacher getTeacher1(@Param("tid") int id); }
2、4编写Mapper配置
2、5实现
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运行结果
Teacher(id=1, name=罗老师, student=[Student(id=1, name=小明, tid=1), Student(id=2, name=小红, tid=1), Student(id=3, name=小张, tid=1), Student(id=4, name=小王, tid=1), Student(id=5, name=小罗, tid=1)])
忍者必须死34399账号登录版 最新版v1.0.138v2.0.72
下载勇者秘境oppo版 安卓版v1.0.5
下载忍者必须死3一加版 最新版v1.0.138v2.0.72
下载绝世仙王官方正版 最新安卓版v1.0.49
下载Goat Simulator 3手机版 安卓版v1.0.8.2
Goat Simulator 3手机版是一个非常有趣的模拟游
Goat Simulator 3国际服 安卓版v1.0.8.2
Goat Simulator 3国际版是一个非常有趣的山羊模
烟花燃放模拟器中文版 2025最新版v1.0
烟花燃放模拟器是款仿真的烟花绽放模拟器类型单机小游戏,全方位
我的世界动漫世界 手机版v友y整合
我的世界动漫世界模组整合包是一款加入了动漫元素的素材整合包,
我的世界贝爷生存整合包 最新版v隔壁老王
我的世界MITE贝爷生存整合包是一款根据原版MC制作的魔改整