[Notes] MyBatis

MyBatis

1. Introduction to MyBatis

Is a Java-based persistence layer framework

  1. Support customized SQL, stored procedures and advanced mapping
  2. Avoid almost all JDBC code and manual setting of parameters and obtaining result sets
  3. You can use simple XML or annotations for configuration and original mapping, mapping interfaces and Java POJOs into records in the database

2. The difference between # and $ in Mybatis?

  1. # Treat the incoming data as a string, and add a double quotation mark to the automatically incoming data. For example: order by #user_id#, if the value passed in is 111, then the value when parsed into sql is order by "111"
  2. $ Directly displays the incoming data and generates it in sql. Such as: order by userid user_iduserid , if the value passed in is 111, then the value when parsed into sql is order by user_id
  3. The # method can prevent SQL injection to a large extent, and the $ method cannot prevent Sql injection.
  4. Don't use $ if you can generally use #

3. What are the programming steps of Mybatis?

  1. Create SqlSessionFactory
  2. Create SqlSession through SqlSessionFactory
  3. Perform database operations through sqlsession
  4. Call session.commit() to commit the transaction
  5. Call session.close() to close the session

4. What are the requirements when using the mapper interface of MyBatis to call?

  1. The Mapper interface method name is the same as the id of each SQL defined in mapper.xml
  2. The input parameter type of the Mapper interface method is the same as the parameterType of each SQL defined in mapper.xml
  3. The output parameter type of the Mapper interface method is the same as the resultType of each SQL defined in mapper.xml
  4. The namespace in the Mapper.xml file is the classpath of the mapper interface.

5. What is the first level cache and the second level cache in Mybatis?

  1. Level 1 cache: HashMap local cache based on PerpetualCache. Its storage scope is Session. After Session flush or
    close, all Cache in the Session will be emptied.
  2. The mechanism of the second-level cache is the same as that of the first-level cache. PerpetualCache and HashMap storage are also used by default. The difference is that the storage scope is
    Mapper (Namespace), and the storage source can be customized, such as Ehcache. The scope of namespance means that
    all select operation results in the configuration file corresponding to the namespance are cached, so that different threads can share the second-level cache.
    Start the secondary cache: in the mapper configuration file:.
    The second level cache can set the returned cache object strategy:. When readOnly = "true", represents the secondary cache to return
    back to the caller cache all instances of the same cache object instance, the caller can get the update, but this may cause other callers out
    inconsistencies current data situation (because all The caller is calling the same instance). When readOnly="false", what is returned to the caller is
    a copy of the total cache object in the secondary cache, that is, different callers get different instances of the cache object, so that the caller’s modification of the respective cache object will not
    affect Other callers are safe, so the default is readOnly="false";
  3. For the cache data update mechanism, when a C/U/D operation is performed in a certain scope (first-level cache Session/second-level cache Namespaces), by
    default, all the caches in select under this scope will be cleared.

6. MyBatis returns the primary key ID during the insert operation

When the database is MySql:

1. <insert id="insert" parameterType="com.test.User" keyProperty="userId" useGeneratedKeys="true" >

"KeyProperty" indicates that the returned id is to be stored in the property of the object, and "useGeneratedKeys" indicates that the primary key id is in self-growth mode.
The above configuration in MySQL is OK

7. What are the shortcomings of JDBC programming, and how does MyBatis solve these problems?

  1. Frequent database link creation and release cause waste of system resources and affect system performance. This problem can be solved if the database link pool is used.
    Solution: Configure the data link pool in SqlMapConfig.xml, and use the connection pool to manage database links.

  2. Sql statement written in the code makes the code difficult to maintain, and the actual application of sql may change greatly, and the change of sql needs to change the java code.
    Solution: Separate the Sql statement configuration from the java code in the XXXXmapper.xml file.

  3. Passing parameters to the sql statement is troublesome, because the where condition of the sql statement is not necessarily, there may be more or less, and the placeholders need to correspond to the parameters one-to-one.
    Solution: Mybatis automatically maps java objects to sql statements.

  4. It is troublesome to parse the result set. The sql change causes the parsing code to change, and it needs to be traversed before parsing. It is
    more convenient if the database records can be encapsulated into pojo objects for parsing.

    Solution: Mybatis automatically maps sql execution results to java objects.

Guess you like

Origin blog.csdn.net/qq_25046005/article/details/114890622