在mybatis和spring整合中使用spring的MapperScannerConfigurer 运行出错

渊兮 发布于 2016/05/20 18:50
阅读 691
收藏 0

@minidai 你好,想跟你请教个问题:在mybatis和spring整合中使用spring的MapperScannerConfigurer 运行出错

报错:

DEBUG [main] - Initializing new StandardEnvironment
DEBUG [main] - Adding [systemProperties] PropertySource with lowest search precedence
DEBUG [main] - Adding [systemEnvironment] PropertySource with lowest search precedence
DEBUG [main] - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
DEBUG [main] - Looking for matching resources in directory tree [E:\myworkspace\mybatis_spring3\bin\com\nemo\ssm\mapper]
DEBUG [main] - Searching directory [E:\myworkspace\mybatis_spring3\bin\com\nemo\ssm\mapper] for files matching pattern [E:/myworkspace/mybatis_spring3/bin/com/nemo/ssm/mapper/**/*.class]
DEBUG [main] - Resolved location pattern [classpath*:com/nemo/ssm/mapper/**/*.class] to resources [file [E:\myworkspace\mybatis_spring3\bin\com\nemo\ssm\mapper\UserMapper.class], file [E:\myworkspace\mybatis_spring3\bin\com\nemo\ssm\mapper\UserMapperTest.class]]
 INFO [main] - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@71318ec4: defining beans [org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,dataSource,sqlSessionFactory,org.mybatis.spring.mapper.MapperScannerConfigurer#0]; root of factory hierarchy

spring的配置文件applicationContext.xml:

<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       <property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean>
<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/mybatis_config.xml" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.nemo.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>

注:如果使用使用MapperFactoryBean的话 不报错。用批量扫描MapperScannerConfigurer的话就会报这一错误。实在想不明白。搜了很多也没找到正确的答案。。。 希望你能给我解答,谢谢!
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.nemo.ssm.mapper.UserMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

加载中
0
minidai
minidai
你可以看看我的,我是这样配置的
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> -->
        <!-- 自动扫描mapping.xml文件 -->    
        <property name="mapperLocations" value="classpath:mybatis/mapper/**/*.xml" ></property> 
    </bean> 

<!-- DAO接口所在包名,Spring会自动查找其下的类  配置扫描器-->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.appmanager.dao" />  
        <property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"></property>  
    </bean>



返回顶部
顶部