实现多表查询 一对多关系 -
1)在plsql developer工具中编写多表查询的语句,正确查询到学生和课程信息;
2)在实体类Student中添加课程属性,类型是List,重新生成getter and setter、无参有参构造、toString();
3)在StudentMapper中编写,写入多表查询语句;使用resultMap指定结果集映射;
编写新的结果集映射resultMap,返回类型是Student类路径。其中包含学生与课程的字段-属性映射关系,通过使用
包含课程的字段-属性映射关系;
4)创建dao层接口,编写抽象方法,方法名与mapper中id一致;
5)创建service层接口和实现类,在实现类中创建dao层对象并调用多表查询方法;
6)编写测试类,创建service层对象并调用多表查询方法,输出返回结果,查看学生和课程信息。
subjectmapper代码
?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="com.gxy.dao.IStudentDao">
<!--启用缓存功能-->
<!--通过结果集映射 表中字段名称与实体类属性名称对应-->
<resultMap id="BaseResultMap" type="com.gxy.dao.SubjectMapper.xml">
<!--配置主键列 column列名 property-->
<id column="sub_id" property="id"></id>
<!--配置非空主键-->
<result column="sub_name" property="name"></result>
<result column="stu_id" property="stuid"></result>
</resultMap>
<!--查询list-->
<select id="selectSubject" resultMap="BaseResultMap">
select * from subject
</select>
</mapper>
主要是studentmapper
代码
<!--通过结果集映射 表中字段名称与实体类属性名称对应-->
<resultMap id="BaseResultMap" type="com.gxy.dao.SubjectMapper.xml">
<!--配置主键列 column列名 property-->
<id column="sub_id" property="id"></id>
<!--配置非空主键-->
<result column="sub_name" property="name"></result>
<result column="stu_id" property="stuid"></result>
</resultMap>
<!--查询list-->
<select id="selectSubject" resultMap="BaseResultMap">
select * from subject
</select></mapper>
student 实体类代码
package com.gxy.entity;
import java.util.List;
/**
-
Created by lenovo on 2021/4/9.
*/
public class Student {
private int id;
private String name;
private int age;
private String gender;
private int teaid;
private String date;
private Teacher teacher;
private List subjects;public Student() {
}public Student(int id, String name, int age, String gender, int teaid, String date, Teacher teacher, List subjects) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.teaid = teaid;
this.date = date;
this.teacher = teacher;
this.subjects = subjects;
}public int getId() {
return id;
}public void setId(int id) {
this.id = id;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public int getAge() {
return age;
}public void setAge(int age) {
this.age = age;
}public String getGender() {
return gender;
}public void setGender(String gender) {
this.gender = gender;
}public int getTeaid() {
return teaid;
}public void setTeaid(int teaid) {
this.teaid = teaid;
}public String getDate() {
return date;
}public void setDate(String date) {
this.date = date;
}public Teacher getTeacher() {
return teacher;
}public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}public List getSubjects() {
return subjects;
}public void setSubjects(List subjects) {
this.subjects = subjects;
}@Override
public String toString() {
return “Student{” +
“id=” + id +
“, name=’” + name + ‘’’ +
“, age=” + age +
“, gender=’” + gender + ‘’’ +
“, teaid=” + teaid +
“, date=’” + date + ‘’’ +
“, teacher=” + teacher +
“, subjects=” + subjects +
‘}’;
}@Override
public int hashCode() {
return super.hashCode();
}
}
dao service接口方法
public List selectOne2more();
Impl
SqlSession session = MyBatisUtil.getSqlSession();
//创建dao层对象 代理模式(反射)
IStudentDao dao = session.getMapper(IStudentDao.class);
//调用dao层提供的方法
List list = dao.selectOne2more() ;
//关闭资源
MyBatisUtil.close();
return list;
测试类
List<Subject> b=i.selectOne2more();
System.out.println(b);