The framework of Mybatis learning notes (I ).
Summary of problems in original-ecology JDBC programming 1. Use jdbc alone to connect to the database
Maven dependency package:
<!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.31</version> </dependency>
Use Code
Package com. test; import java. SQL. *; public class jdbc {public static void main (String [] args) {// database Connection connection = null; // pre-compiled statementPreparedStatement preparedStatement = null; // result set ResultSet resultSet = null; try {// load the database Driver Class. forName ("com. mysql. jdbc. driver "); // obtain the database link through the Driver management class. the user name and password connection = DriverManager. getConnection ("jdbc: mysql: // 192.168.6.12: 3306/test", "dev", "mybatis"); // define an SQL statement ? String SQL = "select * from user where userName =? "; // Obtain the pre-processing statementpreparedStatement = connection. prepareStatement (SQL); // sets the parameter. The first parameter is the serial number of the parameter in the SQL statement, and the second parameter is the set parameter value preparedStatement. setString (1, "James"); // sends an SQL query to the database to execute the query. The query result set resultSet = preparedStatement.exe cuteQuery (); // traverses the result set while (resultSet. next () {System. out. println (resultSet. getString ("id") + "" + resultSet. getString ("username") ;}} catch (Exception e) {e. printStackTrace ();} finally {// release the resource if (resultSet! = Null) {try {resultSet. close ();} catch (Exception e) {e. printStackTrace ();}}}}}2. Problems Encountered
1). database connections are created when used, but are not released immediately. This causes frequent enabling and disabling of the database, resulting in a waste of database resources and reduced performance.
Solution: use the database connection pool to manage database links
2). hard-coded SQL statements into java code, and SQL statements are also written in the Code. If the SQL statement needs to be modified, the java code needs to be re-compiled, which is not conducive to system maintenance.
Solution: configure the SQL statement in the xml configuration file. Even if the SQL statement changes, you do not need to re-compile the java code.
3). Set the parameter to PreparedStatement, to the placeholder (?) Location and set parameter values are also hardcoded in java code. Not conducive to maintenance.
Solution: configure all SQL statements and placeholders in xml.
4). When traversing the result set data from the resutSet, there is hard encoding, and the obtained fields are hard-coded, which is not conducive to maintenance.
Solution: automatically maps the query result set to a java object.
Mybatis framework
Mybatis is a persistent layer architecture and a top-level project under apache. It enables programs to focus on SQL and generate freely and flexibly using the ing method provided by mybatis (semi-automated, most of the SQL statements that need to be compiled by programmers.
Mybatis can automatically map input parameters to the PreparedStatement, and flexibly map the query result set to a java object (output ing)
Mybatis code: https://github.com/mybatis/mybatis-3/releases
1. mybatis framework
Reference
Link: http://www.icoolxue.com/album/show/216
Deep understanding of mybatis principle: http://blog.csdn.net/column/details/mybatis-principle.html? Page = 1