Mybatis Interview Question Summary

2024-05-05 06:12

本文主要是介绍Mybatis Interview Question Summary,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. In best practice, usually an Xml mapping file will write a Dao interface corresponding to it. What is the working principle of the Dao interface? Can the methods in the Dao interface be overloaded when the parameters are different?

Answer: The Dao interface is commonly referred to as the Mapper interface. The fully qualified name of the interface is the value of the namespace in the mapping file, the method name of the interface is the ID value of the MappedStatement in the mapping file, and the parameters in the interface method are the parameters passed to sql. Mapper interface has no implementation class. When calling interface methods, the interface full name+method name concatenated string is used as the key value to uniquely locate a MappedStatement, for example: com. mybatis3. mappers StudentDao.findStudentById, you can find that the only namespace is com.mybatis3.mappers MappedStatement with id=findStudentById under StudentDao. In Mybatis, each,,,tag will be resolved to a MappedStatement object.
The method in the Dao interface cannot be overloaded, because it is the save and search strategy of fully qualified name+method name.
The working principle of the Dao interface is JDK dynamic proxy. Mybatis runtime will use JDK dynamic proxy to generate proxy objects for the Dao interface. The proxy object proxy will intercept the interface method, instead execute the sql represented by MappedStatement, and then return the sql execution results.

2. How does Mybatis paginate? What is the principle of paging plug-ins?

Answer: Mybatis uses the RowBounds object for paging. It is memory paging for the ResultSet result set, not physical paging. You can directly write parameters with physical paging in sql to complete the physical paging function, or you can use the paging plug-in to complete physical paging.
The basic principle of the paging plug-in is to use the plug-in interface provided by Mybatis to implement a user-defined plug-in, intercept the sql to be executed in the plug-in interception method, then rewrite the sql, and add the corresponding physical paging statements and physical paging parameters according to the dialect of dialect.
For example: select * from student. After intercepting sql, it is rewritten as: select t. * from (select * from student) t limit 0, 10

3. Briefly describe the operation principle of Mybatis plug-in and how to write a plug-in.

Answer: Mybatis can only write plug-ins for the four interfaces: ParameterHandler, ResultSetHandler, StatementHandler, and Executor. Mybatis uses the JDK’s dynamic proxy to generate proxy objects for the interfaces that need to be intercepted to implement the interface method interception function. Each time the methods of the four interface objects are executed, the interception method will enter, specifically the invoke() method of the InvocationHandler. Of course, only those methods that you specify need to be intercepted will be intercepted.
Implement the Interceptor interface of Mybatis and copy the intercept() method, then write comments for the plug-in to specify which methods of which interface to intercept. Remember, don’t forget to configure the plug-in you wrote in the configuration file.

4. Can Mybatis return the database primary key list when it performs batch insert?

A: Yes, both JDBC and Mybatis can.

5. What does Mybatis dynamic sql do? What are the dynamic sql? Can you briefly describe the execution 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 logical judgment and dynamic splicing of sql. Mybatis provides nine dynamic sql tags trim | where | set | foreach | if | choose | when | otherwise | bind.
Its implementation principle is to use OGNL to calculate the value of the expression from the sql parameter object, and dynamically splice sql according to the value of the expression to complete the dynamic sql function.

6. How does Mybatis encapsulate sql execution results as target objects and return them? What are the mapping forms?

Answer: The first is to use thetag to define the mapping relationship between column names and object attribute names one by one. The second is to use the alias function of the sql column to write the column alias as the object attribute name, such as T_NAME AS NAME. The object attribute name is generally name, lowercase, but the column name is not case sensitive. Mybatis will ignore the case of the column name and find the corresponding object attribute name intelligently. You can even write it as T_NAME AS NaMe. Mybatis can work normally as well.
With the mapping relationship between the column name and the attribute name, Mybatis creates objects through reflection. At the same time, it uses the attributes reflected to the object to assign values one by one and return them. The attributes that cannot find the mapping relationship cannot be assigned.

7. Can Mybatis perform one-to-one and one to many association queries? What are the implementation methods and the differences between them.

A: Yes, Mybatis can not only execute one-to-one and one to many association queries, but also execute many to one and many to many association queries. Many to one queries are actually one-to-one queries. Just change selectOne () to selectList (); Many to many queries are actually one to many queries. You just need to change selectOne () to selectList ().
There are two ways to query associated objects. One is to send a separate sql to query the associated object, assign it to the main object, and then return the main object. The other is to use nested queries, which means to use join queries. One column is the attribute value of object A, and the other column is the attribute value of associated object B. The advantage is that only one sql query can be issued to find the main object and its associated object.
Then the problem arises. The join queries 100 records. How can we determine that there are 5 main objects instead of 100? The principle of de duplication is that thesub tag in thetag specifies the id column that uniquely determines a record. Mybatis performs the de duplication function of 100 records according to thecolumn value. There can be multipletags, representing the semantics of the joint primary key.
Similarly, the associated object of the primary object is repeated according to this principle. Although in general, only the primary object will have duplicate records, the associated object will not be repeated.

8. Does Mybatis support delayed loading? If yes, what is its implementation principle?

Answer: Mybatis only supports delayed loading of association associated objects and collection associated collection objects. Association refers to one-to-one queries and collection refers to one to many queries. In the Mybatis configuration file, you can configure whether to enable lazy loading lazyLoadingEnabled=true | false.
Its principle is that CGLIB is used to create the proxy object of the target object. When the target method is called, the interceptor method will enter, for example, a.getB(). getName(). When the interceptor invoke() method finds that a.getB() is null, the interceptor will separately send the pre saved query sql associated with the B object, query B, and then call a.setB (b), so that the object b property of a has a value, and then complete the call of a.getB(). getName() method. This is the basic principle of delayed loading.
Of course, not only Mybatis, but also Hibernate. The principle of supporting delayed loading is the same.

9. In the Xml mapping file of Mybatis, can the IDs of different Xml mapping files be repeated?

Answer: 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 duplicate; After all, namespaces are not necessary, but best practices.
The reason is that namespace+id is used as the key of Map<String, MappedStatement>. If there is no namespace, there is only an id left. If the id is repeated, the data will overwrite each other. With namespace, the natural ID can be repeated. With different namespaces, the namespace+ID will be different.

10. How to perform batch processing in Mybatis?

Answer: Use BatchExecutor to complete batch processing.

11. What Executor actuators does Mybatis have? What is the difference between them?

A: Mybatis has three basic executors, SimpleExecutor, ReuseExecutor, and BatchExecutor.
SimpleExecutor: Every time an update or select is executed, a Statement object will be opened, and the Statement object will be closed as soon as it is used up.
ReuseExecutor: execute update or select, use sql as the key to find the Statement object, use it if it exists, and create it if it does not exist. After use, do not close the Statement object, but place it in Map<String, Statement>for next use. In short, reusing Statement objects.
BatchExecutor: Execute update (no select, JDBC batch processing does not support select), add all sql to batch processing (addBatch()), and wait for unified execution (executeBatch()). It caches multiple Statement objects. After each Statement object is addBatch(), wait for executeBatch() batch processing one by one. Same as JDBC batch processing.
Scope: These features of the Executor are strictly limited to the SqlSession life cycle.

12. How to specify which Executor executor to use in Mybatis?

Answer: In the Mybatis configuration file, you can specify the default ExecutorType actuator type, or manually pass the ExecutorType type parameter to the method of creating a SqlSession in the DefaultSqlSessionFactory.

13. Can Mybatis map Enum enumeration classes?

Answer: Mybatis can map enumeration classes, not only enumeration classes, but also any object to a column of the table. The mapping method is to customize a TypeHandler and implement the setParameter() and getResult() interface methods of the TypeHandler. TypeHandler has two functions: one is to complete the conversion from javaType to jdbcType, and the other is to complete the conversion from jdbcType to javaType, which is reflected in setParameter() and getResult(), which respectively represent the setting of sql question mark placeholder parameters and obtaining column query results.

14. In the Mybatis mapping file, if the A tag references the content of the B tag through include, can the B tag be defined after the A tag, or must it be defined before the A tag?

Answer: Although Mybatis parses the Xml mapping file in order, the referenced B tag can still be defined anywhere, and Mybatis can correctly recognize it.
The principle is that Mybatis parses the A tag and finds that the A tag references the B tag, but the B tag has not yet been resolved, and does not exist. At this time, Mybatis will mark the A tag as unresolved, and then continue to parse the remaining tags, including the B tag. After all tags are parsed, Mybatis will re parse those tags marked as unresolved. At this time, when parsing the A tag, the B tag already exists, and the A tag can be parsed normally.

15. Briefly describe the mapping relationship between the Xml mapping file of Mybatis and the internal data structure of Mybatis?

Answer: Mybatis encapsulates all the Xml configuration information into the All In One heavyweight object Configuration. In the Xml mapping file, thetag will be resolved into a ParameterMap object, and each of its child elements will be resolved into a ParameterMapping object< The resultMap>tag will be resolved into a ResultMap object, and each of its child elements will be resolved into a ResultMapping object. Each,,, andtag will be resolved to a MappedStatement object, and the sql in the tag will be resolved to a BoundSql object.

16. Why is Mybatis a semi-automatic ORM mapping tool? What is the difference between it and full automation?

Answer: Hibernate is a fully automatic ORM mapping tool. When Hibernate is used to query associated objects or associated collection objects, it can be directly obtained according to the object relationship model, so it is fully automatic. When Mybatis queries associated objects or associated collection objects, it needs to write sql manually. Therefore, it is called a semi-automatic ORM mapping tool.

17. What if the attribute name in the entity class is different from the field name in the table?

Type 1: By defining the alias of the field name in the query sql statement, make the alias of the field name consistent with the attribute name of the entity class
Type 2: map the one-to-one correspondence between field names and entity class attribute names through

18. How to pass multiple parameters in mapper?

First: the idea of using placeholders
Use # {0} in the mapping file, and # {1} represents the number of parameters passed in
Use the @ param annotation: to name parameters
The second is to use the Map set as a parameter to load

19. What does Mybatis dynamic sql do? What are the dynamic sql? Can you briefly describe the execution principle of dynamic sql?

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.
Mybatis provides nine dynamic sql tags: trim | where | set | foreach | if | choose | when | otherwise | bind.
Its implementation principle is to use OGNL to calculate the value of the expression from the sql parameter object, and dynamically splice sql according to the value of the expression to complete the dynamic sql function.

20. In the Xml mapping file of Mybatis, can the IDs of different Xml mapping files be repeated?

If namespace is configured, it can be repeated, because our Statement is actually namespace+id
If the namespace is not configured, the same ID will result in overwriting.

21. How can interface binding be implemented?

Interface binding can be implemented in two ways:
One is binding through annotations, that is, adding@ Select@Update Such annotations contain Sql statements to bind
The other is to bind by writing SQL in xml. In this case, the namespace in the xml mapping file must be the full path name of the interface

22. How does Mybatis paginate? What is the principle of paging plug-ins?

Mybatis uses the RowBounds object for paging. It is memory paging for the ResultSet result set instead of physical paging. You can directly write parameters with physical paging in SQL to complete the physical paging function, or you can use paging plug-ins to complete physical paging.

23. The basic principle of the paging plug-in is to use the plug-in interface provided by Mybatis to implement a user-defined plug-in, intercept the sql to be executed in the plug-in interception method, and then rewrite the sql. According to the dialect of dialect, add the corresponding physical paging statements and physical paging parameters.

For example:
Select * from student. After intercepting sql, it is rewritten as: select t. * from (select * from student) t limit 0, 10

24. Briefly describe the operation principle of Mybatis plug-in and how to write a plug-in

Mybatis can only write plug-ins for the four interfaces: ParameterHandler, ResultSetHandler, StatementHandler, and Executor. Mybatis uses the JDK’s dynamic proxy to generate proxy objects for the interfaces that need to be intercepted to implement the interface method interception function. Whenever the methods of the four interface objects are executed, the interception method will enter, specifically the invoke() method of InvocationHandler. Of course, only those methods that you specify need to be intercepted will be intercepted.
Implement the Interceptor interface of Mybatis and copy the intercept() method, then write comments for the plug-in to specify which methods of which interface to intercept. Remember, don’t forget to configure the plug-in you wrote in the configuration file.

这篇关于Mybatis Interview Question Summary的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/960962

相关文章

持久层 技术选型如何决策?JPA,Hibernate,ibatis(mybatis)

转自:http://t.51jdy.cn/thread-259-1-1.html 持久层 是一个项目 后台 最重要的部分。他直接 决定了 数据读写的性能,业务编写的复杂度,数据结构(对象结构)等问题。 因此 架构师在考虑 使用那个持久层框架的时候 要考虑清楚。 选择的 标准: 1,项目的场景。 2,团队的技能掌握情况。 3,开发周期(开发效率)。 传统的 业务系统,通常业

tensorboard-----summary用法总结

Tensorflow学习笔记——Summary用法         最近在研究tensorflow自带的例程speech_command,顺便学习tensorflow的一些基本用法。 其中tensorboard 作为一款可视化神器,可以说是学习tensorflow时模型训练以及参数可视化的法宝。 而在训练过程中,主要用到了tf.summary()的各类方法,能够保存训练过程以及参数分布图并在

MyBatis-Plus常用注解详解与实战应用

MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。它提供了大量的常用注解,使得开发者能够更方便地进行数据库操作。 MyBatis-Plus 提供的注解可以帮我们解决一些数据库与实体之间相互映射的问题。 @TableName @TableName 用来指定表名 在使用 MyBatis-Plus 实现基本的 C

MyBatis系列之分页插件及问题

概述 无论是C端产品页面,还是后台系统页面,不可能一次性将全部数据加载出来。后台系统一般都是PC端登录,用Table组件(如Ant Design Table)渲染展示数据,可点击列表的下一页(或指定某一页)查看数据。C端产品如App,在下滑时可查看更多数据,看起来像是一次性加载数据,实际上也是分批请求后台系统获取数据。而这,就是分页功能。 如果没有使用Hibernate或MyBatis这样的O

【MyBatis学习8】MyBatis中的二级缓存

1. 二级缓存的原理   前面介绍了,mybatis中的二级缓存是mapper级别的缓存,值得注意的是,不同的mapper都有一个二级缓存,也就是说,不同的mapper之间的二级缓存是互不影响的。为了更加清楚的描述二级缓存,先来看一个示意图:      从图中可以看出: sqlSession1去查询用户id为1的用户信息,查询到用户信息会将查询数据存储到该UserMapper的二级缓存中

【MyBatis学习7】MyBatis中的一级缓存

缓存的作用是减轻数据库的压力,提高数据库的性能的。mybatis中提供了一级缓存和二级缓存,先来看一下两个缓存的示意图:    从图中可以看出: 一级缓存是SqlSession级别的缓存。在操作数据库时需要构造sqlSession对象,在对象中有一个数据结构(HashMap)用于存储缓存数据。不同的sqlSession之间的缓存数据区域(HashMap)是互相不影响的。二级缓存是mappe

springboot+vue+mybatis旅游管理+PPT+论文+讲解+售后

随着人民生活水平的提高,旅游业已经越来越大众化,而旅游业的核心是信息,不论是对旅游管理部门、对旅游企业,或是对旅游者而言,有效的获取旅游信息,都显得特别重要.旅游管理系统将使旅游相关信息管理工作规范化、信息化、程序化,提供旅游景点、旅游线路,旅游新闻等服务本文以jsp为开发技术,实现了一个旅游网站系统。旅游网站系统的主要使用者分为管理员和用户,管理员权限如下;主页、个人中心、景点分类管理、景点信息

spring(一)--spring/springmvc/spring+hibernate(mybatis)配置文件

这篇文章用来总结一下spring,springmvc,spring+mybatis,spring+hibernate的配置文件 1.web.xml 要使用spring,必须在web.xml中定义分发器等信息,基本的配置信息如下: <?xml version="1.0" encoding= "UTF-8"?><web-app version= "3.0"xmlns="http://java.

Mybatis-映射文件中select标签resultType属性的使用

数据库的最最基本操作“增删改查”,“查”是最复杂的,有各种各样的查询,所以对应到Mybatis中的select标签也是这四个操作中最复杂的 resultType属性的使用 1.返回的结果是List集合的类型 select标签里的resultType类型设置为List集合里的元素类型 2.返回一个Map集合 key是列名称,value是列对应的值 3.返回的查询结果也是Map集合

三、MyBatis实践:提高持久层数据处理效率

三、MyBatis实践:提高持久层数据处理效率 目录 一、Mybatis简介 1.1 简介1.2 持久层框架对比1.3 快速入门(基于Mybatis3方式) 二、MyBatis基本使用 2.1 向SQL语句传参 2.1.1 mybatis日志输出配置2.1.2 #{}形式2.1.3 ${}形式 2.2 数据输入 2.2.1 Mybatis总体机制概括2.2.2 概念说明2.2.3 单个简单类型