请老师答疑!
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
中
<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
中:
<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
中:
<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&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 {
// 一对多
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);
}
}
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.报错信息:
13
收起
正在回答
1回答
同学你好,
1、上述问题与之前同学反馈的问题,原因是一样的哦,resultMap是对外部ResultMap的引用,这里并不是引用,所以需要将student.xml中一对多的resultMap修改为resultType即可,如下所示:
2、同学的建表语句表名为classes,但代码中的查询表名为class,这里需要注意~
祝学习愉快~
相似问题
登录后可查看更多问答,登录/注册
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星