|
|
|
|
@ -1,22 +1,84 @@
|
|
|
|
|
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 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;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class TestMyBatis {
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
// 1. 加载配置文件
|
|
|
|
|
Reader reader = Resources.getResourceAsReader("config.xml");
|
|
|
|
|
//SqlSession工厂对象
|
|
|
|
|
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
|
|
|
|
|
//SqlSession对象:代表了和数据库的一次对话
|
|
|
|
|
SqlSession session = factory.openSession();
|
|
|
|
|
System.out.println("MyBatis入门成功!");
|
|
|
|
|
//关闭链接
|
|
|
|
|
session.close();
|
|
|
|
|
SqlSession sqlSession = factory.openSession();
|
|
|
|
|
|
|
|
|
|
// 2. 测试院系模块
|
|
|
|
|
testDepartment(sqlSession);
|
|
|
|
|
// 3. 测试专业模块
|
|
|
|
|
testMajor(sqlSession);
|
|
|
|
|
// 4. 测试学生模块
|
|
|
|
|
testStudent(sqlSession);
|
|
|
|
|
// 5. 测试公告模块
|
|
|
|
|
testNotice(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);
|
|
|
|
|
System.out.println("\n===== 专业模块 - 按ID查询 =====");
|
|
|
|
|
System.out.println(major);
|
|
|
|
|
List<Major> majorList = mapper.getMajorAll();
|
|
|
|
|
System.out.println("\n===== 专业模块 - 查询所有 =====");
|
|
|
|
|
majorList.forEach(System.out::println);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 学生模块测试
|
|
|
|
|
private static void testStudent(SqlSession sqlSession) {
|
|
|
|
|
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
|
|
|
|
|
Student student = mapper.getStudentById(1);
|
|
|
|
|
System.out.println("\n===== 学生模块 - 按ID查询 =====");
|
|
|
|
|
System.out.println(student);
|
|
|
|
|
List<Student> studentList = mapper.getStudentAll();
|
|
|
|
|
System.out.println("\n===== 学生模块 - 查询所有 =====");
|
|
|
|
|
studentList.forEach(System.out::println);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 公告模块测试
|
|
|
|
|
private static void testNotice(SqlSession sqlSession) {
|
|
|
|
|
NoticeMapper mapper = sqlSession.getMapper(NoticeMapper.class);
|
|
|
|
|
Notice notice = mapper.getNoticeById(1);
|
|
|
|
|
System.out.println("\n===== 公告模块 - 按ID查询 =====");
|
|
|
|
|
System.out.println(notice);
|
|
|
|
|
List<Notice> noticeList = mapper.getNoticeAll();
|
|
|
|
|
System.out.println("\n===== 公告模块 - 查询所有 =====");
|
|
|
|
|
noticeList.forEach(System.out::println);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|