Summary of MyBatis for SSM (MYBATIS+SPRING+SPRINGMVC)
For the ORM Persistence framework has been used before the JDBC to connect to the database, for JDBC to connect to the library may have some shortcomings, then mybatis can only be considered semi-persistent framework, because mybatis need us to automatically write our SQL statements, We can do some comparisons with Jdbc&mybatis.
When we use JDBC, we perform some of the operations that frequently create connections and release connections to the database, affecting the performance of the entire system. So, for this, our mybatis is good at using database connection pools to manage our links, Column as we use the connection pool in the Sqlmapconfig.xml configuration file is a good solution to this problem so the connection pooling principle is to improve the performance of the production, the first time a connection to the database will be created a connection and then the request ends then the connection will not be closed, but into the connection pool waiting for the next connection, when There is a request to use the last connection to access the server directly then this omits the creation of the connection and the destruction of the connection to some extent improve the performance of the program
When we looked back at JDBC technology, we used JDBC to connect database SQL statements with StringBuffer and StringBuilder or string concatenation, which would be detrimental to our maintenance of the code. We all know that there are many changes in the actual application of the SQL statement mybatis. Put our SQL statements into the mapper mapping file in the dynamic spelling SQL then the SQL statement can improve performance to some extent and increase the scalability of the code.
MyBatis can also use @param annotations (more than two parameters to add @param annotations) to automatically map Java objects to our SQL statements, and mybatis to map the SQL statements we have executed to the object
MyBatis's execution can be divided into five steps: first create the sqlsessionfactory, through sqlsessionfactory create sqlsession, this time we can through the sqlsession to do some of the database operations, The session is then called to commit the transaction of the database, and the last Session.close () method closes the database
MyBatis first-level cache and level two cache
MyBatis first go to the cache to query the result set, if not query the database, if there is a cache out of the returned result set will not go to the database. The MyBatis internal storage cache uses a hashmap,key as a hashcode+sqlid+sql statement. Value is the Java object generated from the query-out mapping
MyBatis's Level Two cache is the query cache, which is scoped to a mapper namespace, where querying SQL in the same namespace can fetch data from the cache. A secondary cache can be cross-sqlsession.
Then the first-level cache is required to be enabled by default, and level two caches need to be manually opened.
MyBatis in SQL statements using #{} incoming parameters and ${} incoming parameter differences
Using #{} to pass in a parameter automatically adds a single quotation mark to the argument and ${} is the same as the argument directly to the SQL statement, #{} to a large extent to prevent SQL injection, ${} cannot prevent SQL injection, General ${} is used to pass objects such as the table name in the database, MyBatis sorting when using the order by dynamic parameter requires attention, with $ instead of #
The role of Namescape in MyBatis
In MyBatis, the mapping file in the namespace is used to bind the DAO interface, that is, we often say that interface-oriented programming, we through the namespace binding interface, do not need to write the interface implementation class, MyBatis will help you find the corresponding SQL statement to execute by automatically binding the function
Well, here we can also mention that the ID in our curd is consistent. Then find the corresponding SQL statement by ID
Output result type
Resulttype, fields and properties in Java objects are consistently recommended for use with Resulttype
Resultmap, using Resultmap when there are more inconsistent attributes in fields and Java objects
MyBatis Summary of SSM (MYBATIS+SPRING+SPRINGMVC)