- 将数据库连接信息从config.xml移至db.properties配置文件 - 在config.xml中添加properties资源引用实现配置外部化 - 为Department、Major、Student、Notice四个模块添加resultMap查询方法 - 创建TestMyBatisQueryResultMap测试类验证resultMap查询功能 - 更新SQL映射文件添加resultMap定义和相关查询语句 - 实现基于resultMap的按ID查询和查询所有数据功能main
parent
e3fd2600e6
commit
8d65f1b081
@ -0,0 +1,83 @@
|
||||
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.Reader;
|
||||
import java.util.List;
|
||||
|
||||
public class TestMyBatisQueryResultMap {
|
||||
public static void main(String[] args) throws Exception {
|
||||
// 加载配置文件
|
||||
Reader reader = Resources.getResourceAsReader("config.xml");
|
||||
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
|
||||
SqlSession sqlSession = factory.openSession();
|
||||
|
||||
// 四个模块 resultMap 查询测试
|
||||
testDeptResultMap(sqlSession);
|
||||
testMajorResultMap(sqlSession);
|
||||
testStudentResultMap(sqlSession);
|
||||
testNoticeResultMap(sqlSession);
|
||||
|
||||
sqlSession.close();
|
||||
reader.close();
|
||||
}
|
||||
|
||||
// 院系 resultMap 查询
|
||||
private static void testDeptResultMap(SqlSession sqlSession) {
|
||||
DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
|
||||
Department dept = mapper.getDepartmentMapById(1);
|
||||
System.out.println("===== 院系 resultMap 按ID查询 =====");
|
||||
System.out.println(dept);
|
||||
|
||||
List<Department> deptList = mapper.getDepartmentMapAll();
|
||||
System.out.println("\n===== 院系 resultMap 查询所有 =====");
|
||||
deptList.forEach(System.out::println);
|
||||
}
|
||||
|
||||
// 专业 resultMap 查询
|
||||
private static void testMajorResultMap(SqlSession sqlSession) {
|
||||
MajorMapper mapper = sqlSession.getMapper(MajorMapper.class);
|
||||
Major major = mapper.getMajorMapById(1);
|
||||
System.out.println("\n===== 专业 resultMap 按ID查询 =====");
|
||||
System.out.println(major);
|
||||
|
||||
List<Major> majorList = mapper.getMajorMapAll();
|
||||
System.out.println("\n===== 专业 resultMap 查询所有 =====");
|
||||
majorList.forEach(System.out::println);
|
||||
}
|
||||
|
||||
// 学生 resultMap 查询
|
||||
private static void testStudentResultMap(SqlSession sqlSession) {
|
||||
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
|
||||
Student student = mapper.getStudentMapById(1);
|
||||
System.out.println("\n===== 学生 resultMap 按ID查询 =====");
|
||||
System.out.println(student);
|
||||
|
||||
List<Student> studentList = mapper.getStudentMapAll();
|
||||
System.out.println("\n===== 学生 resultMap 查询所有 =====");
|
||||
studentList.forEach(System.out::println);
|
||||
}
|
||||
|
||||
// 公告 resultMap 查询
|
||||
private static void testNoticeResultMap(SqlSession sqlSession) {
|
||||
NoticeMapper mapper = sqlSession.getMapper(NoticeMapper.class);
|
||||
Notice notice = mapper.getNoticeMapById(1);
|
||||
System.out.println("\n===== 公告 resultMap 按ID查询 =====");
|
||||
System.out.println(notice);
|
||||
|
||||
List<Notice> noticeList = mapper.getNoticeMapAll();
|
||||
System.out.println("\n===== 公告 resultMap 查询所有 =====");
|
||||
noticeList.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
driver=com.mysql.cj.jdbc.Driver
|
||||
url=jdbc:mysql://localhost:3306/ssm_score_sys?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true
|
||||
user=root
|
||||
password=123456
|
||||
Loading…
Reference in new issue