首先在数据库创建3个表,老师表、学生表和课程表

其中

1 某一课程和老师是一对一的关系

2 所有课程和老师是多对一的关系

3 某一课程和学生是一对多的关系

表和表中的数据sql语句

/*
Navicat MySQL Data TransferSource Server         : localhost
Source Server Version : 50729
Source Host           : localhost:3306
Source Database       : testmybatisTarget Server Type    : MYSQL
Target Server Version : 50729
File Encoding         : 65001Date: 2020-08-20 22:25:17
*/SET FOREIGN_KEY_CHECKS=0;-- ----------------------------
-- Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (`c_id` int(5) NOT NULL AUTO_INCREMENT,`c_name` varchar(20) NOT NULL,PRIMARY KEY (`c_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('1', '语文');
INSERT INTO `course` VALUES ('2', '数学');
INSERT INTO `course` VALUES ('3', '外语');
INSERT INTO `course` VALUES ('4', '理综');-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (`s_id` int(5) NOT NULL AUTO_INCREMENT,`s_name` varchar(20) NOT NULL,`s_age` int(5) NOT NULL,`c_id` int(5) NOT NULL,PRIMARY KEY (`s_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 'cxq', '18', '1');
INSERT INTO `student` VALUES ('2', 'gy', '18', '2');
INSERT INTO `student` VALUES ('3', 'wkl', '18', '4');
INSERT INTO `student` VALUES ('4', 'gdh', '18', '3');
INSERT INTO `student` VALUES ('5', 'zs', '19', '2');
INSERT INTO `student` VALUES ('6', 'ww', '20', '1');
INSERT INTO `student` VALUES ('7', 'zs111', '21', '3');
INSERT INTO `student` VALUES ('8', 'ls', '22', '4');-- ----------------------------
-- Table structure for `teacher`
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (`t_id` int(5) NOT NULL AUTO_INCREMENT,`t_name` varchar(20) NOT NULL,`t_age` int(5) NOT NULL,`c_id` int(5) NOT NULL,PRIMARY KEY (`t_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('1', '韩梅梅', '25', '1');
INSERT INTO `teacher` VALUES ('2', '秦江', '28', '4');
INSERT INTO `teacher` VALUES ('3', '陈好', '24', '3');
INSERT INTO `teacher` VALUES ('4', '陈婷', '26', '2');

构建实体类(引入lombok)

Course.java

@Data
@ToString
public class Course {private Integer cId;private String cName;private Teacher teacher;private List<Student> students;
}

Student.java

@Data
@ToString
public class Student {private Integer sId;private String sName;private Integer sAge;private Integer cId;
}

Teacher.java

@Data
@ToString
public class Teacher {private Integer tId;private String tName;private Integer tAge;private Integer cId;}

构建Mapper接口 CourseMapper.java

public interface CourseMapper {//查询所有课程并对应老师//多对一List<Course> findCourseAndTeachersByCourseId();//查询某一个课程并对应老师//一对一Course findCourseAndTeacherByCourseId(Integer cId);//通过课程来查询学生//一对多Course findCourseAndStudentByCourseId(Integer cId);
}

CourseMapper.xml

<?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="mapper.CourseMapper"><select id="findCourseAndTeachersByCourseId" resultMap="CourseAndTeachers">select c.c_name,t.t_name,t.t_age from course c ,teacher t where c.c_id = t.c_id</select><resultMap id="CourseAndTeachers" type="entity.Course"><result column="c_name" property="cName"/><association property="teacher" javaType="entity.Teacher"><result column="t_name" property="tName"/><result column="t_age" property="tAge"/></association></resultMap><select id="findCourseAndTeacherByCourseId" parameterType="Integer" resultMap="CourseAndTeacher">select c.c_name,t.t_name,t.t_age from course c ,teacher t where c.c_id = t.c_id and c.c_id=#{cId}</select><resultMap id="CourseAndTeacher" type="entity.Course"><result column="c_name" property="cName"/><association property="teacher" javaType="entity.Teacher"><result column="t_name" property="tName"/><result column="t_age" property="tAge"/></association></resultMap><select id="findCourseAndStudentByCourseId" parameterType="Integer" resultMap="CourseAndStudents">select c.c_name,s.s_name,s.s_age from course c,student s where c.c_id=s.c_id and s.c_id=#{cId}</select><resultMap id="CourseAndStudents" type="entity.Course"><result column="c_name" property="cName"/><collection property="students" ofType="entity.Student"><result column="s_name" property="sName"/><result column="s_age" property="sAge"/></collection></resultMap></mapper>

配置文件中记得开启驼峰命令

测试类

import entity.Course;
import mapper.CourseMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;
import java.io.Reader;
import java.util.List;public class Test {public static void main(String[] args) throws IOException {String resoruce = "mybatis.xml";Reader reader = Resources.getResourceAsReader(resoruce);SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);SqlSession session = sessionFactory.openSession();CourseMapper courseMapper = session.getMapper(CourseMapper.class);List<Course> lists = courseMapper.findCourseAndTeachersByCourseId();Course course1 = courseMapper.findCourseAndTeacherByCourseId(1);Course course2 = courseMapper.findCourseAndStudentByCourseId(1);System.out.println(lists);System.out.println(course1);System.out.println(course2);session.commit();session.close();}}

运行结果

[Course(cId=null, cName=语文, teacher=Teacher(tId=null, tName=韩梅梅, tAge=25, cId=null), students=[]), Course(cId=null, cName=数学, teacher=Teacher(tId=null, tName=陈婷, tAge=26, cId=null), students=[]), Course(cId=null, cName=外语, teacher=Teacher(tId=null, tName=陈好, tAge=24, cId=null), students=[]), Course(cId=null, cName=理综, teacher=Teacher(tId=null, tName=秦江, tAge=28, cId=null), students=[])]
Course(cId=null, cName=语文, teacher=Teacher(tId=null, tName=韩梅梅, tAge=25, cId=null), students=null)
Course(cId=null, cName=语文, teacher=null, students=[Student(sId=null, sName=陈小强, sAge=18, cId=null), Student(sId=null, sName=ww, sAge=20, cId=null)])

Mybatis中的多对一、一对一、一对多相关推荐

  1. Mybatis中的关系映射(一对一,一对多,多对多)

    在网上寻了很久,大多数讲关系性的文章都是大篇幅的去将表照搬上来,本来就很生硬,此文就不在讲述关系性映射的具体实现,转而从浅层来讲讲其概念性. 1.1 关联关系概述 在关系型数据库中,多表之间存在着三种 ...

  2. Mybatis学习第四天——一对一一对多

    两表关系: 1.Mybatis中一对一关系 1 <!-- 两表联查,通过相同属性user_id 2 left join 表示以左边的表为主表 3 --> 4 <select id=& ...

  3. mybatis中的多对一的查询

    多对一也分为单条sql语句和多条sql语句 下面就以员工和就职部门为例: 员工实体类 private Integer empno;private String empname;private Inte ...

  4. Mybatis中的动态SQL,一对一,一对多以及标签

    文章目录 动态SQL中的结果集映射 一对一 一对多 where标签 where..if...标签(作用和Java中的if一样,只要满足if条件的都可以拼接) where..choose..标签(作用和 ...

  5. mysql中建立一对多映射_Mybatis中的高级映射一对一、一对多、多对多

    学习hibernate的时候,小编已经接触多各种映射,mybatis中映射有到底是如何运转的,今天这篇博文,小编主要来简单的介绍一下mybatis中的高级映射,包括一对一.一对多.多对多,希望多有需要 ...

  6. mybatis与php,浅谈mybatis中的#和$的区别

    浅谈mybatis中的#和$的区别 发布于 2016-07-30 11:14:47 | 236 次阅读 | 评论: 0 | 来源: 网友投递 MyBatis 基于Java的持久层框架MyBatis 本 ...

  7. mybatis高级映射多对多查询(二)

    在这篇博客中,我来介绍下mybatis中的多对多查询的案例,在mybatis中,如何使用ResultMap来实现多对多的查询? 案例:一个user可以有很多role,一个role可以有很多entitl ...

  8. MyBatis从入门到精通(九):MyBatis高级结果映射之一对一映射

    最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 本篇博客主要讲解MyBatis中实现查 ...

  9. Mybatis(四) 高级映射,一对一,一对多,多对多映射

    天气甚好,怎能不学习? 一.单向和双向 包括一对一,一对多,多对多这三种情况,但是每一种又分为单向和双向,在hibernate中我们就详细解析过这单向和双向是啥意思,在这里,在重复一遍,就拿一对多这种 ...

最新文章

  1. 机房布线的至高境界,美到窒息!
  2. 【BZOJ】3751: [NOIP2014]解方程【秦九韶公式】【大整数取模技巧】
  3. saltstack之(二)软件包下载安装
  4. 基于 Istio 的全链路灰度方案探索和实践
  5. Unity手游开发札记——移动平台的天气系统实现
  6. activemq端口好_ActiveMQ已准备好黄金时段
  7. vue.js v-bind
  8. 性能测试的需求如何确定
  9. 犹太教、基督教和伊斯兰教的简单关系
  10. 在 hibernate 中使用 proxool 数据库连接池
  11. 阿里4万亿市值是怎样炼成的?(深度)
  12. 2019年畅销好书大盘点,有你喜欢的书吗?
  13. 计算机科学与技术专业导论论文,浅谈计算机科学与技术专业论文
  14. java事务_Java 事务详解
  15. 介绍7个适合普通大学生参加的编程比赛/考试(注:有的比赛如蓝桥杯有多种赛别,本文仅介绍其中的程序设计/编程比赛)
  16. linux打开7z文件_什么是7Z文件(以及如何打开一个文件)?
  17. 图像处理:连通区域算法
  18. 免费,好用的天气预报API
  19. 一些高阶矩的介绍,峰度和偏度
  20. 点云损失函数Chamfer Distance 和 Earth Mover‘s Distance

热门文章

  1. nSPack 手工脱壳过程
  2. 算法——暴力之美(volence‘s beautify of algorithm)
  3. 【问题解决方案】电脑关闭搜狐微门户的弹窗
  4. html数字拼图游戏,JavaScript_JS写的数字拼图小游戏代码[学习参考],复制代码 代码如下:html - phpStudy...
  5. 小鹤同文的几个修改项
  6. 钱多多软件制作第四天
  7. 数通 | 某些基本知识梳理
  8. 用 Python 写个俄罗斯方块小游戏
  9. 100条超搞笑的“雷人”QQ/MSN 签名
  10. 单片机系统怎么用c语言复位,第11章 单片机小知识(复位 )