概述
文章目录
- MyBatis——实现复杂查询
- 多对一情况下实现复杂查询
- 1、环境搭建
- 1.1、建表插数据
- 1.2、导入Lombok依赖,编写实体类
- 1.3、编写接口和Mapper.xml文件
- 2、用子查询实现
- 3、用连表查询实现
- 一对多情况下实现复杂查询
- 1、环境搭建
- 2、用连表查询实现
- 2、用子查询实现
MyBatis——实现复杂查询
多对一情况下实现复杂查询
对学生而言:多个学生关联一个老师
1、环境搭建
1.1、建表插数据
CREATE TABLE `teacher` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO teacher(`id`, `name`) VALUES (1, '秦老师');
INSERT INTO teacher(`id`, `name`) VALUES (2, '沈老师');
CREATE TABLE `student` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`tid` INT(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fktid` (`tid`),
CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1', '小明', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('2', '小红', '2');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('3', '小张', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('4', '小李', '2');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('5', '小王', '1');
1.2、导入Lombok依赖,编写实体类
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
Teacher
@Data
public class Teacher {
private int id;
private String name;
}
Student
@Data
public class Student {
private int id;
private String name;
//每个学生都需要关联一个老师,所以在每个学生里面都有一个老师对象
private Teacher teacher;
}
1.3、编写接口和Mapper.xml文件
//StudentMapper
public interface StudentMapper {
//查询所有学生的信息和老师的名字
List<Student> getStudent();
}
//TeacherMapper
public interface TeacherMapper {
Teacher gerTeacher( int id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cheng.dao.TeacherMapper">
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cheng.dao.StudentMapper">
</mapper>
2、用子查询实现
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cheng.dao.StudentMapper">
<!--思路
1.查询所有学生信息
2.根据查询出来的学生tid,找到对应的老师信息
-->
<select id="getStudent" resultMap="Student" >
select * from mybatis.student
</select>
<!--结果集映射-->
<resultMap id="Student" type="Student">
<!--property实体类的属性,column表中的字段-->
<result property="id" column="id"></result>
<result property="name" column="name"></result>
<!--复杂的属性需要单独处理
对象使用association teacher在Student实体类中是一个对象 javaType指定属性的类型 在里面绑定一个查询语句select="getTeacher"
集合使用collection
-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"></association>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from mybatis.teacher where id=#{id}
</select>
</mapper>
测试
@Test
public void getStudent(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudent();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
3、用连表查询实现
<select id="getStudent" resultMap="Student1">
select s.id sid,s.name sname,t.name tname
from mybatis.student s,mybatis.teacher t
where s.tid=t.id
</select>
<resultMap id="Student1" type="Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<association property="teacher" javaType="Teacher" >
<result property="name" column="tname"></result>
</association>
</resultMap>
测试
@Test
public void getStudent2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudent2();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
一对多情况下实现复杂查询
对老师而言:一个老师拥有多个学生
1、环境搭建
环境搭建除了实体类不一样,其他和一对多一样
Teacher
@Data
public class Teacher {
private int id;
private String name;
//一个老师拥有多个学生
private List<Student> students;
}
Student
@Data
public class Student {
private int id;
private String name;
private int tid;
}
2、用连表查询实现
//获取指定老师下的所有学生及老师的信息
Teacher getTeacher(@Param("tid") int id);
<mapper namespace="com.cheng.dao.TeacherMapper">
<select id="getTeacher" resultMap="TeacherSt">
select s.id sid,s.name sname,t.name tname,t.id tid
from mybatis.student s,mybatis.teacher t
where s.tid=t.id and t.id=#{tid}
</select>
<resultMap id="TeacherSt" type="Teacher">
<result property="id" column="tid"></result>
<result property="name" column="tname"></result>
<!--集合用collection 集合中的泛型信息,用ofType获取-->
<collection property="students" ofType="Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<result property="tid" column="tid"></result>
</collection>
</resultMap>
</mapper>
测试
@Test
public void getTeacher(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
2、用子查询实现
//获取指定老师下的所有学生及老师的信息
Teacher getTeacher2(@Param("tid") int id);
<!--思路:
1、查询所有老师的信息
2、根据查询出来的老师的id,找到对应的学生信息
-->
<select id="getTeacher2" resultMap="TeacherStu">
select * from teacher where id=#{id}
</select>
<resultMap id="TeacherStu" type="Teacher">
<result property="id" column="id"></result>
<result property="name" column="name"></result>
<collection property="students" column="id" ofType="Student" javaType="ArrayList" select="getStudent"></collection>
</resultMap>
<select id="getStudent" resultType="student">
select * from student where tid= #{tid}
</select>
测试
@Test
public void getTeacher2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
总结:
- collection集合 一对多
- association对象 多对一
最后
以上就是傲娇口红为你收集整理的MyBatis——一对多和多对一情况下实现复杂查询MyBatis——实现复杂查询的全部内容,希望文章能够帮你解决MyBatis——一对多和多对一情况下实现复杂查询MyBatis——实现复杂查询所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复