What is Mybatis
-
Mybatis is a semi-ORM framework, which encapsulates JDBC internally. When developing, you only need to care about the SQL statement itself, and do not need to care about the underlying implementation.
-
Mybatis can develop and map native information using XML or annotations to map POJOs to records in the database, eliminating almost all of the JDBC code and manual setting of parameters and fetching result sets
-
The statement to be executed is configured through XML files or annotations, and the final SQL statement is generated through mapping binding between Java objects and dynamic parameters of SQL statements in the statement. Finally, the framework encapsulates the returned results and maps them into objects for return.
What are the advantages and disadvantages of Mybatis?
Advantages:
-
SQL statement based programming, quite flexible, does not affect the existing design of the application and database, SQL written in XML reduces the coupling with Java code, easy to manage, provides dynamic tags can achieve dynamic SQL writing.
-
More than 50% less code than JDBC, eliminating a lot of redundant code
-
Integrates well with Spring
-
Provides label mapping and supports ORM field relational mapping between objects and databases
-
Support one-to-many, many-to-many query implementation
Disadvantages:
- A lot of SQL statements need to be written
What is the difference between #{} and ${}
#{} is precompiled processing, and ${} is string substitution
Mybatis will replace #{} with? Call the set method in PreparedStatement to assign
Mybatis processes ${} by replacing it with the value of a variable
#{} can effectively prevent SQL injection, improve system security
4. What if the attributes in the entity class are inconsistent with the fields in the table?
-
Aliases are mapped using query statements
-
Map by mapping field names and entity names
How is Mybatis paginated? How does paging plug-ins work?
Mybatis uses RowRounds object for paging, which is a memory paging for ResultsEt ResultsEt. It is not physical paging. You can directly write parameters with physical paging in SQL to complete paging function, or you can use paging plug-in to complete physical paging.
The principle of paging plug-in is to use the plug-in interface provided by Mybatis to achieve self-defined plug-in, intercept SQL in execution in the plug-in interception method, and then add the statement of physical paging to achieve paging function.
How does Mybatis encapsulate SQL results as target objects and return them? What are the mappings?
-
Labels are used for mapping, and the mapping between database field names and object attribute names is defined in ResultMap one by one
-
Use aliases for SQL columns for mapping
7. How many ways to achieve one-to-one in Mybatis? How to operate
-
There are federated queries
-
Nested query
Joint query is a query of several tables together, which is implemented by configuring association with ResultMap to configure a one-to-one class
Nested query is to query one table and then query data from another table based on the association relationship of the table. You can configure association and set search conditions for the query based on the ResultMap
Does Mybatis support lazy loading? If so, how does it work?
Mybatis support only association associated with collection collection objects of lazy loading, in Mybatis configuration file Settings lazyLoadingEnable = true | false
The idea is that cglib creates a proxy object for the target object, which enters the interceptor method when the target method is called.
Such as:
When a.geb () is NULL, the invoke() method will send a pre-saved SQL query to the associated object, query B, and then call A.geb (B), so that the object has a value. The a.geb ().getName() method is then called.
Mybatis level 1 cache, level 2 cache
-
Level 1 Cache: local Cache of HashMap based on PrepetualCache. This Cache is stored in the Session scope. When a Session is flushed or closed, all caches in this Session are flushed
-
Level 2 cache: Default Perpetuation and HashMap storage. The difference is that the storage functions as a Mapper(Namespace) and can be customized
Level 1 caching is enabled by default
Level-2 cache is not enabled by default. To enable level-2 cache, you can configure it in the mapping file and implement the Serializable interface