当前位置: 代码迷 >> Java Web开发 >> 请问Mybatis和Spring结合的配置有关问题,多谢
  详细解决方案

请问Mybatis和Spring结合的配置有关问题,多谢

热度:3398   发布时间:2013-02-25 21:18:46.0
请教Mybatis和Spring结合的配置问题,谢谢!
我自己搭了一个struts2+Mybatis+Spring的框架,想通过MapperScannerConfigurer来自动添加Mybatis的mapper,发现一个很奇怪的问题:

如果不使用MapperScannerConfigurer,而采取手动配置添加mapper,一切正常;
如果使用了MapperScannerConfigurer,服务器启动的时候会报错,但是报的是数据源配置读取错误,好像和MapperScannerConfigurer无关。

错误信息和spring配置文件如下:

XML code
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:aop="http://www.springframework.org/schema/aop"          xmlns:tx="http://www.springframework.org/schema/tx"           xmlns:jdbc="http://www.springframework.org/schema/jdbc"           xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd           http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"          default-autowire="byName">        <!-- 向spring注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、    PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor -->    <context:annotation-config></context:annotation-config>    <!-- 定义组件扫描的包名 -->    <context:component-scan base-package="edu.zjut" />        <!-- 导入属性配置文件 -->    <bean id="propertyConfigurer"        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <list>                <value>classpath:mysql.properties</value>            </list>        </property>    </bean>    <!-- context:property-placeholder location="classpath:mysql.properties" / -->    <!-- 定义使用C3P0连接池的数据源 -->    <bean id="dataSource"        class="com.mchange.v2.c3p0.ComboPooledDataSource">        <!-- 指定连接数据库的JDBC驱动 -->        <property name="driverClass">            <value>${jdbc.driverClassName}</value>        </property>        <!-- 连接数据库所用的URL -->        <property name="jdbcUrl">            <value>${jdbc.url}</value>        </property>        <!-- 连接数据库的用户名 -->        <property name="user">            <value>${jdbc.user}</value>        </property>        <!-- 连接数据库的密码 -->        <property name="password">            <value>${jdbc.password}</value>        </property>        <!-- 设置数据库连接池的最大连接数 -->        <property name="maxPoolSize">            <value>${jdbc.maxPoolSize}</value>        </property>        <!-- 设置数据库连接池的最小连接数 -->        <property name="minPoolSize">            <value>${jdbc.minPoolSize}</value>        </property>        <!-- 设置数据库连接池的初始化连接数 -->        <property name="initialPoolSize">            <value>${jdbc.initialPoolSize}</value>        </property>        <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->        <property name="maxIdleTime">            <value>${jdbc.maxIdleTime}</value>        </property>    </bean>        <!-- 集成myBaits框架,配置sqlSessionFatory -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="configLocation" value="classpath:mybatis-config.xml" />        <property name="dataSource" ref="dataSource" />    </bean>        <!-- 配置sqlSessionTemplate -->    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>    </bean>        <!-- mybatis自动扫描包下的mapper -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="org.mybatis.spring.sample.mapper" />        <!-- optional unless there are multiple session factories defined -->        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />    </bean>     <!-- bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="config.mybatis.mapper" />    </bean -->        <!-- 数据库的事务管理器配置 -->    <bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 使用annotation定义数据库事务,这样可以在类或方法中直接使用@Transactional注解来声明事务 -->    <tx:annotation-driven transaction-manager="transactionManager" /></beans>
  相关解决方案