Interviewer: proficient Mybatis? Please answer these questions under

Point of attention, do not get lost; continually updated Java-related technologies and information architecture thermal paper! ! !

Mybatis is now very mainstream persistence framework, although usually with more, but few details of where the problem can not say what they were?

First, the most common parameter # {} and $ {} What is the difference?

1, points:

  • #{}It is a compilation process, to prevent SQL injection attacks.
  • ${}Static text is replaced, there is the risk of SQL injection attacks.

2, the details:

  • #{}Sql statement will be good to compile and use placeholders? , And then the values ​​(value converted to a string) to a large extent prevent sql injection. $ {} First value (without any treatment directly reference) and then to compile sql statement can not prevent sql injection, usually for incoming database objects, such as table names and the like.
  • #{}Sql parameter is a placeholder, Mybatis will in sql # {} with the? No, it will compile process. PreparedStatement parameter setting method will be used before the implementation of sql, sql sequentially to the? No. placeholder set parameter values. {} Is the Properties file placeholder variable, which can be a tag property value and the internal sql, a static text replacement.  such as{driver} will be replaced with a static com.mysql.jdbc.Driver.

Second, please explain the caching mechanism under Mybatis?

Cache persistence framework is a very important function, she can reduce the pressure on database access and improve application performance. Mybatis cache is divided into two: one cache and level two cache.

  • Level cache is SqlSession-level cache, the cached data is only valid for SqlSession.
  • Mapper is a secondary cache level cache, the same namespace public that a cache, so SqlSession is shared.

1, L1 cache

Mybatis level cache is SqlSession level cache, enabled by default. She is based PerpetualCache of HashMap local cache that stores scope to Session.

You need to create the database at the time of the operation SqlSession session object, there is a HashMap to store cached data in the object, this is the current session object HashMap private, other SqlSession session object inaccessible. Session after the flush or close, all of the buffer will be emptied of the Session.

Caching process:

  • select complete data is written HashMap will be found in the cache the first time in SqlSession execution.
  • The second will check the implementation of select data from the cache, if you select the same parameters and pass the same, then you can return data from the cache.

2, the secondary cache

Mapper Mybatis secondary cache level cache is, not turned on by default, to be manually configured. Its scope is stored Mapper, which is the same namespace of mappe.xml.

Two cache with a mechanism which is the same, also use the default PerpetualCache, HashMap stored, it is stored in a different scope as Mapper (Namespace), and may be custom storage source, such as Ehcache, Hazelcast like. When the same time using a plurality SqlSession Mapper database operation, the resulting data is cached in the secondary cache area of ​​the same.

Cache process:

  • When a sqlseesion perform a select, close the session at the time, the query results will be cached to the L2 cache.
  • When another sqlsession perform select, will first look at his own level cache, if not found, and he went looking for the second level cache, found a return, do not go to the database.

3, cache Notes

  • If SqlSession performed DML operations (insert, update, delete), and commit, then mybatis will erase all the current cache data SqlSession cache, so you can ensure that the data stored in the cache and the database is always consistent, avoid dirty read.
  • A cache enabled by default, does not require manual configuration; secondary cache is not turned on by default, to be manually configured.
  • mybatis cache is based on the [namespace: sql statement: parameter] to the cache, which means that when HashMap to store cached data SqlSession is to use [namespace: sql: parameter] as a key, returned by the query statement as a value saved.

Well, the time, today, the first record of two common issues now. Mybatis above two issues you are clear yet.

Guess you like

Origin www.cnblogs.com/Java-no-1/p/11025686.html