MyBatis this is apache an open source project iBatis , 2010 Nian this project by the apache software foundation moved to Google code , and changed its name to MyBatis , essentially Mybatis to ibatis some improvements.
MyBatis is an excellent persistence framework, the process of its operation jdbc database encapsulation, so that developers only need to focus on SQL itself , without the need to spend energy to processing such as registration drive, create a connection, create a statement, manually set the parameters, retrieval result set jdbc complicated procedure code .
Various statement (statement, preparedStatemnt, CallableStatement) Mybatis xml or annotation by way arranged to be performed together, and the final map generation sql statement executed by a java object and the sql statement, sql and executed by the Last frame mybatis mapped into java objects and return.
- Analysis of the original eco-jdbc program Problems
- The original ecology J DBC program code
public static void main(String[] args) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { //1、加载数据库驱动 Class.forName("com.mysql.jdbc.Driver"); //2、通过驱动管理类获取数据库链接 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8", "root", "root"); //3、定义sql语句 ?表示占位符 String sql = "select * from user where username = ?"; //4、获取预处理statement preparedStatement = connection.prepareStatement(sql); //5、设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值 preparedStatement.setString(1, "王五"); //6、向数据库发出sql执行查询,查询出结果集 resultSet = preparedStatement.executeQuery(); //7、遍历查询结果集 while(resultSet.next()){ System.out.println(resultSet.getString("id")+" "+resultSet.getString("username")); } } catch (Exception e) { e.printStackTrace(); }finally{ //8、释放资源 if(resultSet!=null){ try { resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(preparedStatement!=null){ try { preparedStatement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
- The original ecology J DBC program code
-
- J DBC problem summary
- Frequent opening and closing the database connection, it will seriously affect the performance of the database.
- Hard-coded code is present, it is hard-coded portions and hard-coded database SQL execution section.