|
|
|
|
@ -1,19 +1,14 @@
|
|
|
|
|
package com.ssm.test;
|
|
|
|
|
|
|
|
|
|
import com.ssm.entity.Department;
|
|
|
|
|
import com.ssm.entity.Major;
|
|
|
|
|
import com.ssm.entity.Student;
|
|
|
|
|
import com.ssm.entity.Notice;
|
|
|
|
|
import com.ssm.mapper.DepartmentMapper;
|
|
|
|
|
import com.ssm.mapper.MajorMapper;
|
|
|
|
|
import com.ssm.mapper.StudentMapper;
|
|
|
|
|
import com.ssm.mapper.NoticeMapper;
|
|
|
|
|
import com.ssm.entity.*;
|
|
|
|
|
import com.ssm.mapper.*;
|
|
|
|
|
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.Reader;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class TestMyBatis {
|
|
|
|
|
@ -23,33 +18,33 @@ public class TestMyBatis {
|
|
|
|
|
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
|
|
|
|
|
SqlSession sqlSession = factory.openSession();
|
|
|
|
|
|
|
|
|
|
// 2. 测试院系模块
|
|
|
|
|
// 2. 查询测试(之前写的)
|
|
|
|
|
testDepartment(sqlSession);
|
|
|
|
|
// 3. 测试专业模块
|
|
|
|
|
testMajor(sqlSession);
|
|
|
|
|
// 4. 测试学生模块
|
|
|
|
|
testStudent(sqlSession);
|
|
|
|
|
// 5. 测试公告模块
|
|
|
|
|
testNotice(sqlSession);
|
|
|
|
|
|
|
|
|
|
// 3. 添加测试(本次任务重点)
|
|
|
|
|
testAddDepartment(sqlSession);
|
|
|
|
|
testAddMajor(sqlSession);
|
|
|
|
|
testAddStudent(sqlSession);
|
|
|
|
|
testAddNotice(sqlSession);
|
|
|
|
|
|
|
|
|
|
sqlSession.close();
|
|
|
|
|
reader.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 院系模块测试
|
|
|
|
|
// ========== 查询方法(保留) ==========
|
|
|
|
|
private static void testDepartment(SqlSession sqlSession) {
|
|
|
|
|
DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
|
|
|
|
|
// 按ID查询
|
|
|
|
|
Department dept = mapper.getDepartmentById(1);
|
|
|
|
|
System.out.println("===== 院系模块 - 按ID查询 =====");
|
|
|
|
|
System.out.println(dept);
|
|
|
|
|
// 查询所有
|
|
|
|
|
List<Department> deptList = mapper.getDepartmentAll();
|
|
|
|
|
System.out.println("\n===== 院系模块 - 查询所有 =====");
|
|
|
|
|
deptList.forEach(System.out::println);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 专业模块测试
|
|
|
|
|
private static void testMajor(SqlSession sqlSession) {
|
|
|
|
|
MajorMapper mapper = sqlSession.getMapper(MajorMapper.class);
|
|
|
|
|
Major major = mapper.getMajorById(1);
|
|
|
|
|
@ -60,7 +55,6 @@ public class TestMyBatis {
|
|
|
|
|
majorList.forEach(System.out::println);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 学生模块测试
|
|
|
|
|
private static void testStudent(SqlSession sqlSession) {
|
|
|
|
|
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
|
|
|
|
|
Student student = mapper.getStudentById(1);
|
|
|
|
|
@ -71,7 +65,6 @@ public class TestMyBatis {
|
|
|
|
|
studentList.forEach(System.out::println);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 公告模块测试
|
|
|
|
|
private static void testNotice(SqlSession sqlSession) {
|
|
|
|
|
NoticeMapper mapper = sqlSession.getMapper(NoticeMapper.class);
|
|
|
|
|
Notice notice = mapper.getNoticeById(1);
|
|
|
|
|
@ -81,4 +74,63 @@ public class TestMyBatis {
|
|
|
|
|
System.out.println("\n===== 公告模块 - 查询所有 =====");
|
|
|
|
|
noticeList.forEach(System.out::println);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ========== 添加方法(本次任务重点) ==========
|
|
|
|
|
// 1. 添加院系
|
|
|
|
|
private static void testAddDepartment(SqlSession sqlSession) {
|
|
|
|
|
DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
|
|
|
|
|
// 新建对象
|
|
|
|
|
Department dept = new Department();
|
|
|
|
|
dept.setDeptName("软件工程学院");
|
|
|
|
|
dept.setDeptCode("SE011");
|
|
|
|
|
dept.setDeptPhone("024-11112222");
|
|
|
|
|
dept.setDeptAddress("实训楼E座2楼");
|
|
|
|
|
// 调用添加方法
|
|
|
|
|
mapper.addDepartment(dept);
|
|
|
|
|
// 提交事务(必须写!)
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
System.out.println("\n===== 院系模块 - 添加成功 =====");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. 添加专业
|
|
|
|
|
private static void testAddMajor(SqlSession sqlSession) {
|
|
|
|
|
MajorMapper mapper = sqlSession.getMapper(MajorMapper.class);
|
|
|
|
|
Major major = new Major();
|
|
|
|
|
major.setMajorName("人工智能");
|
|
|
|
|
major.setMajorCode("AI01");
|
|
|
|
|
major.setDeptId(1); // 所属院系ID,根据你的数据修改
|
|
|
|
|
major.setStudyYear(4);
|
|
|
|
|
mapper.addMajor(major);
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
System.out.println("===== 专业模块 - 添加成功 =====");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. 添加学生
|
|
|
|
|
private static void testAddStudent(SqlSession sqlSession) {
|
|
|
|
|
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
|
|
|
|
|
Student student = new Student();
|
|
|
|
|
student.setStudentNo("2023011");
|
|
|
|
|
student.setStudentName("新同学");
|
|
|
|
|
student.setGender("男");
|
|
|
|
|
student.setAge(20);
|
|
|
|
|
student.setClassId(1); // 所属班级ID,根据你的数据修改
|
|
|
|
|
student.setPhone("13800138011");
|
|
|
|
|
mapper.addStudent(student);
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
System.out.println("===== 学生模块 - 添加成功 =====");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. 添加公告
|
|
|
|
|
private static void testAddNotice(SqlSession sqlSession) {
|
|
|
|
|
NoticeMapper mapper = sqlSession.getMapper(NoticeMapper.class);
|
|
|
|
|
Notice notice = new Notice();
|
|
|
|
|
notice.setTitle("新增实训通知");
|
|
|
|
|
notice.setContent("下周将进行人工智能专业实训,请大家提前准备");
|
|
|
|
|
notice.setPublishTime(new Date());
|
|
|
|
|
notice.setPublisher("实训中心");
|
|
|
|
|
notice.setStatus(1);
|
|
|
|
|
mapper.addNotice(notice);
|
|
|
|
|
sqlSession.commit();
|
|
|
|
|
System.out.println("===== 公告模块 - 添加成功 =====");
|
|
|
|
|
}
|
|
|
|
|
}
|