(**1) 去掉 Dao **接口实现类
    image.jpeg

    (**2getMapper **获取代理对象
    只需调用 SqlSessiongetMapper()``方法,即可获取指定接口的实现类对象。该方法的参数为指定 Dao接口类的 class 值。

    1. SqlSession session = factory.openSession();
    2. StudentDao dao = session.getMapper(StudentDao.class);

    使用工具类:

    1. StudentDao studentDao = MyBatisUtil.getSqlSession().getMapper(StudentDao.class);

    (**3) 使用 Dao 代理对象方法执行 sql **语句

    1. package com.wzy.dao;
    2. import com.wzy.pojo.Student;
    3. import com.wzy.utils.MyBatilsUtils;
    4. import org.apache.ibatis.io.Resources;
    5. import org.apache.ibatis.session.SqlSession;
    6. import org.apache.ibatis.session.SqlSessionFactory;
    7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    8. import org.junit.Test;
    9. import java.io.IOException;
    10. import java.io.InputStream;
    11. import java.text.ParseException;
    12. import java.text.SimpleDateFormat;
    13. import java.util.Date;
    14. import java.util.List;
    15. public class StudentDaoTest2 {
    16. @Test
    17. public void selectStudents() {
    18. //1.获取代理对象
    19. StudentDao studentDao = MyBatilsUtils.getSqlSession().getMapper(StudentDao.class);
    20. //2.调用查询方法
    21. List<Student> studentList = studentDao.selectStudents();
    22. //3.输出结果
    23. studentList.forEach(student -> System.out.println(student));
    24. }
    25. @Test
    26. public void insertStudent() throws ParseException {
    27. SqlSession sqlSession = null;
    28. try {
    29. //1.获取代理对象
    30. sqlSession = MyBatilsUtils.getSqlSession();
    31. StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
    32. //2.调用方法执行sql语句
    33. int result = studentDao.insertStudent(new Student(null, "莉莉", "女", 15,
    34. new SimpleDateFormat("yyyy-MM-dd").parse("2006-5-26"), null));
    35. //3.没有异常提交数据
    36. sqlSession.commit();
    37. System.out.println("成功插入了" + result + "条数据!");
    38. } catch (ParseException e) {
    39. e.printStackTrace();
    40. sqlSession.rollback();
    41. } finally {
    42. if (sqlSession != null) {
    43. sqlSession.close();
    44. }
    45. }
    46. }
    47. @Test
    48. public void upDateStudent() {
    49. SqlSession sqlSession = null;
    50. Date parse = null;
    51. try {
    52. sqlSession = MyBatilsUtils.getSqlSession();
    53. StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
    54. parse = new SimpleDateFormat("yyyy-MM-dd").parse("1996-8-20");
    55. int result = studentDao.updateStudent(new Student(13, "花花", "女", 25, parse, null));
    56. sqlSession.commit();
    57. System.out.println("成功修改了" + result + "条数据!");
    58. } catch (ParseException e) {
    59. e.printStackTrace();
    60. sqlSession.rollback();
    61. } finally {
    62. if (sqlSession != null) {
    63. sqlSession.close();
    64. }
    65. }
    66. }
    67. @Test
    68. public void deleteStudent() {
    69. SqlSession sqlSession = null;
    70. try {
    71. String config = "MyBatis.xml";
    72. InputStream resourceAsStream = Resources.getResourceAsStream(config);
    73. SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    74. sqlSession = factory.openSession();
    75. StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
    76. int result = studentDao.deleteStudent(13);
    77. sqlSession.commit();
    78. System.out.println("成功删除了" + result + "条记录!");
    79. } catch (IOException e) {
    80. e.printStackTrace();
    81. sqlSession.rollback();
    82. } finally {
    83. if (sqlSession != null) {
    84. sqlSession.close();
    85. }
    86. }
    87. }
    88. }