background
Recently, the project is about to be launched, and a data migration program needs to be developed. The main function of the program is to query the data in one database and import it into another database after a series of processing. Considering the convenience and speed of development. Naturally, I thought of integrating it with spring and mybatis. Even using mybatis' automatic code generation can save a lot of development of dao layers.
The pit of integration
Previous project: I have had this kind of similar program before, so I directly used the configuration of spring and mybatis integration to modify it. The previous integrated configuration is as follows:
1. Considering the configurability of the database url and username and password, put this information into the properties file. Used in spring configuration file
<context:property-placeholder location="classpath:config.properties" />
2. The integrated configuration of mybatis and spring in the spring configuration file is like this
<bean id="sqlSessionFactory"><property name="dataSource" ref="dataSource" /></bean><bean><property name="basePackage" value="com.lagou.chat.record.transfer.dao" /></bean>
There is no problem with the above configuration. So just copy the configuration to the new project
Current project: Copy the configuration of the old project, but the new project needs to connect to two databases, so naturally two data sources (record and im) are needed, so the old configuration is modified as follows
1. The configuration of using properties files remains unchanged
2. Previously, because there was only one data source (a sqlSessionFactory), the <property name="sqlSessionFactory" ref="sqlSessionFactory"/> was not configured under the MapperScannerConfigurer. Because the default sqlSessionFactory is used. But now there are two data sources, not specifying them will definitely lead to confusion. So the configuration is modified as follows
<bean id="record_sqlSessionFactory"><property name="dataSource" ref="record_dataSource" /></bean><bean id="config1"><property name="basePackage" value="com.xxx.util.rollback.record.dao" /><property name="sqlSessionFactory" ref="record_sqlSessionFactory" /></bean> <bean id="im_sqlSessionFactory"><property name="dataSource" ref="im_dataSource" /></bean><bean id="config2"><property name="basePackage" value="com.xxx.util.rollback.im.dao" /><property name="sqlSessionFactory" ref="im_sqlSessionFactory" /></bean>
The result is that when running a new project, the properties such as ${jdbc.url}, ${jdbc.name} in the spring configuration file cannot be replaced by the specified values in the properties. At first, I naturally didn't expect that it was because of the integration of spring and mybatis, so I kept checking whether the spring configuration file was wrong, whether the properties file was wrong, whether the properties file was not referenced or the properties file was not compiled into the classpath directory, etc. Of course, if the analysis does not analyze the cause of the problem, it is naturally impossible to find a solution to the problem. I had to turn to the Internet. Finally found the answer
Fixed method: Change the configuration to the following, the problem was solved:
<bean id="record_sqlSessionFactory"><property name="dataSource" ref="record_dataSource" /></bean><bean id="config1"><property name="basePackage" value="com.xxx.util.rollback.record.dao" /><property name="sqlSessionFactoryBeanName" value="record_sqlSessionFactory" /></bean> <bean id="im_sqlSessionFactory"><property name="dataSource" ref="im_dataSource" /></bean><bean id="config2"><property name="basePackage" value="com.xxx.util.rollback.im.dao" /><property name="sqlSessionFactoryBeanName" value="im_sqlSessionFactory"/></bean>
It is to change the sqlSessionFactory property to sqlSessionFactoryBeanName. Of course, ref must also be changed to value. Because the sqlSessionFactoryBeanName property is a string type
reason
When using org.mybatis.spring.mapper.MapperScannerConfigurer for automatic scanning in spring, if the sqlSessionFactory is set, the PropertyPlaceholderConfigurer may be invalid, that is, expressions such as ${jdbc.username} will not be able to obtain the contents in the properties file.
This is because the MapperScannerConigurer is actually in the parse loading bean definition stage. If you set the sqlSessionFactory at this time, it will cause some classes to be initialized in advance. At this time, the PropertyPlaceholderConfigurer has not had time to replace the variables in the definition, resulting in the expression being copied as a string. However, if the sqlSessionFactory property is not set, it is necessary to ensure that the sessionFactory name in spring must be sqlSessionFactory, otherwise it will not be automatically injected.
The above is the pitfalls of Spring integrating Mybatis when using <context:property-placeholder> that I have introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!