spring mvc+mybatis整合读取数据源配置文件时报空指针异常

我的配置:

<!-- 引入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>这个配置项了。 

转载于:https://my.oschina.net/u/1431757/blog/728912

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值