Today’s sharing started, please give us more advice ~
Summary of MyBatis interview questions
Preface:
Today I would like to share with you some questions that you will encounter in the interview. I hope this sharing can make the interview more smooth!
1.#{} and ${}?
2. The Xml mapping file, in addition to the common select/Inserct/update/delete tags, and the labels?
3. As a best practice, an Xml mapping file will usually have a Dao interface corresponding to it. How does this Dao interface work? Can methods in the Dao interface be overloaded if their parameters are different?
4. How is Mybatis paginated? How does paging plug-ins work?
5. What does Mybatis dynamic SQL do? What is the dynamic SQL? Can you briefly explain how dynamic SQL is executed?
6. How does Mybatis encapsulate SQL execution results as target objects and return them? What are the mappings?
7. Can Mybatis implement one-to-one or one-to-many associated query? What are the implementation methods and the differences between them?
8. Does Mybatis support lazy loading? If so, how does it work?
9. In the XML mapping files of Mybatis, can the IDS of different XML mapping files be repeated?
10. What Executor executors does Mybatis have? What’s the difference between them?
11. How to specify which Executor to use in Mybatis?
12. Can Mybatis map Enum classes?
13. In Mybatis mapping file, if tag A references the content of tag B through include, is the content of tag B defined after tag A or must be defined before tag A?
What is the difference between Mybatis and Spring dataJpa?
Without further ado, the body begins ~
1.#{} and ${}
#{} : is the parameter placeholder of SQL, Mybatis will replace the #{} of SQL with? , in order to SQL? Placeholders set parameter values.
This is based on parameters, not dynamic SQL (SQL with if statements and so on)
{}: is a variable placeholder in the database configuration file and belongs to static text replacement. {}: is a variable placeholder in the database configuration file and belongs to static text replacement. For example, {Driver} is statically replaced with com.mysql.jdbc.driver.
2. The Xml mapping file, in addition to the common select/Inserct/update/delete tags, and the labels?
Also,,,,, plus 9 tags for dynamic SQL etc. It is possible to introduce additional SQL fragments to generate policy labels for primary keys that do not support self-increment.
3. As a best practice, an Xml mapping file will usually have a Dao interface corresponding to it. How does this Dao interface work? Can methods in the Dao interface be overloaded if their parameters are different?
Dao interface is also known as Mapper interface. The full name of the interface is the value of namespace in the mapping file, the method name of the interface is the ID value of MappedStatement in the mapping file, and the parameters in the interface method are the parameters passed to SQL. The Mapper interface has no implementation class. When an interface method is called, the interface full name + method name concatenation string is used as the key value to uniquely locate an MappedStatement.
Methods in the Dao interface cannot be overridden because of the save and find policy of the full name + method name.
The working principle of Dao interface is JDK dynamic proxy. When MyBatis runs, it uses JDK dynamic proxy to generate proxy proxy for Dao interface. Proxy object Proxy intercepts interface methods, executes SQL represented by MappedStatement, and returns SQL execution results.
4. How is Mybatis paginated? How does paging plug-ins work?
1.Mybatis uses the RowBounds object for paging, which is memory separation for ResultSet result sets rather than physical paging;
RowBounds paging (not USING SQL), which is no longer implemented using SQL
interface
List <User> getUserByRowBound();
Copy the code
mapper.xml
test
2. Directly write limit in SQL for paging;
Use Limit paging
Select * from user limit 2; #[0,n]Copy the code
[0,2] came out first and second. So 0 is the first one.
3. Use the paging plug-in for physical paging.
5. What does Mybatis dynamic SQL do? What is the dynamic SQL? Can you briefly explain how dynamic SQL is executed?
Mybatis dynamic SQL allows us to compile dynamic SQL in the form of tags in XML mapping files, complete logical judgment and dynamic splicing SQL functions, Mybatis provides 9 kinds of dynamic SQL tags
Its implementation principle is to use OGNL to calculate the value of the expression from the SQL parameter object, according to the value of the expression dynamic splicing SQL, to complete the function of dynamic SQL.
6. How does Mybatis encapsulate SQL execution results as target objects and return them? What are the mappings?
The first is to use tags to define and list the mapping between object attribute names one by one.
The second is to use SQL column aliasing to write column names as object property names.
Mybatis uses reflection to create objects, and uses reflection to assign values to the attributes of the object one by one and return them. For those attributes that cannot find the mapping relationship, assignment cannot be completed.
7. Can Mybatis implement one-to-one or one-to-many associated query? What are the implementation methods and the differences between them?
Mybatis not only can execute one-to-one, one-to-many associative query, but also can execute many-to-one, many-to-many associative query, many-to-one query, actually is one-to-one query, just need to change selectOne() to selectList(); Many-to-many queries, which are essentially one-to-many queries, simply change selectOne() to selectList().
Associated object query can be performed in two ways:
One is to send a separate SQL query to the associated object resultMap, assign to the master object, and then return the master object;
Processing by query nesting (subquery)
The other is to use nested query. Nested query means to use JOIN query. Part of the column is the attribute value of object A, and the other part is the attribute value of associated object B. The advantage is that only one SQL query can be sent to find out the main object and its associated object.
Processing by query nesting (syntable query)
8. Does Mybatis support lazy loading? If so, how does it work?
Mybatis only supports lazy loading of association associative objects and collection associative objects. Association refers to one-to-one query and collection value is one-to-many query. In the Mybatis configuration file, you can configure whether to enable lazy loading lazyLoadingEnabled= Truefalse.
When the target method is called and the interceptor method is entered, for example, a.gettb.getName (), invoke() method a.gettb () is null, then the pre-saved SQL for querying the associated B object will be sent separately. Call a.setb (B), then a.getName (), then a.getb (). This is the basic principle of lazy loading.
9. In the XML mapping files of Mybatis, can the IDS of different XML mapping files be repeated?
Different XML mapping files, if configured with Namespace, then the Id can be repeated; If no namespace is configured, the Id must be unique. After all, Namespace is not mandatory, just a best practice.
The namespace+ ID is used by the key of Map< string. MappedStatement>. If there is no namespace, only ids are left. With namespace, the natural ID can be repeated. Different namespcae, namespace+ ID naturally also different.
10. What Executor executors does Mybatis have? What’s the difference between them?
There are three types: SimpleExecutor, ReuseExector, and BatchExector.
SimpleExecutor: Every time an update or select is performed, the Statement object is opened and closed immediately.
ReuseExecutor: Perform update or select to find the Statement object using the SQL as the key. If the Statement object exists, use it. If the Statement object does not exist, create it. In short, the Statement object is reused.
BatchExecutor: Performs Update(no SELECT,JDBC batch does not support SELECT), adds all SQL to the batch (addBatch ()), and waits for unified execution (executorBatch()). It caches multiple Statement objects. After addBatch() is complete, each Statement object waits for the executeBatch() batch to be executed one by one, which is the same as the JDBC batch.
11. How to specify which Executor to use in Mybatis?
In the Mybatis configuration file, you can specify the default ExecutorType ExecutorType, or you can manually pass ExecutorType parameters to the SqlSession creation method of the DefalutSqlSessionFactory.
12. Can Mybatis map Enum classes?
Mybatis can map enumeration classes, not only can map enumeration classes, Mybatis can map any object to a column of a table. The mapping method is to define a custom TypeHandler that implements the setParameter() and getResult() interface methods of TypeHandler. TypeHandler performs a conversion from javaType to jdbcType and a conversion from jdbcType to javaType as setParameter() and getResult(). Represents setting SQL hello placeholder parameters and getting column query results.
13. In Mybatis mapping file, if tag A references the content of tag B through include, is the content of tag B defined after tag A or must be defined before tag A?
Although Mybatis parses Xml mapping files sequentially, the referenced B tag can still be defined anywhere and Mybatis can recognize it correctly.
The principle is that when A contains B, Mybatis will set A to the unparsed state when IT finds THAT B has not been parsed when IT reaches A, and then continue to parse the following tags. After all tags are fully parsed, Mybatis will parse the unparsed tags again.
14. Differences between Mybatis and Spring dataJpa
First of all, SprigData JPA can be understood as the abstraction of THE JPA specification again, and the underlying implementation is still using hibernate framework’S JPA technology.
JPA is implemented using Hibernate technology by default, so Springdata JPA, also known as Hibernate, is generally used.
Hibernate is an open source object-relational mapping framework that encapsulates JDBC objects in a very lightweight way. It maps POJOs to database tables. It is a fully automatic ORM framework. Hibernate automatically generates SQL statements, allowing Java members to manipulate the database with object programming thinking at will.
Mybatis is an excellent persistence layer framework that supports customized SQL, stored procedures, and advanced mapping. Mybatis avoids almost all of the JDBC code and manual setting of parameters and fetching result sets. Mybatis can configure and map native information using simple XML or annotations to map interfaces and Java Entity objects to records in the database.
So, we say the difference between JPA and Mybatis also say the difference between Hibernate and MyBatis.
Hibernate has strong object/relational mapping capability and good database independence. For software with high requirements on relational model (such as customized software with fixed requirements), developing with Hibernate can save a lot of code and improve efficiency. However, the learning threshold is high, and the threshold is higher to master, and the design of O/R(Object/Relation) mapping needs to be satisfied. How to balance between performance and Object model? I saw the data model in PowerDesigner during my internship, and there were indeed many tables. Many-to-many tables have an associative table in the middle, so when I write poJOs I have an extra class.
In general, Mybatis can perform more detailed SQL optimization to query the necessary fields, but needs to maintain the mapping between THE SQL and the query result set. The portability of database is poor, and different SQL is written for different databases.
Hibernate provides a relatively complete package for database, encapsulating the basic DAO layer operations, and has good database portability, but the learning cycle is long and the development difficulty is greater than Mybatis.
Summary:
Mybatis learning threshold is low, easy to learn, a programmer to write SQL directly, can strictly control the SQL execution performance, high flexibility, very suitable for low requirements for relational data model of software development, such as the Internet software, business software, because this kind of software requirements changes frequently, a change but demand for output results quickly.
Today’s share has ended, please forgive and give advice!