1、介绍
持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集
2、一般SQL调用
Class.forName("driver");// 1 Connection c = DriverManager.getConnection();// 2 Statement s = c.createStatement();ResultSet r = s.executeQuery("sql"); //或者 PreparedStatement pst = conn.prepareStatement(sql2);pst.setString(1,"8888");pst.setInt(2,198);pst.executeUpdate();// 3 r.close();c.close();// 4
3、调用逻辑
String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try {
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);
} finally {
session.close();
}