Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the integrated MapperScannerConfigurer of Spring and Mybatis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to use MapperScannerConfigurer on the integration of Spring and Mybatis. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

MapperScannerConfigurer introduction

MapperScannerConfigurer is a class provided in the mybatis-spring jar package of spring and mybatis integration.

If you want to understand what this class does, you need to know MapperFactoryBean.

The emergence of MapperFactoryBean is implemented using dynamic proxies instead of manually writing data access objects (DAO) using SqlSessionDaoSupport or SqlSessionTemplate.

For example, the configuration in the following official document:

Org.mybatis.spring.sample.mapper.UserMapper is an interface, we create an instance of MapperFactoryBean, and then inject this interface and sqlSessionFactory (the SqlSessionFactory interface provided in mybatis, MapperFactoryBean uses SqlSessionFactory to create SqlSession).

If you want to use the UserMapper interface, inject the bean directly through spring, and then you can use it directly. A dynamic proxy for this interface will be created inside spring.

When you find that you want to use multiple MapperFactoryBean, one definition must be very troublesome, so mybatis-spring provides the class MapperScannerConfigurer, which will look for mappers under the classpath and automatically create them as MapperFactoryBean.

This configuration scans all interfaces under org.mybatis.spring.sample.mapper and then creates dynamic proxy classes for their respective interfaces.

MapperScannerConfigurer low-level code analysis

Take the following code as an example (part of the code, other code and configuration omitted):

Package org.format.dynamicproxy.mybatis.dao;public interface UserDao {public User getById (int id); public int add (User user); public int update (User user); public int delete (User user); public List getAll ();}

Let's first see what the implementation class of userDao is through the test case debug.

We can see that userDao is an instance of the MapperProxy class.

Take a look at the source code of MapperProxy, yes, the implementation of InvocationHandler, indicating the use of jdk's own dynamic proxy.

Public class MapperProxy implements InvocationHandler, Serializable {private static final long serialversionUID =-6424540398559729838L; private final SqlSession sqlSession; private final Class mapperinterface; private final Map methodCache; public MapperProxy (SqlSession sqlSession, Class mapperInterface, Map methodCache) {this.sqlSession = sqlSession; this.mapperInterface = mapperInterface; this.methodCache = methodCache } public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {if (Object.class.equals (method.getDeclaringClass () {try {return method.invoke (this, args);} catch (Throwable t) {throw ExceptionUtil.unwrapThrowable (t);}} final MapperMethod mapperMethod = cachedMapperMethod (method); return mapperMethod.execute (sqlSession, args) } private MapperMethod cachedMapperMethod (Method method) {MapperMethod mapperMethod = methodCache.get (method); if (mapperMethod = = null) {mapperMethod = new MapperMethod (mapperInterface, method, sqlSession.getConfiguration ()); methodCache.put (method, mapperMethod);} return mapperMethod;}} start analyzing the source code of MapperScannerConfigurer

MapperScannerConfigurer implements the BeanDefinitionRegistryPostProcessor interface, which is an interface that can modify the bean defined in the spring foreman, which has a postProcessBeanDefinitionRegistry method.

Then we take a look at how the key in ClassPathMapperScanner is to scan the interface under the corresponding package.

In fact, the function of MapperScannerConfigurer is to change the corresponding interface type to MapperFactoryBean, and the attribute mapperInterface of this MapperFactoryBean is the original type. MapperFactoryBean has been analyzed at the beginning of this article.

So in the end, we have to analyze the implementation principle of MapperFactoryBean!

MapperFactoryBean inherits the SqlSessionDaoSupport class, the SqlSessionDaoSupport class inherits the DaoSupport abstract class, and the DaoSupport abstract class implements the InitializingBean interface, so the afterPropertiesSet method of the InitializingBean interface is called when you instance a MapperFactoryBean.

The afterPropertiesSet method of DaoSupport:

MapperFactoryBean overrides the checkDaoConfig method:

Then when you get the corresponding bean through the spring factory:

The SqlSession here is the getMapper method of SqlSessionTemplate,SqlSessionTemplate:

The getMapper method of Configuration, which uses the getMapper method of MapperRegistry:

The getMapper method of MapperRegistry:

MapperProxyFactory constructs MapperProxy:

That's right! MapperProxyFactory uses the Proxy with the jdk group to complete the dynamic proxy.

MapperProxy had already mentioned it at the beginning. The MapperMethod class is used internally in MapperProxy to complete the call to the method:

Next, let's use UserDao's getById method to debug to see how MapperMethod's execute method works.

@ Testpublic void testGet () {int id = 1; system.out.println (userDao.getById (id));}

This is the end of this article on "how to use MapperScannerConfigurer for the integration of Spring and Mybatis". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report