Interviewer: Oh, Mybatis is quite thorough? Netizen: After reading this Mybatis, my thinking suddenly became clear!

MyBatis is an excellent persistence layer framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC
code and manual setting of parameters and obtaining result sets.

What is MyBatis?

answer:

  • MyBatis is a persistence layer framework that can customize SQL, stored procedures and advanced mapping.

mybatis framework

Insert picture description here

Talk about the cache of MyBatis

answer:

  • MyBatis's cache is divided into first-level cache and second-level cache. The first-level cache is placed in the session. It is available by default. The second-level cache is placed in its namespace. It is not turned on by default. The use of the second-level cache attribute class needs to be implemented. Serializable serialization interface (can be used to save the state of the object), which can be configured in its mapping file

I have compiled interview questions from more than 20 companies here, as well as various knowledge points about Spring, Spring boot, Spring MVC, MyBatis, MySQL, JVM, etc. If you need a small partner, you can add the group 1149778920 password: qf
Insert picture description here

How does Mybatis pagination? What is the principle of the paging plugin?

answer:

  • Mybatis uses the RowBounds object for paging. You can also directly write sql to achieve paging, or you can use Mybatis's paging plug-in.
  • The principle of the paging plug-in: implement the interface provided by Mybatis, implement a custom plug-in, intercept the SQL to be executed in the plug-in's interception method, and then rewrite the SQL.
    Example: select * from student, after intercepting sql, rewrite as: select t.* from (select * from student) tlimit 0,10

Why is Mybatis a semi-automatic ORM mapping tool? What is the difference between it and fully automatic?

answer:

  • Hibernate is a fully automatic ORM mapping tool. When you use Hibernate to query related objects or related collection objects, you can directly obtain them according to the object relationship model, so it is fully automated. When Mybatis queries related objects or related collection objects, you need to manually write sql to complete, so it is called a semi-automatic ORM mapping tool.

Does Mybatis support lazy loading? If so, what is its implementation principle?

answer:

  • Mybatis only supports lazy loading of association objects and collection objects. Association refers to one-to-one, and collection refers to one-to-many query. In the Mybatis configuration file, you can configure whether to enable lazy loading lazyLoadingEnabled=true|false.
  • Its principle is to use CGLIB to create the proxy object of the target object. When the target method is called, enter the interceptor method, such as calling a.getB().getName(), and the interceptor invoke() method finds that a.getB() is null value, then it will separately send the previously saved sql query associated with the B object, query B up, and then call a.setB(b), so the object b attribute of a has a value, and then complete a.getB( ).getName() method call. This is the basic principle of lazy loading.

What is the difference between MyBatis and Hibernate?

answer:

  • Mybatis is different from hibernate in that it is not exactly an ORM framework, because MyBatis requires programmers to write Sql statements by themselves, but mybatis can flexibly configure the SQL statements to be run through XML or annotations, and map java objects and sql statements to generate the final execution Sql, and finally remap the result of sql execution to generate a java object.
  • Mybatis has a low learning threshold, simple and easy to learn, programmers directly write original ecological SQL, can strictly control SQL execution performance, and have high flexibility. It is very suitable for software development that does not require high relational data models, such as Internet software, enterprise operation software, etc. Because the requirements of this kind of software change frequently, once the requirements change, the results must be output quickly. But the premise of flexibility is that mybatis cannot be database-independent. If you need to implement software that supports multiple databases, you need to customize multiple sets of sql mapping files, which is a lot of work.
  • Hibernate has strong object/relational mapping capabilities and good database independence. For software with high requirements for relational models (such as customized software with fixed requirements), if you develop with hibernate, you can save a lot of code and improve efficiency. However, the disadvantage of Hibernate is that the threshold for learning is high, the threshold for proficiency is higher, and how to design O/R mapping, how to balance between performance and object model, and how to use Hibernate well requires strong experience and ability. In short, as long as a software architecture with good maintainability and scalability can be made in a limited resource environment according to user needs, it is a good architecture, so the framework is best if it is suitable.

How is the dynamic Sql in MyBatis set? What syntax?

Answer: The dynamic Sql in MyBatis is generally implemented through the if node, through OGNL syntax, but if you want to
write it completely, you must cooperate with the where and trim nodes. The where node is to insert where if the node contains content, otherwise it will not be
inserted. , The trim node is used to determine if the dynamic statement starts with and or or, then this and or or will be automatically
removed.

How does Mybatis encapsulate the sql execution result as a target object and return it? What are the mapping forms?

Answer: The
first is to use tags to define the mapping relationship between column names and object attribute names one by one.
The second function is to use an alias sql column, the column alias name is written as object properties, such as T_NAME AS NAME, for
like property name usually name, lowercase, but the column names are case insensitive, Mybatis ignores the column name case , Intelligently
find the corresponding object attribute name, you can even write T_NAME AS NaMe, Mybatis can work normally
.
With the mapping relationship between column names and attribute names, Mybatis creates objects through reflection, and uses the attributes reflected to the objects
to assign and return one by one. Those attributes that cannot find the mapping relationship cannot be assigned.

In the Xml mapping file, besides the common select|insert|updae|delete tags, what other tags are there?

A: There are many other label ,,,,, ,
plus nine dynamic sql tag,
the TRIM | the WHERE | the SET | foreach | IF | the Choose | the when | otherwise | the bind, among which is the sql fragment tag, pass
through The tag introduces sql fragments to generate strategy tags for primary keys that do not support auto-increment.

When the attribute name in the entity class is not the same as the field name in the table, how to encapsulate the result of the query in the specified pojo?

Answer:
1) By defining the alias of the field name in the query sql statement.
2) Use to map the one-to-one correspondence between field names and entity class attribute names.

How to write like statement in fuzzy query

Answer:
1) Concatenate wildcards in java and assign values ​​through #{}
2) Concatenate wildcards in Sql statements (unsafe will cause Sql injection)

At last

In view of the fact that many people have been interviewing recently, I have also compiled a lot of interview topic materials here, as well as experience from other major companies. Hope it helps everyone. Friends in
need can add group 1149778920 secret code: qf

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/SpringBoot_/article/details/109405048