Mybatis framework interview questions

Eight, Mybatis framework interview questions

1. What is MyBatis?

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

2. What does Mybatis dynamic sql do? What dynamic sql are there? Can you briefly describe the implementation principle of dynamic sql?

a. Mybatis dynamic sql allows us to write dynamic sql in the form of tags in the Xml mapping file to complete the functions of logical judgment and dynamic splicing of sql. b. Mybatis provides 9 dynamic sql tags: trim|where|set|foreach|if|choose|when|otherwise|bind.
c. Its execution principle is to use OGNL to calculate the value of the expression from the sql parameter object, and dynamically splicing sql according to the value of the expression, so as to complete the function of dynamic sql.

3. What is the difference between #{} and ${}?

a. #{} is precompiled processing, KaTeX parse error: Expected 'EOF', got '#' at position 24: …. b. When Mybatis processes #̲{}, it will replace ${} with the value of the variable when #{…{} in sql.
d. Using #{} can effectively prevent SQL injection and improve system security.

4. Why is Mybatis a semi-automatic ORM mapping tool? How is it different from fully automatic?

Hibernate is a fully automatic ORM mapping tool. When using Hibernate to query related objects or related collection objects, it can be obtained directly according to the object relational model, so it is fully automatic. Mybatis needs to manually write sql when querying associated objects or associated collection objects, so it is called a semi-automatic ORM mapping tool.

5. What is the difference between MyBatis and Hibernate?

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

6. What are the benefits of MyBatis?

a. MyBatis separates the sql statement from the Java source program and writes it in a separate XML file, which brings great convenience to program maintenance.
b. MyBatis encapsulates the calling details of the underlying JDBC API, and can automatically convert the result set into Java Bean objects, which greatly simplifies the repetitive work of Java database programming.
c. Because MyBatis requires programmers to write sql statements by themselves, programmers can flexibly control sql statements in combination with the characteristics of the database itself, so it can achieve higher query efficiency than fully automatic orm frameworks such as Hibernate, and can complete complex queries.

7. What is the interface binding of MyBatis and what are the benefits?

Interface mapping is to define an interface arbitrarily in MyBatis, and then bind the method in the interface to the SQL statement. We can directly call the interface method, so that we can have more flexible choices and settings than the method provided by the original SqlSession.

8. There are several ways to implement interface binding, and how are they implemented?

There are two ways to implement interface binding. One is to bind through annotations, that is, to add @Select@Update and other annotations to the interface methods to bind with Sql statements in them, and the other is to bind by writing SQL in xml. In this case, the namespace in the specified xml mapping file must be the full path name of the interface.

9. When the attribute name in the entity class is different from the field name in the table, what if the query result is encapsulated into the specified pojo?

a. By defining the alias of the field name in the sql statement of the query.
b. Map the one-to-one correspondence between field names and entity class attribute names.

10.Mapper DAO layer development specification

a. The full path of the interface should be consistent with the namespace of the mapping file
b. The method name of the interface should be consistent with the statementId in the mapping file
c. The parameter type and return type of the interface method should be consistent with the parameterType and resultType in the mapping file d. It is best to keep the names of the interface and the mapping file consistent. For example: UserMapper.java/UserMapper.xml
e. It is best to put the interface and the mapping file in the same directory

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325059528&siteId=291194637