最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
在MyBatis中实现一对多查询和多对一查询代码方式解析
时间:2022-06-29 01:56:29 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下在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)])
相关文章
- 《原神》5.2卡池抽取建议 11-14
- 《原神》5.2版本新怪物介绍 11-14
- 《原神》希诺宁增伤触发方法 11-14
- 《原神》循音觅奇活动入口 11-14
- 《原神》循音觅奇兑换码获取方法 11-14
- 《原神》花羽会活动飞行技巧介绍 11-14