实验四 关联关系映射
实验步骤实验目的
了解数据表之间以及对象之间的三种关联关系;
掌握一对一、一对多、多对多的关联关系映射的配置。
实验内容
建学生数据库,创建院系表和学生信息表,院系表中存储院系编号,院系名称,
学生信息表中存储学生学号、姓名、性别、出生日期、籍贯、专业、所属院系等信息,在2个表中输入若干数据。
创建Java web项目,配置一对多的关联关系,插入一个学生数据,并且查询所有计算机学院的学生。
- 1、创建maven项目,在pom.xml文件中配置依赖
- 2、新建实体类Faculty
- 3、新建实体类Student
- 4、在resources下建db.properties、mybatis-config.xml文件。注意要在mybatis-config.xml文件中引入db.properties文件
- 5、在src.main.resources中建包com.mapper,包中建StudentMapper接口。
- 6、在mybatis-config.xml文件中注册StudentMapper.xml文件
- 7、在src.main.resources中建包com.mapper,包中建FacutlyMapper接口。
- 8、在mybatis-config.xml文件中注册FacutlyMapper.xml文件
- 9、在Test.java目录中创建测试类。
- 10、MybatisUtils
- 11、运行测试方法。
2、新建实体类Faculty4.0.0 org.example sy2 1.0-SNAPSHOT org.mybatis mybatis 3.5.2 mysql mysql-connector-java 8.0.11 junit junit 4.12 test 8 8 src/main/java ***.properties true src/main/resources ***.properties true
public class Faculty {
public Integer yxid;
public String yxname;
public Faculty(){}
public Faculty(Integer yxid,String yxname){
this.yxid=yxid;
this.yxname=yxname;
}
public Integer getYxid() {
return yxid;
}
public void setYxid(Integer yxid) {
this.yxid = yxid;
}
public String getYxname() {
return yxname;
}
public void setYxname(String yxname) {
this.yxname = yxname;
}
@Override
public String toString() {
return "faculty{" +
"yxid=" + yxid +
", yxname='" + yxname + ''' +
'}';
}
}
3、新建实体类Student
public class Student {
public int xsid;
public String xsname;
public String sex;
public String birthplace;
public String birth;
public String major;
public String yxname;
public List facultiesList;
public Student(){}
public Student(int xsid,String xsname,String sex,String birthplace,String birth, String major, String yxname) {
this.xsid = xsid;
this.xsname=xsname;
this.sex=sex;
this.birthplace=birthplace;
this.birth=birth;
this.major = major;
this.yxname=yxname;
}
public int getXsid() {
return xsid;
}
public void setXsid(int xsid) {
this.xsid = xsid;
}
public String getXsname() {
return xsname;
}
public void setXsname(String xsname) {
this.xsname = xsname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthplace() {
return birthplace;
}
public void setBirthplace(String birthplace) {
this.birthplace = birthplace;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getYxname() {
return yxname;
}
public void setYxname(String yxname) {
this.yxname = yxname;
}
@Override
public String toString() {
return "student{" +
"xsid=" + xsid +
", xsname='" + xsname + ''' +
", sex='" + sex + ''' +
", birthplace='" + birthplace + ''' +
", birth='" + birth + ''' +
", major='" + major + ''' +
", yxname='" + yxname + ''' +
'}';
}
}
4、在resources下建db.properties、mybatis-config.xml文件。注意要在mybatis-config.xml文件中引入db.properties文件
//db.properties mysql.driver=com.mysql.cj.jdbc.Driver mysql.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false mysql.username=root mysql.password=123456 //mybatis-config.xml5、在src.main.resources中建包com.mapper,包中建StudentMapper接口。
import pojo.Student;
import java.util.List;
public interface StudentMapper {
int addStudent(Student newstudent) ;
List findStudentByFaculty(Student newstudent);
}
6、在mybatis-config.xml文件中注册StudentMapper.xml文件
7、在src.main.resources中建包com.mapper,包中建FacutlyMapper接口。insert into tb_newstudent(xsid,xsname,sex,birthplace,birth,major,yxname) values (#{xsid},#{xsname},#{sex},#{birthplace},#{birth},#{major},#{yxname}) select u.* from tb_newstudent u,tb_faculty o where u.yxname=o.yxname and u.yxname=#{yxname};
import pojo.Faculty;
public interface FacultyMapper {
int addFaculty(Faculty newfaculty) ;
}
8、在mybatis-config.xml文件中注册FacutlyMapper.xml文件
9、在Test.java目录中创建测试类。insert into tb_faculty(yxid,yxname) values (#{yxid},#{yxname})
public class Test {
@org.junit.Test
public void facultyAddTest(){
SqlSession session= MybatisUtils.getSqlsession();
FacultyMapper mapper = session.getMapper(FacultyMapper.class);
Faculty u1=new Faculty();
u1.setYxid(104);
u1.setYxname("外国语学院");
int n= mapper.addFaculty(u1);
session.commit();
if(n>0) System.out.println("ok,新添加的学院为"+u1.yxname+"学院ID为:"+u1.yxid);
else System.out.println("error");
//关闭session
session.close();
}
@org.junit.Test
public void studentAddTest(){
SqlSession session= MybatisUtils.getSqlsession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student u1=new Student();
u1.setXsid(204);
u1.setXsname("小飞机");
u1.setSex("女");
u1.setBirthplace("安阳");
u1.setBirth("1999-02");
u1.setMajor("国家英语");
u1.setYxname("外国语学院");
int n= mapper.addStudent(u1);
session.commit();
if(n>0) System.out.println("ok,新添加的学生为"+u1);
else System.out.println("error");
//关闭session
session.close();
}
@org.junit.Test
public void findStudentTest() {
SqlSession session = MybatisUtils.getSqlsession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student s1 = new Student();
s1.setYxname("计算机学院");
List students = mapper.findStudentByFaculty(s1);
for (Student s : students) {
System.out.println(s);
}
session.close();
}
}
10、MybatisUtils
package com.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.InputStream;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try
{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
catch(IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlsession(){return sqlSessionFactory.openSession();}
}
11、运行测试方法。
1.初始数据
2.插入一个学生
3.查询所有计算机学院的学生



