• MyBatis关联映射例题


    在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。

    基于框架的程序要成功运行,对于JAR包的版本,配置文件的正确性有着苛刻的要求,任何一个地方出错了,都会导致框架程序运行失败。 请务必严格按照教程的指导,完全模仿操作,直到成功看到运行效果。 第一次成功之后,信心,思路都会有较好的铺垫,然后再根据自己的疑惑,在“成功”的代码上做原本想做的改动和调整,这样可以大大节约学习的时间,提高效率,切勿一来就擅自改动,给自己的学习制造障碍!!!

    源码:

    链接:https://pan.baidu.com/s/1kJCSSK_nTvwvct-SeMGFvw?pwd=j1v8
    提取码:j1v8

     在实际应用中,对数据库的操作会涉及多张表,这在面向对象中就涉及对象与对象之间的关联关系。针对多表之间的操作,MyBatis提供了关联映射,通过关联映射来处理与对象之间的关联联系。

    在关系型数据库中,多表之间存在着三种关联关系,分别为一对一、一对多和多对多。

    • 一对一:在任意一方引入对方主键作为外键。
    • 一对多:在“多”的一方,,添加“一”的一方的主键作为外键。
    • 多对多:产生中间关系表,引入两张表的主键作为外键,两个主键成为联合主键或使用新的字段作为主键。

    目录

    一对一:

    一对多:

    多对多:


    前提:创建一个java项目,并导入相应的jar包。

    一对一:

    通过学生与学生证之间的一对一关联关系为例。查询学生及其关联的学生证信息是先通过查询学生表中的主键来获取学生信息,然后通过表中的外键来获取学生证表中的学生证号信息。

     1.创建数据库和表并插入一些数据。

    1. create database db_mybatis ;
    2. use db_mybatis;
    3. create table tb_studentidcard(
    4. id int primary key auto_increment ,
    5. CODE varchar(8)
    6. );
    7. insert into tb_studentidcard values(null,'12345678');
    8. insert into tb_studentidcard values(null,'87654321');
    9. create table tb_student(
    10. id int primary key auto_increment ,
    11. name varchar(32) ,
    12. sex char(1) ,
    13. card_id int unique ,
    14. foreign key (card_id) references tb_studentidcard(id)
    15. );
    16. insert into tb_student values(null,'zyy', 'f','1');
    17. insert into tb_student values(null,'wrr' ,'m','2');

     2.创建一个包命名为po,在该包内创建两个实体类StudentIdCard和Student。

     StudentIdCard.java

    1. package po;
    2. public class StudentIdCard {
    3. private Integer id;
    4. private String code;
    5. public Integer getId() {
    6. return id;
    7. }
    8. public void setId(Integer id) {
    9. this.id=id;
    10. }
    11. public String getCode() {
    12. return code;
    13. }
    14. public void setCode(String code) {
    15. this.code=code;
    16. }
    17. public String toString() {
    18. return "StudentIdCard [id="+id+",code="+code+"]";
    19. }
    20. }

    Student.java

    1. package po;
    2. public class Student {
    3. private Integer id;
    4. private String name;
    5. private String sex;
    6. private StudentIdCard studentIdCard;
    7. public Integer getId() {
    8. return id;
    9. }
    10. public void setId(Integer id) {
    11. this.id=id;
    12. }
    13. public String getName() {
    14. return name;
    15. }
    16. public void setName(String name) {
    17. this.name=name;
    18. }
    19. public String getSex() {
    20. return sex;
    21. }
    22. public void setSex(String sex) {
    23. this.sex=sex;
    24. }
    25. public StudentIdCard getStudentIdCard() {
    26. return studentIdCard;
    27. }
    28. public void setStudentIdCard(StudentIdCard studentIdCard) {
    29. this.studentIdCard=studentIdCard;
    30. }
    31. public String toString() {
    32. return "Student[id="+id+",name="+name+",sex="+sex+",studentidcard="+studentIdCard+",]";
    33. }
    34. }

    3.创建一个包命名为mapper,创建StudentIdCardMapper.xml和StudentMapper.xml并在两个映射文件中编写一对一关联映射查询的配置信息。

    StudentIdCardMapper.xml

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="mapper.StudentIdCardMapper">
    4. <select id="findStudentIdCardById" parameterType="Integer" resultType="po.StudentIdCard">
    5. select * from tb_studentidcard where id=#{id}
    6. select>
    7. mapper>

    StudentMapper.xml

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="mapper.StudentMapper">
    4. <select id="findStudentById" parameterType="Integer" resultMap="StudentIdCardWithStudentResult">
    5. select * from tb_student where id=#{id}
    6. select>
    7. <resultMap type="po.Student" id="StudentIdCardWithStudentResult">
    8. <id property="id" column="id"/>
    9. <result property="name" column="name"/>
    10. <result property="sex" column="sex"/>
    11. <association property="studentIdCard" column="card_id" javaType="po.StudentIdCard" select="mapper.StudentIdCardMapper.findStudentIdCardById" />
    12. resultMap>
    13. mapper>

     4.在src目录下创建核心配置文件mybatis-config.xml。

    1. configuration
    2. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    4. <configuration>
    5. <typeAliases>
    6. <package name="mybatis"/>
    7. typeAliases>
    8. <environments default="development">
    9. <environment id="development">
    10. <transactionManager type="JDBC"/>
    11. <dataSource type="POOLED">
    12. <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
    13. <property name="url" value="jdbc:mysql://localhost:3306/db_mybatis?characterEncoding=UTF-8"/>
    14. <property name="username" value="root"/>
    15. <property name="password" value="root"/>
    16. dataSource>
    17. environment>
    18. environments>
    19. <mappers>
    20. <mapper resource="mapper/StudentIdCardMapper.xml" />
    21. <mapper resource="mapper/StudentMapper.xml" />
    22. mappers>
    23. configuration>

    5.创建一个包命名为test,在包内编写测试程序test01

    1. package test;
    2. import java.io.InputStream;
    3. import org.apache.ibatis.io.Resources;
    4. import org.apache.ibatis.session.SqlSession;
    5. import org.apache.ibatis.session.SqlSessionFactory;
    6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    7. import po.Student;
    8. public class test01 {
    9. public static void main(String[] args) throws Exception{
    10. String resourse="mybatis-config.xml";
    11. InputStream inputStream=Resources.getResourceAsStream(resourse);
    12. SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    13. SqlSession sqlSession=sessionFactory.openSession();
    14. Student student=sqlSession.selectOne("mapper.StudentMapper.findStudentById",1);
    15. System.out.println(student);
    16. sqlSession.commit();
    17. sqlSession.close();
    18. }
    19. }

    使用MyBatis嵌套查询的方式查询出了学生及其关联的学生证信息,这就是MyBatis的一对一关联查询。

    改进:

    虽然使用嵌套查询的方式比较简单,但是嵌套查询的方式要执行多条SQL语句,这对于大型数据集合和列表展示不是很好,因为这样可能会导致成百上千条关联的SQL语句被执行,从而极大地消耗数据库性能,并且会降低查询效率。为此,MyBatis提供了嵌套结果的方式进行关联查询。

     对StudentMapper.xml进行修改:

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="mapper.StudentMapper">
    4. <select id="findStudentById" parameterType="Integer" resultMap="StudentIdCardWithStudentResult">
    5. select s.* ,sidcard.code
    6. from tb_student s, tb_studentidcard sidcard
    7. where s.card_id =sidcard.id and s.id =#{id}
    8. select>
    9. <resultMap type="po.Student" id="StudentIdCardWithStudentResult">
    10. <id property="id" column="id"/>
    11. <result property="name" column="name"/>
    12. <result property="sex" column="sex"/>
    13. <association property="studentIdCard" javaType="po.StudentIdCard" >
    14. <id property="id" column="card_id"/>
    15. <result property="code" column="code"/>
    16. association>
    17. resultMap>
    18. mapper>

    运行test01.java结果与上面相同。但是MyBatis嵌套结果的方式只执行了一条SQL语句。

     在使用MyBatis嵌套查询方式进行关联查询映射时,使用MyBatis的延迟加载在一定程度上可以降低运行消耗并提高查询效率。MyBatis默认没有开启延迟加载,需要在核心配置文件mybatis-config.xml中使用元素进行配置。

    1. <settings>
    2. <setting name="lazyLoadingEnabled" value="true"/>
    3. <setting name="aggressiveLazyLoading" value="false"/>
    4. settings>

    在映射文件中,MyBatis关联映射的元素和元素中都已经默认配置了延迟加载属性,即默认属性 fetchType="lazy" ,所以在配置文件中开启延迟加载后,无须在映射文件中再做配置。

    一对多:

    在实际应用中,应用更多的关联关系是一对多(或多对一)。例如一个系有多个老师,也即多个老师属于一个系。

    <resultMap>元素中,包含了一个子元素,MyBatis就是通过该元素来处理一对多关联关系的。

    1.我们在db_mybatis数据库中创建tb_banji表和tb_teacher表并插入几条数据

    1. create table tb_xi(
    2. id int primary key auto_increment ,
    3. name varchar(32)
    4. );
    5. insert into tb_banji values(null,'xi01');
    6. insert into tb_banji values(null,'xi02');
    7. create table tb_teacher(
    8. id int primary key auto_increment ,
    9. name varchar(32) ,
    10. sex char(1) ,
    11. xi_id int ,
    12. foreign key (xi_id) references tb_banji(id)
    13. );
    14. insert into tb_teacher values(null,'qdw', 'f',1);
    15. insert into tb_teacher values(null,'hxy' ,'m',2);

    2.在po包中创建实体类Xi和Teacher

    Teacher.java

    1. package po;
    2. public class Teacher {
    3. private Integer id;
    4. private String name;
    5. private String sex;
    6. public Integer getId() {
    7. return id;
    8. }
    9. public void setId(Integer id) {
    10. this.id=id;
    11. }
    12. public String getName() {
    13. return name;
    14. }
    15. public void setName(String name) {
    16. this.name=name;
    17. }
    18. public String getSex() {
    19. return sex;
    20. }
    21. public void setSex(String sex) {
    22. this.sex=sex;
    23. }
    24. public String toString() {
    25. return "Student[id="+id+",name="+name+",sex="+sex+"]";
    26. }
    27. }

    Xi.java

    1. package po;
    2. import java.util.List;
    3. public class Xi {
    4. private Integer id;
    5. private String name;
    6. private List<Teacher> TeacherList;
    7. public Integer getId() {
    8. return id;
    9. }
    10. public void setId(Integer id) {
    11. this.id=id;
    12. }
    13. public String getCode() {
    14. return name;
    15. }
    16. public void setName(String name) {
    17. this.name=name;
    18. }
    19. public List<Teacher> getTeacherList() {
    20. return TeacherList;
    21. }
    22. public void setTeacherList(List teacherList) {
    23. this.TeacherList=teacherList;
    24. }
    25. public String toString() {
    26. return "StudentIdCard [id="+id+",name="+name+",teacherList="+TeacherList+"]";
    27. }
    28. }

    3.在mapper包下创建系映射文件XiMapper.xml,并编写配置。

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="mapper.XiMapper">
    4. <select id="findXiWithTeacher" parameterType="Integer" resultMap="XiWithTeacherResult">
    5. select x.* ,t.id as teacher_id ,t.name as tname ,t.sex
    6. from tb_xi x, tb_teacher t
    7. where x.id =t.xi_id and x.id =#{id}
    8. select>
    9. <resultMap type="po.Xi" id="XiWithTeacherResult">
    10. <id property="id" column="id"/>
    11. <result property="name" column="name"/>
    12. <collection property="teacherList" ofType="po.Teacher">
    13. <id property="id" column="teacher_id"/>
    14. <result property="name" column="tname"/>
    15. <result property="sex" column="sex"/>
    16. collection>
    17. resultMap>
    18. mapper>

    4.在mybatis-config.xml核心配置文件中添加路径配置。

           <mapper resource="mapper/XiMapper.xml" />

    5.在test包创建test02类,进行测试。

    1. package test;
    2. import java.io.InputStream;
    3. import org.apache.ibatis.io.Resources;
    4. import org.apache.ibatis.session.SqlSession;
    5. import org.apache.ibatis.session.SqlSessionFactory;
    6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    7. import po.Xi;
    8. public class test02 {
    9. public static void main(String[] args) throws Exception{
    10. String resourse="mybatis-config.xml";
    11. InputStream inputStream=Resources.getResourceAsStream(resourse);
    12. SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    13. SqlSession sqlSession=sessionFactory.openSession();
    14. Xi xi=sqlSession.selectOne("mapper.XiMapper.findXiWithTeacher",1);
    15. System.out.println(xi);
    16. sqlSession.commit();
    17. sqlSession.close();
    18. }
    19. }

    运行结果:

    班级与学生之间是一对多的关联关系,但如果从单个学生的角度出发,一个学生只能属于一个班级,即一对一的关联关系。

    多对多:

    在实际项目开发中,多对多的关联关系也是非常常见的。以学生和课程为例,一个学生可以选修多门课程,而一门课程又可以被多个学生选修,学生和课程就属于多对多的关联关系。

    1.db_mybatis数据库中创建tb_couse表和tb_electivecourse表和tb_student表(一对一创建)

    1. create table tb_course(
    2. id int primary key auto_increment ,
    3. name varchar(32) ,
    4. code vaRchar(32)
    5. )
    6. insert into tb_course values(null,'java' ,'001');
    7. insert into tb_course values(null,'javaweb' ,'101');
    8. create table tb_electivecourse(
    9. id int primary key auto_increment ,
    10. student_id int ,
    11. course_id int ,
    12. foreign key (student_id) references tb_student(id) ,
    13. foreign key (course_id) references tb_course(id)
    14. )
    15. insert into tb_electivecourse values(null,1,1);
    16. insert into tb_electivecourse values(null,1,2);
    17. insert into tb_electivecourse values(null,2,2);

    2.在po包下创建Course类并修改Student类

     Course.java

    1. package po;
    2. import java.util.List;
    3. public class Course {
    4. private Integer id;
    5. private String name;
    6. private String code;
    7. private List<Student> studentList;
    8. public Integer getId() {
    9. return id;
    10. }
    11. public void setId(Integer id) {
    12. this.id=id;
    13. }
    14. public String getName() {
    15. return name;
    16. }
    17. public void setName(String name) {
    18. this.name=name;
    19. }
    20. public String getCode() {
    21. return code;
    22. }
    23. public void setCode(String code) {
    24. this.code=code;
    25. }
    26. public List<Student> getStudentlist() {
    27. return studentList;
    28. }
    29. public void setStudentlist(List studentList) {
    30. this.studentList=studentList;
    31. }
    32. public String toString() {
    33. return "Course [id="+id+",name="+name+",code="+code+",studentlist="+studentList+"]";
    34. }
    35. }

     Student.java

    1. package po;
    2. import java.util.List;
    3. public class Student {
    4. private Integer id;
    5. private String name;
    6. private String sex;
    7. private StudentIdCard studentIdCard;
    8. private List<Course> courseList;
    9. public Integer getId() {
    10. return id;
    11. }
    12. public void setId(Integer id) {
    13. this.id=id;
    14. }
    15. public String getName() {
    16. return name;
    17. }
    18. public void setName(String name) {
    19. this.name=name;
    20. }
    21. public String getSex() {
    22. return sex;
    23. }
    24. public void setSex(String sex) {
    25. this.sex=sex;
    26. }
    27. public StudentIdCard getStudentIdCard() {
    28. return studentIdCard;
    29. }
    30. public void setStudentIdCard(StudentIdCard studentIdCard) {
    31. this.studentIdCard=studentIdCard;
    32. }
    33. public List<Course> getCourselist() {
    34. return courseList;
    35. }
    36. public void setCourselist(List courselist) {
    37. this.courseList=courselist;
    38. }
    39. public String toString() {
    40. return "Student[id="+id+",name="+name+",sex="+sex+"]";
    41. }
    42. }

    3.在mapper包下创建CourseMapper.xml映射文件和StudentMapper.xml

    CourseMapper.xml

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="mapper.CourseMapper">
    4. <select id="findCourseWithStudent" parameterType="Integer" resultMap="CourseWithStudentResult">
    5. select * from tb_course where id=#{id}
    6. select>
    7. <resultMap type="po.Course" id="CourseWithStudentResult">
    8. <id property="id" column="id"/>
    9. <result property="name" column="name"/>
    10. <result property="code" column="code"/>
    11. <collection property="studentList" ofType="po.Student" column="id" select="mapper.StudentMapper.findStudentById">
    12. collection>
    13. resultMap>
    14. mapper>

    修改StudentMapper.xml

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="mapper.StudentMapper">
    4. <select id="findStudentById" parameterType="Integer" resultType="po.Student">
    5. select * from tb_student where id in (
    6. select student_id from tb_electivecourse where course_id=#{id}
    7. )
    8. select>
    9. mapper>

    4.修改mybatis-config.xml,添加映射文件路径

    <mapper resource="mapper/CourseMapper.xml"/>

    5.在test包下创建test03类进行测试。

    1. package test;
    2. import java.io.InputStream;
    3. import org.apache.ibatis.io.Resources;
    4. import org.apache.ibatis.session.SqlSession;
    5. import org.apache.ibatis.session.SqlSessionFactory;
    6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    7. import po.Course;
    8. public class test03 {
    9. public static void main(String[] args) throws Exception{
    10. String resourse="mybatis-config.xml";
    11. InputStream inputStream=Resources.getResourceAsStream(resourse);
    12. SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    13. SqlSession sqlSession=sessionFactory.openSession();
    14. Course course=sqlSession.selectOne("mapper.CourseMapper.findCourseWithStudent",1);
    15. System.out.println(course);
    16. sqlSession.commit();
    17. sqlSession.close();
    18. }
    19. }

    运行结果

     如果喜欢用多表关联查询的SQL语句,可以对CourseMapper.xml进行修改。

    1. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="mapper.CourseMapper">
    4. <select id="findCourseWithStudent" parameterType="Integer" resultMap="CourseWithStudentResult">
    5. select c.* ,s.id as sid ,s.name as sname ,s.sex as ssex
    6. from tb_course c ,tb_student s,tb_electivecourse ec
    7. where ec.course_id=c.id
    8. and ec.student_id =s.id and c.id=#{id}
    9. select>
    10. <resultMap type="po.Course" id="CourseWithStudentResult">
    11. <id property="id" column="id"/>
    12. <result property="name" column="name"/>
    13. <result property="code" column="code"/>
    14. <collection property="studentList" ofType="po.Student">
    15. <id property="id" column="sid"/>
    16. <result property="name" column="sname"/>
    17. <result property="sex" column="ssex"/>
    18. collection>
    19. resultMap>
    20. mapper>

     运行结果与上面相同。

  • 相关阅读:
    2021最新Java笔试题及答案,Java面试真题精选
    从零开始写一个PHP开发框架websocket框架
    软考高级系统架构设计师系列案例考点专题六:面向服务架构设计
    如何空手套白狼?一口气省7K再抓住一个7K起步的工作?
    【面试系列】Vue
    Linux内存管理 | 一、内存管理的由来及思想
    高校校园网规划与设计
    FPGA片内ROM读写测试实验
    Nginx listen 监听端口详解
    FreeSWITCH对接vosk实现实时语音识别
  • 原文地址:https://blog.csdn.net/weixin_52473454/article/details/126045610