Description: sqlSessionFactory injection method of MapperScannerConfigurer, sessionfactory Injection

Source: Internet
Author: User

Description: sqlSessionFactory injection method of MapperScannerConfigurer, sessionfactory Injection

SqlSessionFactory injection for MapperScannerConfigurer

First, there is a configuration in Mybatis that is very convenient, saving us time to write DaoImpl (Dao implementation class). This configuration is packet scanning ....

Let's take a look at the Code:

<! -- 4: configure the package to scan the Dao interface, dynamically implement the Dao interface, and inject it into the Spring container --> <bean class = "org. mybatis. spring. mapper. MapperScannerConfigurer"> <! -- Here is the focus of today's speech --> <property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactory"> </property> <! -- Provides the Dao interface package to be scanned --> <property name = "basePackage" value = "org. seckill. dao"> </property> </bean>

In MapperScannerConfigurer, we know that there are four injection methods for sqlSessionFactory: sqlSessionFactory, sqlSessionFactoryBeanName, sqlSessionTemplate, sqlSessionTemplateBeanName, and sqlSessionFactory is outdated, next, let's talk about the benefits of this. Why should we use it!

Cause 1:

Injection of sqlSessionFactory (no need to configure) only when multiple data sources are configured, there will be multiple sqlSessionFactory. You can change the attribute to specify which sqlSessionFactory (summary on the Internet)

Cause 2 (important ):

When sqlSessionFactory is injected, the following value is the bean name of SqlSessionFactory, that is, the id of sqlSessionFactory. When mapperscannerconfigurer is started, our jdbc may appear. the properties file is not loaded. In this case, the DataSource it obtains is incorrect, because it is similar to $ {jdbc. the attributes such as url} have not been replaced. Therefore, in BeanName post-processing mode, when we use our Mybatis, it will find our corresponding sqlSessionFactory, to prevent it from initializing our sqlSessionFactory in advance.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.