2023-Mybatis common interview questions

 What is 0RM framework

ORM (Object-Relational Mapping) is used to map between relational databases and business entity objects, so that

When we specifically operate business objects, we don't need to deal with complex SQL statements anymore, as long as we operate as usual

Just manipulate it as an object. For example, our hibernate and mybatis are an orm framework

What is Mybatis

  1. It is a very good persistence layer framework
  2. Support custom SQL (manually write SQL statements), support stored procedures and advanced mapping (one-to-one, one-to-many, many-to-many)
  3. Eliminates almost all JDBC code and the work of setting parameters and packaging result sets
  4. SQL statements can be written through simple XML files or annotations
  5. is an ORM framework

mybatis execution process

1. The project starts and loads mybatis-config.xml, reads the mybatis operating environment, and loads the sq1 mapping file of mybatis (configures the sql statement and mapping relationship for operating the database)

2. Create a SqlSessionFactory factory through a configuration file to create a SqlSession

3. Operate the crud of the database through SqlSession

4. The execution of SqlSession is to execute the sql statement (MappedStatement) through the Executor executor defined at the bottom of mybatis

5. Then the executor executes  the MappedStatement object encapsulated at the bottom of mybatis. The current object encapsulates the information of sql mapping and the information of input and output results

(A sql in the xxMapper.xm1 file corresponds to a MappedStatment object)

Can the dao layer in mybatis be overloaded?

No, the method name of the interface in the dao layer is consistent with the tag id value in the xml , so once the method is repeated

After loading, the same method name will appear, and the id in the xml will also have the same name. Once the system is running, an error will be reported  .

The fully qualified name of the Mapper interface is the value of the namespace in the mapping file, and the method name of the interface is the mapping

The id value of MappedStatement in the file and the parameters in the interface method are the parameters passed to sql. The Mapper interface does not have an implementation class. When calling an interface method, the full interface name + method name concatenated string is used as the key value, which can be uniquely

Be sure to locate a MappedStatement, because it uses the full name + method name to save and find the strategy.

The working principle of the Mapper interface is the JDK dynamic proxy. Mybatis will use the JDK dynamic proxy to generate a proxy object Proxy for the Mapper interface during runtime. The proxy object will intercept the interface method and execute the sql represented by the MapperStatement, and then return the sql execution result

The first and second level caches of Mybatis :

It only opens the first-level cache, and the first-level cache is only relative to the same Sq1Session. So in parameters and SQL

In exactly the same situation, we use the same SqlSession object to call a Mapper method, and often only execute SQL once, because after the first query using SelSession, MyBatis will put it in the cache, and when querying later, if there is no statement If it needs to be refreshed and the cache has not timed out, SqlSession will fetch the current cached data without sending SQL to the database again. The second-level cache at the SqlSessionFactory level is not enabled by default and has a large range. Generally, third-party caching technologies are used, such as redis

Pros and Cons of Mybaits:

(1) Advantages:

① Compared with JDBC, it reduces the amount of code by more than 50%, eliminates a large number of redundant codes of JDBC, and does not require manual switch connection;

② Based on SQL statement programming, it is quite flexible and will not have any impact on the existing design of the application program or database. SQL is written in XML, which decouples sql from the program code and facilitates unified management; XML tags are provided to support writing dynamic SQL statement and can be reused.

③ Very compatible with various databases (because MyBatis uses JDBC to connect to the database, so as long as the database MyBatis supports JDBC supports).

④ Can be well integrated with Spring;

⑤ Provide mapping tags to support ORM field relationship mapping between objects and databases; provide object-relational mapping tags to support object-relational component maintenance.

(2) Disadvantages:

① The workload of writing SQL statements is relatively large, especially when there are many fields and many associated tables, there are certain requirements for developers to write SQL statements.

② SQL statements depend on the database, resulting in poor database portability, and the database cannot be replaced at will.

The difference between JDBC, Hibernate and Mybatis

Hibernate is fully automatic, Mybatis is semi-automatic, and Jdbc is manual. In terms of development efficiency, hibernate

Higher, Mybatis is in the middle, and jdbc is lower. In terms of execution efficiency, hibernate is lower, Mybatis is in the middle, and jdbc is higher, because jdbc is purely hand-written sql statements, and the development efficiency is extremely low. Mybatis has relatively flexible control over sql. Optimize according to business needs, and hibernate has relatively high development efficiency because of its high encapsulation, but because of this reason, programmers are relatively weak in the control and optimization of sql statements.

In the Xml mapping file of Mybatis, can the id of different Xml mapping files be repeated?

        For different Xml mapping files, if the namespace is configured, the id can be repeated; if the namespace is not configured, the id cannot be repeated; the reason is that the namespace+id is used as the key of the Map, if there is no namespace, only the id is left, then, Duplicate ids will cause data to overwrite each other. With the namespace, the id can be repeated naturally, and the namespace+id is naturally different if the namespace is different.

Briefly describe the operating principle of Mybatis plug-ins and how to write a plug-in.

Answer: Mybatis can only write plug-ins for the 4 interfaces of ParameterHandler, ResultSetHandler, StatementHandler, and Executor. Mybatis uses the dynamic proxy of JDK to generate proxy objects for the interfaces that need to be intercepted to implement the interface method interception function. Whenever these 4 interfaces are executed Object method, it will enter the interception method, specifically the invoke() method of InvocationHandler. Of course, only those methods that you specify to be intercepted will be intercepted.

Write a plug-in: implement the Interceptor interface of Mybatis and override the intercept() method, then write annotations for the plug-in, specify which methods of which interface to intercept, and finally configure the plug-in you wrote in the configuration file.

What dynamic SQL does Mybatis have? Execution principle? What kind of dynamic SQL?

  answer:

       1. The dynamic SQL of Mybatis can be written in the form of tags in the XML mapping file. The execution principle is to complete the logical judgment and dynamically splice SQL according to the value of the expression.

       2. Mybatis provides 9 dynamic sql tags: trim | where | set | foreach | if | choose | when | otherwise | bind .

Why is MyBatis a semi-ORM framework? How is it different from Hibernate?

  1. ORM, it means the mapping between objects and relationships, including [object -> relationship mapping] and [relationship -> object mapping] two aspects.
  2. Hibernate is a complete ORM framework, which realizes the functions of these two aspects. MyBatis only completes [relationship->object mapping]. To be precise, MyBatis is a SQL mapping framework rather than an ORM framework, because it only has field mapping, and object data and the actual relationship between objects still need to be implemented through handwritten SQL.
  3. MyBatis can directly write native SQL, and can strictly control the execution performance of SQL, with high flexibility. Hibernate can only implement database queries by writing HQL.

What is the interface binding of MyBatis? What binding methods are there?

  1. Interface binding is to define the interface in MyBatis, and then bind the methods in the interface to SQL statements. We can directly call the methods in the interface to operate the database. This is more flexible and simpler than directly using the original ecological method provided by the SqlSession object.
  2. There are two main ways to implement interface binding: (1) The first is to bind through annotations, that is to say, add @Select, @Update and other annotations on the interface methods, and the annotations contain SQL statements for binding. This method can save the xml mapping file of SQL, which is more suitable for simple SQL, but it is difficult to maintain later, and it is basically not used in business. (2) The second is to bind by writing SQL in the SQL xml mapping file.  In this case, specify that the namespace parameter in the xml mapping file must be the full class name of the interface. Whether the SQL is simple or complex, the xml file method is relatively simple and efficient, and it is also the most commonly used.

Guess you like

Origin blog.csdn.net/weixin_45934981/article/details/130155214