=============== MyBatis单元测试 =============== 在pom.xml中添加JUnit依赖: junit junit 4.13.2 test 在StudentDao类名后面点右键 生成... 测试... 勾选需要被测试的方法 public class StudentDaoTest { // 测试增加学生 @Test public void testAdd() { } // 测试删除学生 @Test public void testDel() { } } 测试增加学生: public class StudentDaoTest { ... // 测试增加学生 @Test public void testAdd() { // SQL会话工厂建造器 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); try { // MyBatis配置文件输入流 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); // SQL会话工厂 SqlSessionFactory factory = builder.build(is); // SQL会话 SqlSession session = factory.openSession(); // 数据访问对象 StudentDao dao = session.getMapper(StudentDao.class); // 学生实体 Student student = new Student(0, "1001", "张飞", "男", 21); // 增加学生 int rows = dao.add(student); // 打印受影响的行数 System.out.println(rows); // 提交事务 session.commit(); } catch (IOException e) { e.printStackTrace(); } } ... } 例程:HelloMyBatis ___________________________________ | mybatis-config.xml | |-----------------------------------| |MySQL:driver/url/username/password| | _______________________________ | | | StudentMapper.xml | | | |-------------------------------| | | | /add:insert into ...| | | |StudentDao | | | | \del:delete from ...| | | |_______________________________| | |___________________________________| | v InputStream | v SqlSessionFactoryBuilder | v SqlSessionFactory | v SqlSession | v ___________________________________ | StudentDao接口的实现类对象 | |-----------------------------------| | /add { ... } | | StudentDaoImpl | | \del { ... } | |___________________________________|