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 integrate mybatis with spring extension points

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to integrate mybatis using spring extension points". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how mybatis integrates with spring extension points.

Mybatis generates dynamic proxies for each mapper interface. Each dynamic proxy object holds a reference to sqlSession. So how does mybatis hand over its own generated proxy objects to spring management?

When spring and mybatis are integrated, there are two main points:

1.@MapperScan

@ MapperScan We see the @ Import annotation, which injects the ImportBeanDefinitionRegistear implementation class MapperScannerRegistrar.class directly into the spring container. Execute the registerBeanDefinitions method inside, scan all interfaces under the specified package (mybatis has generated proxy objects for these interfaces), and dynamically put the proxy implementation classes of these interfaces into the MapperFactoryBean class.

You can see that SqlSessionFactoryBean has a way to implement InitializingBean and FactoryBean (two interfaces first) to rewrite InitializingBean's afterPropertiesSet, which puts various properties in the configuration file, such as mapperlocation, typealias, plugins, and so on, into configuration. And generate a sqlSessionFactory object. The getObject method that overrides FactoryBean calls the afterPropertiesSet method to generate the sqlSessionFactory object (in this case, the parsed configuration sqlSessionFactory is put into the spring).

Usually we configure SqlSessionFactoryBean in the xml of spring, where you can specify dataSource, configLocation, mapperLocations, and so on. DataSource is the original configuration of the data, configLocation is the global configuration file of xml, and mapperLocations is the location of the specified mapper.xml.

At this point, I believe you have a deeper understanding of "how mybatis uses spring extension points to integrate". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report