spring 整合mybatis配置 MapperScannerConfigurer,无法读配置文件错误

spring 集成 mybatis 时,配置自动映射dao接口和mapper.xml 的关系,使用org.mybatis.spring.mapper.MapperScannerConfigurer 代理来实现。但是配置的时候,启动总是报数据库无法读取配置文件的错误

这里写图片描述

Caused by: java.lang.NumberFormatException: For input string: “${pool.idleConnectionTestPeriodInMinutes}”

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in class path resource [config/spring/spring-data-config.xml]: Cannot resolve reference to bean 'smkAppSqlSession' while setting bean property 'sqlSessionFactory'; nested exception is org.spri

根据报错提示 是

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactory" ref="smkAppSqlSession" />
         <property name="basePackage" value="com.**.dao" />
    </bean> 

这里配置的问题,然后去看源码

public class MapperScannerConfigurer implements BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware {

  private String basePackage;

  private boolean addToConfig = true;

  private SqlSessionFactory sqlSessionFactory;

  private SqlSessionTemplate sqlSessionTemplate;

  private String sqlSessionFactoryBeanName;

  private String sqlSessionTemplateBeanName;

  private Class<? extends Annotation> annotationClass;

  private Class<?> markerInterface;

  private ApplicationContext applicationContext;

  private String beanName;

  private boolean processPropertyPlaceHolders;

  private BeanNameGenerator nameGenerator;

看到这里有2个配置
private SqlSessionFactory sqlSessionFactory;

private String sqlSessionFactoryBeanName;
在我的配置文件中使用的是

这个时候 问题就发现了,MapperScannerConfigurer 执行的时候,如果指定了 sqlSessionFactory 实体类,就需要sqlSessionFactory 已经成功加载完成,但是 数据库配置引用类

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
        <value>classpath:config/env/env_dev/dataSource.properties</value>
        </list>
    </property>
</bean>

是在 MapperScannerConfigurer 执行后才能执行的。所以就报错了。

这里我只需要改变MapperScannerConfigurer 加载类就可以了
如下

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="smkAppSqlSession" />
         <property name="basePackage" value="com.**.dao" />
    </bean>

指定sqlSessionFactoryBeanName 就搞定了

展开阅读全文

没有更多推荐了,返回首页