在接口名称上按ctrl + shift+ t 快捷创建测试类.
    image.png

    1. package com.way.dao;
    2. import javabean.Student;
    3. import org.apache.ibatis.session.SqlSession;
    4. import org.apache.ibatis.session.SqlSessionFactory;
    5. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    6. import org.junit.Test;
    7. import org.apache.ibatis.io.Resources;
    8. import java.io.IOException;
    9. import java.io.InputStream;
    10. import java.util.List;
    11. public class StudentDaoTest {
    12. @Test
    13. public void queryStudentList() throws IOException {
    14. //1.mybatis的配置主文件名称
    15. String config = "mybatisConfig.xml";
    16. //2、读取mybatis.xml得到字节输入流,
    17. // Resources从类路径中加载资源。
    18. //getResourceAsStream读取的文件得到输入流
    19. InputStream inputStream = Resources.getResourceAsStream(config);
    20. //3、创建一个数据库会话工厂并调用它的建造方法,建造方法调用到读取资源的对象,并且赋值给了数据库工厂
    21. SqlSessionFactory builder = new SqlSessionFactoryBuilder().build(inputStream);
    22. //4、用sqlsession工厂对象打开session会话,获得session对象,得到sql语句执行类。
    23. SqlSession sqlSession = builder.openSession();
    24. //5、执行sql语句
    25. List<Object> list = sqlSession.selectList("com.way.dao.StudentDao.queryStudentList");
    26. //6、打印输出
    27. list.forEach(student1-> System.out.println(student1));
    28. //7、释放资源
    29. sqlSession.close();
    30. }
    31. @Test
    32. public void insertStudent() throws IOException {
    33. InputStream inputStream = null;
    34. SqlSessionFactory sessionFactory = null;
    35. SqlSession sqlSession = null;
    36. try {
    37. //1、读取mybatisConfig.xml配置文件(日志、数据库的连接、StudentDao的mapper配置文件)
    38. inputStream = Resources.getResourceAsStream("mybatisConfig.xml");
    39. //2、创建一个数据库会话工厂并调用它的建造方法,建造方法调用到读取资源的对象,并且赋值给了数据库工厂
    40. sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    41. //3、得到sql语句执行类
    42. sqlSession = sessionFactory.openSession();
    43. //4、创建JavaBean类,
    44. //Student student = new Student();
    45. //5、执行slq语句
    46. int insert = sqlSession.insert("com.way.dao.StudentDao.insertStudent",
    47. new Student(4,"李武","5544@1360.com",30));
    48. sqlSession.commit();
    49. System.out.println(insert);//1
    50. } catch (IOException e) {
    51. e.printStackTrace();
    52. sqlSession.rollback();
    53. }finally {
    54. if (sqlSession != null) {
    55. sqlSession.close();
    56. }
    57. }
    58. }
    59. @Test
    60. public void updateSudent() throws IOException {
    61. InputStream inputStream = null;
    62. SqlSessionFactory sessionFactory = null;
    63. SqlSession sqlSession = null;
    64. try {
    65. //1、读取mybatisConfig.xml主配置文件
    66. inputStream = Resources.getResourceAsStream("mybatisConfig.xml");
    67. //2、
    68. sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    69. sqlSession = sessionFactory.openSession();
    70. int update = sqlSession.update("com.way.dao.StudentDao.updateSudent",
    71. new Student(3, "王五", "1565@qq.com", 22));
    72. sqlSession.commit();
    73. } catch (IOException e) {
    74. e.printStackTrace();
    75. sqlSession.rollback();
    76. } finally {
    77. if (sqlSession != null) {
    78. sqlSession.close();
    79. }
    80. }
    81. }
    82. @Test
    83. public void deleteStudnet() {
    84. SqlSession sqlSession = null;
    85. try {
    86. InputStream inputStream = Resources.getResourceAsStream("mybatisConfig.xml");
    87. SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
    88. sqlSession = build.openSession();
    89. int delete = sqlSession.delete("com.way.dao.StudentDao.deleteStudnet", 4);
    90. sqlSession.commit();
    91. System.out.println(delete);
    92. } catch (IOException e) {
    93. e.printStackTrace();
    94. sqlSession.rollback();
    95. } finally {
    96. if (sqlSession != null) {
    97. sqlSession.close();
    98. }
    99. }
    100. }
    101. }