请老师答疑!

请老师答疑!

工程:maven-homework

1.班级表

 CREATE TABLE classes(
  id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
  class_no VARCHAR(20) NOT NULL,
  name VARCHAR(20),
  major VARCHAR(20) NOT NULL
 );
 
 DROP TABLE classes;
 
 
 INSERT INTO classes
 VALUES
 (1,"Class001","一班","计算机"),
 (2,"Class002","二班","计算机"),
 (3,"Class003","三班","会计"),
 (4,"Class004","三班","会计");


2.学生表

 
 CREATE TABLE student(
  id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
  stuno VARCHAR(20) NOT NULL,
  stuname VARCHAR(20) NOT NULL,
  sex VARCHAR(10) NOT NULL,
  class_no VARCHAR(20) NOT NULL
 );
 
 DROP TABLE student;
 
 INSERT INTO student
 VALUES
 (1,"20181101","梅长苏","男","Class001"),
 (2,"20181102","萧景琰","男","Class001"),
 (3,"20181103","宫羽","女","Class001"),
 (4,"20181201","霓凰","女","Class003");
 



3.班级实体类

 package com.imooc.mybatis.entity;
 
 import java.util.List;
 
 public class Class {
 
     private Integer id;
     private String classNo;
     private String name;
     private String major;
     private List<Student> studentsDetails;   // 对象关联
 
     public Integer getId() {
         return id;
     }
 
     public void setId(Integer id) {
         this.id = id;
     }
 
     public String getClassNo() {
         return classNo;
     }
 
     public void setClassNo(String classNo) {
         this.classNo = classNo;
     }
 
     public String getName() {
         return name;
     }
 
     public void setName(String name) {
         this.name = name;
     }
 
     public String getMajor() {
         return major;
     }
 
     public void setMajor(String major) {
         this.major = major;
     }
 
     public List<Student> getStudentsDetails() {
         return studentsDetails;
     }
 
     public void setStudentsDetails(List<Student> studentsDetails) {
         this.studentsDetails = studentsDetails;
     }
 }
 

4.学生实体类

 package com.imooc.mybatis.entity;
 
 public class Student {
 
     private Integer id;
     private String stuno;
     private String stuname;
     private String sex;
     private String classno;
 
     private Class aClass;   // 对象关联
 
 
 
     public Integer getId() {
         return id;
     }
 
     public void setId(Integer id) {
         this.id = id;
     }
 
     public String getStuno() {
         return stuno;
     }
 
     public void setStuno(String stuno) {
         this.stuno = stuno;
     }
 
     public String getStuname() {
         return stuname;
     }
 
     public void setStuname(String stuname) {
         this.stuname = stuname;
     }
 
     public String getSex() {
         return sex;
     }
 
     public void setSex(String sex) {
         this.sex = sex;
     }
 
     public String getClassno() {
         return classno;
     }
 
     public void setClassno(String classno) {
         this.classno = classno;
     }
 
     public Class getaClass() {
         return aClass;
     }
 
     public void setaClass(Class aClass) {
         this.aClass = aClass;
     }
 }

5.在student.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="student">
 
     <!--作为一对多的素材-->
     <!--在学生表中,根据班级编号找到该班级所拥有的所有学生-->
     <select id="selectByClassNo" parameterType="String" resultMap="com.imooc.mybatis.entity.Student">
         select * from student where class_no=#{value}
     </select>
 
 
 
 
     <!--多对一-->
     <resultMap id="rmStudentDetail" type="com.imooc.mybatis.entity.Student">
         <id column="class_no" property="classNo"/>
         <association property="aclass" select="class.selectByClassNo" column="class_no"></association>
     </resultMap>
 
     <select id="selectManyToOne" resultMap="rmStudentDetail">
         select * from student
     </select>
 
 </mapper>


6.在class.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="class">
 
     <!--一对多-->
     <resultMap id="rmClass" type="com.imooc.mybatis.entity.Class">
         <id column="id" property="id"/>
         <collection property="studentsDetails" select="student.selectByClassNo" column="class_no"/>  <!--将之前查询得到的符合班级编号的所有学生,收集到这里,放进班级的一个属性studentsDetails里-->
     </resultMap>
     <select id="selectOneToMany" resultMap="rmClass">
         select * from class
     </select>
 
 
 
     <!--作为多对一的素材:在class表中找到班级编号是1的班级对象,放进list列表中-->
     <select id="selectByClassNo" parameterType="String" resultType="com.imooc.mybatis.entity.Class">
         select * from class where class_no=#{value}
     </select>
 
 </mapper>

7.在核心配置文件mybatis-config.xml中:

 <?xml version="1.0" encoding="UTF-8" ?>
 
 <!DOCTYPE configuration
         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 
 <configuration>
 
 
     <settings>
         <!--比如数据表的字段名reg_no转换为regNo,实现驼峰命名转换-->
         <setting name="mapUnderscoreToCamelCase" value="true"/>
     </settings>
 
 
     <!--设置默认指向的数据库,便捷切换-->
     <environments default="dev">
         <environment id="dev">
             <transactionManager type="JDBC"></transactionManager>
             <dataSource type="POOLED">
                 <property name="driver" value="com.mysql.jdbc.Driver"/>
                 <property name="url" value="jdbc:mysql://localhost:3306/imooc?useUnicode=true&amp;characterEncoding=UTF-8"/>
                 <property name="username" value="root"/>
                 <property name="password" value="1234abcd"/>
             </dataSource>
         </environment>
     </environments>
 
 
     <mappers>
         <mapper resource="mappers/class.xml"/>
         <mapper resource="mappers/student.xml"/>
     </mappers>
 
 
 </configuration>


8.在类MyBatisUtils.java中:

 package com.imooc.mybatis.utils;
 
 
 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;
 
 /**
  * 该类用于创建全局唯一的SqlSessionFactory对象
  */
 public class MyBatisUtils {
 
     // 静态的变量,即类变量
     private static SqlSessionFactory sqlSessionFactory = null;
 
     // 静态的代码块,用于创建sqlSessionFactory对象,提取重复
     static {
         Reader reader = null;
         try {
             reader = Resources.getResourceAsReader("mybatis-config.xml");
             sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
         } catch (IOException e) {
             e.printStackTrace();
             new ExceptionInInitializerError(e);
         }
     }
 
     // 静态的类方法1,用于打开会话
     public static SqlSession openSession(){                     // 返回的是SqlSession
         return sqlSessionFactory.openSession();
     }
 
     // 静态的类方法2,用于关闭会话
     public static void closeSession(SqlSession sqlSession) {    // 传入的是返回的是SqlSession
         sqlSession.close();
     }
 
 }


9.在单元测试类中:

 package com.imooc.mybatis;
 
 import com.imooc.mybatis.entity.Class;
 import com.imooc.mybatis.entity.Student;
 import com.imooc.mybatis.utils.MyBatisUtils;
 import org.apache.ibatis.session.SqlSession;
 import org.junit.Test;
 
 import java.util.*;
 
 // 单元测试类
 public class MyBatisTestor {
 
     // 一对多
     @Test
     public void testOneToMany(){
         SqlSession session = null;
         try {
             session = MyBatisUtils.openSession();
             List<Class> list = session.selectList("class.selectOneToMany");
 
             for (Class cls : list) {
                 System.out.println(cls.getName()+":"+cls.getStudentsDetails().size());
             }
             
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             //MyBatisUtils.closeSession(session);
         }
     }
 
 
 
     @Test
     public void testManyToOne() throws Exception {
         SqlSession session = null;
         try {
             session = MyBatisUtils.openSession();
             List<Student> list = session.selectList("student.selectManyToOne");
             for (Student student : list) {
                 System.out.println(student.getStuname() + ":" + student.getaClass().getClassNo());
             }
         } catch (Exception e) {
             throw e;
         } finally {
             MyBatisUtils.closeSession(session);
         }
     }
 
 }


10.报错信息:

12.2097


12.2098



正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,

1、上述问题与之前同学反馈的问题,原因是一样的哦,resultMap是对外部ResultMap的引用,这里并不是引用,所以需要将student.xml中一对多的resultMap修改为resultType即可,如下所示:

https://img1.sycdn.imooc.com//climg/631860f909453d0c11960183.jpg

2、同学的建表语句表名为classes,但代码中的查询表名为class,这里需要注意~

https://img1.sycdn.imooc.com//climg/6318619e09ec467708330545.jpg

祝学习愉快~

  • Jerry_Zheng24 提问者 #1

    老师好:

    1.resultMap是对外部ResultMap的引用,而resultType就是指结果属于具体哪个类。改好后,程序正常运行了

    2.已改。

    非常感谢!帮助很大。

    2022-09-07 18:06:45
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师