mybatis整合spring使用MapperScannerConfigurer配置问题

在mybatis与spring整合过程中,由于MapperScannerConfigurer可能在数据库连接信息未替换前初始化SqlSessionFactoryBean,导致启动报错。解决方法是通过sqlSessionFactoryBeanName属性来加载,并利用BeanName后处理,确保在需要时才加载sqlSessionFactory。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 mybatis和spring整合的时候,org.mybatis.spring.mapper.MapperScannerConfigurer 中加载sqlSessionFactory的时候要用属性名sqlSessionFactoryBeanName加载,因为org.mybatis.spring.mapper.MapperScannerConfigurer在启动的时候有可能数据库的链接地址,用户名,密码还没有被替换掉,会导致加载报错,项目无法启动,报Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: ${driverClassName}这个错误。

造成这种错误的原因是: 配置文件中的${driverClassName}变量没有被替换成真正的值之前,org.mybatis.spring.mapper.MapperScannerConfigurer 已经开始初始化SqlSessionFactoryBean了,SqlSessionFactoryBean无法拿到真正的数据库的链接地址,用户名,密码,所以会出现上述问题。

对应的配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/task
   		http://www.springframework.org/schema/task/spring-task-4.0.xsd
		http://code.alibabatech.com/schema/dubbo        
		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- Mybatis 工厂 sqlSession -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource"/>	
		<property name="mapperLocations">
   			<list>
   				<value>classpath:com/pearflowers/core/dao/*/*.xml</value>
   			</list>
   		</property>

	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	    <property name="basePackage" value="com.pearflowers.*.dao"/>
	    <property name="properties">
	        <value>
	            mappers=tk.mybatis.mapper.common.Mapper
	        </value>
	    </property>
	    <!-- sqlSessionFactory这个方法已经过时了,所以出现,属性文件中的值还没有加载完成,就开始初始化 SqlSessionFactoryBean-->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
	</bean>   
		
</beans>
解决方式:

   可以使用BeanName后处理的方式,只有当去用mybatis的时候才回去加载sqlSessionFactory,避免了提前初始化sqlSessionFactory.

配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/task
   		http://www.springframework.org/schema/task/spring-task-4.0.xsd
		http://code.alibabatech.com/schema/dubbo        
		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- Mybatis 工厂 sqlSession -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource"/>	
		<property name="mapperLocations">
   			<list>
   				<value>classpath:com/pearflowers/core/dao/*/*.xml</value>
   			</list>
   		</property>

	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	    <property name="basePackage" value="com.pearflowers.*.dao"/>
	    <property name="properties">
	        <value>
	            mappers=tk.mybatis.mapper.common.Mapper
	        </value>
	    </property>
	    <!-- 这样配置以后,只有当真正去用mybatis的时候,才会去初始化SqlSessionFactoryBean-->
        <property name="sqlSessionFactoryBeanName" ref="sqlSessionFactory"/>
	</bean>   
		
</beans>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值