spring mvc+mybatis整合讀取數據源配置文件時報空指針異常

個人配置:web

<!-- 引入jdbc配置文件 -->  
<context:property-placeholder location="classpath:jdbc.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="initialSize"><value>5</value></property> 
        <property name="minIdle"><value>5</value></property> 
<property name="removeAbandoned"><value>true</value></property>  
        <property name="removeAbandonedTimeout"><value>30</value></property> 
        <property name="logAbandoned"><value>true</value></property> 
        <property name="maxActive"><value>50</value></property> 
        <property name="maxWait"><value>30000</value></property> 
</bean> 
    
    <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
  
    <!-- 建立SqlSessionFactory,同時指定數據源 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
   <property name="mapperLocations" value="classpath:com/tongtech/esbserver/mapping/*.xml"></property> 
        <!-- <property name="typeHandlersPackage" value="com.tx.core.mybatis.handler"></property>  
   <property name="failFast" value="true"></property> --> 
    </bean> 
    
      
    <!-- Mapper接口所在包名,Spring會自動查找其下的Mapper -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.tongtech.esbserver.dao" /> 
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 
    </bean>

mybatis官方已經給出解決方案了,就是用本身的xmlns,看下面的配置,注意紅色部分 
<?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:context="http://www.springframework.org/schema/context" 
      xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" 
       xmlns:cache="http://www.springframework.org/schema/cache" 
       xmlns:task="http://www.springframework.org/schema/task" 
       xsi:schemaLocation=" 
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx-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/task 
          http://www.springframework.org/schema/task/spring-task-3.2.xsd 
          http://www.springframework.org/schema/cache 
          http://www.springframework.org/schema/cache/spring-cache-3.1.xsd 
          http://mybatis.org/schema/mybatis-spring 
          http://mybatis.org/schema/mybatis-spring.xsd

       default-autowire="byName"> 
    <context:annotation-config/> 
    <context:property-placeholder location="classpath:jdbc.properties"/> 

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
        <property name="url" value="${jdbc.url}"/> 
        <property name="username"> 
            <value>${jdbc.username}</value> 
        </property> 
        <property name="password" value="${jdbc.password}"/> 
        <property name="filters"> 
            <value>stat,log4j</value> 
        </property> 

        <property name="maxActive"> 
            <value>20</value> 
        </property> 
        <property name="initialSize"> 
            <value>1</value> 
        </property> 
        <property name="maxWait"> 
            <value>60000</value> 
        </property> 
        <property name="minIdle"> 
            <value>1</value> 
        </property> 

        <property name="timeBetweenEvictionRunsMillis"> 
            <value>60000</value> 
        </property> 
        <property name="minEvictableIdleTimeMillis"> 
            <value>300000</value> 
        </property> 

        <property name="validationQuery"> 
            <value>SELECT 'x'</value> 
        </property> 
        <property name="testWhileIdle"> 
            <value>true</value> 
        </property> 
        <property name="testOnBorrow"> 
            <value>false</value> 
        </property> 
        <property name="testOnReturn"> 
            <value>false</value> 
        </property> 

        <property name="poolPreparedStatements"> 
            <value>true</value> 
        </property> 
        <property name="maxOpenPreparedStatements"> 
            <value>20</value> 
        </property> 
    </bean> 

    <bean id="transactionManager" 
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <property name="dataSource" ref="dataSource"/> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager"/> 

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource"/> 
        <property name="configLocation" value="classpath:mybatis-config.xml"/> 
    </bean> 

    <mybatis:scan base-package="com.xxx.web.dao.mapper"/> 


其實關鍵的配置就是<mybatis:scan>這個配置項了。 spring

相關文章
相關標籤/搜索