<select id="selectUser" //Method corresponding to PersonMapper.java interface parameterType="int" //Input parameter format resultType="hashmap" //Return data type. When multiple parameters are returned, resultMap is recommended. resultMap="userResultMap" //The data type returned is equivalent to encapsulating resultType="hashmap". There can only be one resultType and resultMap parameterMap="deprecated" //Waste, now use resultType and resultType flushCache="false" //Whether to empty the cache, default is false useCache="true" //Whether or not the secondary cache, default value: true for select elements. timeout="10000" //The number of seconds waiting for the database to return the result of the request fetchSize="256" statementType="PREPARED" // STATEMENT, one of PREPARED or CALLABLE. This allows MyBatis to use Statement, Prepared Statement or Callable Statement, respectively, with the default value of PREPARED. ...> <insert id="insertUser" parameterType="com.test.model.User" flushCache="true" statementType="PREPARED" timeout="20" keyProperty="" //(Useful only for inserts and update s) Only mark an attribute, MyBatis sets its key value by the return value of getGeneratedKeys or by the selectKey sub-element of the insert statement, default: unset. If you want to get multiple generated columns, you can also be a comma-separated list of attribute names. useGeneratedKeys="" //This will enable MyBatis to use JDBC's getGeneratedKeys method to extract the primary keys generated within the database (e.g. auto-incremental fields of relational database management systems such as MySQL and SQL Server), default value: false. keyColumn="" //(Useful only for insert s and update s) Setting column names in tables by generating key values is only necessary for certain databases (such as PostgreSQL), when the primary key column is not the first column in the table. If you want to get multiple generated columns, you can also be a comma-separated list of attribute names. > <update id="updateUser" parameterType="com.test.model.User" flushCache="true" statementType="PREPARED" timeout="20"> <delete id="deleteUser" parameterType="com.test.model.User" flushCache="true" statementType="PREPARED" timeout="20">
Note:
By default, the update operation return value of mybatis is the number of matched records, not the number of affected records.
If we want to return the number of affected records explicitly, we will modify our database connection configuration slightly by adding the useAffected Rows field:
url:jdbc:mysql://localhost:3306/mindspan?useAffectedRows=true
Notes:
1.Statement, PreparedStatement and Callable Statement are all interfaces.
2.Statement inherits from Wrapper, PreparedStatement from Statement, Callable Statement from PreparedStatement.
3.
The Statement interface provides a basic way to execute statements and obtain results.
The PreparedStatement interface adds methods to process IN parameters.
The CallableStatement interface adds methods for handling OUT parameters.
4.
a.Statement:
Ordinary query SQL without parameters; support batch updates, batch deletions;
b.PreparedStatement:
Variable parameter SQL, compiled once, executed many times, high efficiency;
It has good security and can effectively prevent Sql injection.
Support batch updating and deletion;
c.CallableStatement:
Inheriting from PreparedStatement, it supports SQL operations with parameters.
Supports calling stored procedures, providing support for output and input/output parameters (INOUT);
Every time a Statement executes an sql statement, the database executes the compilation of the sql statement.
It's better to use it when only one query is executed and the result is returned, which is more efficient than PreparedStatement.
PreparedStatement is precompiled, and there are several advantages to using PreparedStatement.
1. PreparedStatement is more efficient than Statement in executing a variable parameter SQL, because DBMS precompiling an SQL is certainly more efficient than compiling one SQL many times.
2. Good security and effective prevention of Sql injection.
3. For statements that are executed repeatedly, the use of PreparedState is a little more efficient, and in this case batch is more suitable;
4. Code readability and maintainability.