• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
  1. What is MyBatis?

    MyBatis is an open source framework that implements JPA specification to connect to the database and add, delete, change and check the operation (just like traditional JDBC, it is a connection to the database), in fact, it is a JDBC package component at the bottom.Copy the code
  2. What is the difference between #{} and ${}?Application scenarios

    #{} is precompiled processing, and ${} is string substitution. Mybatis will replace #{} with? Call the set method in PreparedStatement to assign the value. ${} = ${}; Using #{} can effectively prevent SQL injection and improve system security.Copy the code
  3. How do you pass multiple parameters in Mapper

    #{} in #{} corresponds to the name of the key in the Map. #{} in #{} corresponds to the name of the key in the Map. Method 4: The names in #{} correspond to member attributes in the User class.Copy the code
  4. Name MyBatis dynamic tags (at least 5)

    Foreach 2,concat fuzzy query 3, Choose (when,otherwise) tag 4,selectKey tag 5,if tagCopy the code
  5. Differences between parameterType and resultType

    ParameterType: parameterType is used to store information in a database, such as insert, add data to a database, zhong Update, etcCopy the code
  6. How do I get the primary key increment from the database when I insert with MyBatis?

    1. Use useGeneratedKeys, keyProperty, keyColumn in the INSERT tag; 2. Nested selectKey tag fetch in insert tag.Copy the code
  7. MyBatis batch delete/add when code implementation

    Delete from emp where empno in #{arr} INSERT INTO VALUES (#{content 1}, #{content 1}, #{content 1})Copy the code
  8. MyBatis dynamic SQL execution process and principle

    Select * from sqlSessionFactory; According to the Configuration file (global, SQL mapping) initialization out of the Configuration object parsing file every information saved in the Configuration, return the DefaultSqlSession containing the Configuration; SqlSession object returns a DefaultSQlSession object, which contains Executor and Configuration. This step creates an Executor object; Executor (according to the global defaultExecutorType create corresponding Executor in the configuration file) 3, obtain the proxy object of the interface (MapperProxy) DefaultSqlSession. GetMapper () : Get the MapperProxy corresponding to the Mapper interface. Use MapperProxyFactory to create a proxy object for MapperProxy that contains, Call Executor for DefaultSqlSession (Executor); 2) A StatementHandler object is created. ParameterHandler and ResultSetHandler are also created for ParameterHandler and ResultSetHandler. ParameterHandler is used to set parameters to SQL when ParameterHandler is used. 5) ResultSetHandler encapsulates the resultsCopy the code

JDBC and Mybatis performance efficiency which block? why

JDBC is faster than MyBatis. 2. Mybatis is further encapsulated in JDBC and needs to map and load configuration filesCopy the code

What design patterns are used in the MyBatis framework?

Factory mode: for example, sqlSessionFactory 2. Proxy mode: the core of mybatis implementation, such as MapperProxy JDK dynamic proxy 3. Template method patterns: For example, BaseExecutor and SimpleExecutorCopy the code
  1. MyBatis level 1 cache and level 2 cache difference

    Level 1 cache:

    Mybatis only enables level 1 caching by default, level 1 caching only relative to the same SqlSession. Mybatis will cache the first query result only when the parameters and SQL are exactly the same and the same SqlSession is used. The subsequent query with the same SqlSession will hit the cache instead of directly checking the library

    Level 2 cache:

    Level-1 cache does not match the cache when different SQLSessions are used. That is, level-1 cache must have SqlSession and the parameters must be the same as those in Sql

    The level 2 cache needs to be manually configured so that the cache can be shared by each SqlSession at the SqlSessionFactory level

    Level 2 cache can provide matching for queries with the same parameters, same Sql statements, and then different SQLSessions