Mybatis进阶2

embedded/2024/11/26 5:51:11/

Mybatis进阶1-CSDN博客

Mybatis入门-CSDN博客

Mybatis入门2-CSDN博客

我们接下来要学习Mybatis的高级查询

我们先在数据库中准备我们需要的数据表

teacher表

课程表:与教师表是一对多的关系,所以有一个外键字段

 

学生表

 

由于学生表和课程表是多对多的关系,所以我们创建一个虚拟表

coures_student表

 在Maven工厂的POJO包下创建对应的类,外键字段的成员变量可以不写,因为没啥用

java">public class Teacher {private int id ;private String teacherName;public Teacher() {}public Teacher(int id, String teacherName) {this.id = id;this.teacherName = teacherName;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName = teacherName;}@Overridepublic String toString() {return "Teacher{" +"id=" + id +", teacherName='" + teacherName + '\'' +'}';}}
public class Student {private int id;private String studentName;private int age;public Student() {}public Student(int id, String studentName, int age) {this.id = id;this.studentName = studentName;this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student{" +"id=" + id +", studentName='" + studentName + '\'' +", age=" + age +'}';}
}
public class Course {private int id;private String courseName;public Course() {}public Course(int id, String courseName) {this.id = id;this.courseName = courseName;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getCourseName() {return courseName;}public void setCourseName(String courseName) {this.courseName = courseName;}@Overridepublic String toString() {return "Course{" +"id=" + id +", courseName='" + courseName + '\'' +'}';}
}

Mybatis多表查询的套路

1.基于需求写sql语句

2.基于sql语句的查询结果,分析类与类之间的关联(建立实体类和实体类的关联)

3.在映射文件中,基于sql查询结果,配置映射关联

我们先来练习一对一查询,通过一个课程的名字来查询叫这门课程老师的信息

接口的方法:

java">public interface CourseMapper {/*** 通过课程名字找课程和老师的信息* @param courseName 课程名* @return 返回Coures类对象*/public Course findCourseByName(String courseName);
}

第一步:

编写sql语句

sql">select course.id as coures_id, course.name, course.course_teacher_id, teacher.id as teacher_id, teacher.name
from course inner join teacher on teacher.id=course.course_teacher_id where course.name='java';

第二步: 基于sql语句的查询结果,分析类与类之间的关联(建立实体类和实体类的关联)

1对1查询结果:在Course类中添加新属性:Teacher对象

java">public class Course {private int id;private String courseName;private Teacher teacher;//添加一个老师对象public Course() {}public Course(int id, String courseName) {this.id = id;this.courseName = courseName;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getCourseName() {return courseName;}public void setCourseName(String courseName) {this.courseName = courseName;}public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}@Overridepublic String toString() {return "Course{" +"id=" + id +", courseName='" + courseName + '\'' +'}';}
}

第三步:在映射文件中,基于sql查询结果,配置映射关联

<mapper namespace="com.hhh.dao.CourseMapper"><!--resultMap标签:解决查询结果字段名与实体类属性名不一致的问题;解决多表查询关联映射--><resultMap id="courseMap" type="com.hhh.pojo.Course"><!--配置:查询结果和Course类的映射关联--><id column="coures_id" property="id"/><result column="course_name" property="courseName"/><!--配置:1对1查询配置:查询结果与Teacher类的关联映射--><association property="teacher" javaType="com.hhh.pojo.Teacher" autoMapping="true"><id column="teacher_id" property="id"/><result column="teacher_name" property="teacherName"/></association></resultMap><select id="findCourseByName" resultMap="courseMap">selectcourse.id as coures_id, course.name as course_name,
//取别名来区分两张表的字段名,不然会报错teacher.id as teacher_id, teacher.name as teacher_namefrom course inner join teacheron teacher.id=course.course_teacher_idwhere course.name=#{courseName};</select>
</mapper>

测试:
 

java">public class CourseMapperTest {@Testpublic void testFindCourseByName(){SqlSession sqlSession = MybatisUtil.openSession();CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);Course java = mapper.findCourseByName("java");System.out.println("课程信息为"+java);Teacher teacher = java.getTeacher();System.out.println("教师信息为"+teacher);}
}

接下来练习一对多查询,根据教师id查询其信息和他教课程的信息

第一步: 编写sql语句

sql">select teacher.id, teacher.name, course.id, course.name 
from teacher inner join course 
on teacher.id = course.course_teacher_id 
where teacher.id=1;

需要注意的是这个两行数据是一个Teacher对象

 第二步:基于sql语句的查询结果,分析类与类之间的关联(建立实体类和实体类的关联)

一对多查询结果:在Teacher类添加新属性:List<Course>集合

java">public class Teacher {private int id ;private String teacherName;List<Course> couerses;public List<Course> getList() {return couerses;}public void setList(List<Course> list) {this.couerses = list;}public Teacher() {}public Teacher(int id, String teacherName) {this.id = id;this.teacherName = teacherName;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName = teacherName;}@Overridepublic String toString() {return "Teacher{" +"id=" + id +", teacherName='" + teacherName + '\'' +'}';}}

第三步:在映射文件中,基于sql查询结果,配置映射关联

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.hhh.dao.TeacherMapper"><resultMap id="teacherMap" type="com.hhh.pojo.Teacher" autoMapping="true"><!--配置:查询结果和Teacher类的映射--><id column="teacher_id" property="id"/><result column="teacher_name" property="teacherName"/><!--配置:查询结果和Course类的映射-->      ofType是List元素的类型<collection property="couerses" javaType="java.util.List" ofType="com.hhh.pojo.Course"><id column="course_id" property="id"/><result column="couerse_name" property="courseName"/></collection></resultMap><select id="findTeacherById" resultMap="teacherMap">  取别名是为了区别,不然会报错select teacher.id as teacher_id, teacher.name as teacher_name,course.id as course_id, course.name as couerse_namefrom teacher inner join course on teacher.id = course.course_teacher_idwhere teacher.id=#{id};</select></mapper>

测试:

java">public class TeacherMapperTest {@Testpublic void testFindTeacherById(){SqlSession sqlSession = MybatisUtil.openSession();TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);Teacher teacherById = mapper.findTeacherById(1);System.out.println("教师的信息为"+teacherById);List<Course> list = teacherById.getList();for (Course course : list) {System.out.println("所教的课程信息为"+course);}}
{

结果:


http://www.ppmy.cn/embedded/34088.html

相关文章

8086 汇编学习 Part 5

流程转移 背景 一般情况下指令是顺序地逐条执行的&#xff0c;而在实际中&#xff0c;常需要改变程序的执行流程。 转移指令 可以控制 CPU 执行内存中某处代码的指令。可以修改 IP &#xff0c;或同时修改 CS 和 IP 的指令。 分类 按转移行为分类 段内转移 &#xff1a; 只修改…

Dockerfile自定义镜像

镜像结构: 语法指令: 这里是这个案例dockerfile里边的指令: 指定基础镜像FROM ubuntu:16.04配置环境变量&#xff0c;JDK的安装目录ENV JAVA_DIR/usr/local拷贝jdk和java项目的包COPY ./jdk8.tar.gz $JAVA_DIR/COPY ./docker-demo.jar /tmp/app.jar安装JDKRUN cd $JAVA_DIR \&…

HTML/CSS1

1.前置说明 请点这里 2.img元素 格式&#xff1a; <img src"图片地址" alt"占位文字" width"图片宽度" height"图片高度">其中alt是当图片加载失败时显示的文字 而且不同内核的浏览器显示出来的占位文字的效果也是不尽相同的…

css基础之显示模式、背景、三大特性

显示模式、背景 一、元素显示模式 1.块级元素 独占一行&#xff0c;例如div,p&#xff0c;hr,h1-h6,ul,ol,form,table 可以设置高度宽度&#xff0c;外边距&#xff0c;内边距 宽度默认容器宽度 里面可以放行内或块级元素 注意&#xff1a; 文字类元素不能使用块级元素 <…

基于Springboot的音乐翻唱与分享平台

基于SpringbootVue的音乐翻唱与分享平台设计与实现 开发语言&#xff1a;Java数据库&#xff1a;MySQL技术&#xff1a;SpringbootMybatis工具&#xff1a;IDEA、Maven、Navicat 系统展示 用户登录 首页 音乐资讯 音乐翻唱 在线听歌 后台登录 后台首页 用户管理 音乐资讯管理…

LED跑马灯

延时函数 自己一开始以为,是sleep()函数 需要到stcisp工具里面调出延时函数 注意这个地方的系统频率,江科大讲的是12MHz,然后调整那个指令集为T1 自己可以封装成一个函数可以延时任意毫秒,先给出1ms的函数,任何给一个形参代表循环的次数 void Delay(unsigned int xms) //1…

4个可将 iPhone iPad iPod 修复至正常状态的 iOS 系统恢复软件

许多iOS用户对操作系统问题感到恐慌&#xff0c;例如iPhone卡在恢复模式、白屏死机、黑屏死机、iOS系统损坏、iTunes连接屏幕、iPhone数据丢失等。这些状态通常很无聊&#xff0c;因为您无法使用 iPhone 执行任何操作。 4个可将 iPhone iPad iPod 修复至正常状态的 iOS 系统恢复…

leetCode74. 搜索二维矩阵

leetCode74. 搜索二维矩阵 二分法模板&#xff1a;见到背过就行 // 区间[L,R]被划分为[L,mid]和[mid 1, R]时使用这个模板 int bsearch_1(int l, int r){while(l < r){int mid l r >> 1;if(check(mid)) r mid; //check()判断mid是否满足性质else l mid 1;}retu…