减轻使用 JDBC 的复杂性,不用编写重复的创建 Connetion , Statement ; 不用编写关闭资源代码。直接使用 java 对象,表示结果数据。让开发者专注 SQL 的处理。 其他分心的工作由 MyBatis 代劳。

    MyBatis 可以完成:

    1. 注册数据库的驱动,例如 Class.forName(“com.mysql.jdbc.Driver”))。
    2. 创建 JDBC 中必须使用的 Connection , Statement, ResultSet 对象。
    3. 从 xml 中获取 sql,并执行 sql 语句,把 ResultSet 结果转换 java 对象。
    List<Student> list = new ArrayLsit<>();
    
    ResultSet rs = state.executeQuery(“select * from student”);
    
    while(rs.next){
    
     Student student = new Student();
    
     student.setName(rs.getString(“name”));
    
     student.setAge(rs.getInt(“age”));
    
     list.add(student);
    
    }
    4.关闭资源
    
    ResultSet.close() , Statement.close() , Conenection.close()