SpringMVC+MyBatis,连接池采用druid,启动报错Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

spring的数据库配置文件如下:

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" /> 
<property name="user" value="scott" />
<property name="password" value="tiger" />
</bean>  -->
<!--  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>  -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
      <!-- 基本属性 url、user、password -->
      <property name="url" value="${jdbc.url}" />
      <property name="driverClassName" value="${jdbc.driverClassName}" />
      <property name="username" value="${jdbc.username}" />
      <property name="password" value="${jdbc.password}" />


      <!-- 配置初始化大小、最小、最大 -->
      <property name="initialSize" value="${jdbc.initialSize}" />
      <property name="minIdle" value="${jdbc.minIdle}" /> 
      <property name="maxActive" value="${jdbc.maxActive}" />


      <!-- 配置获取连接等待超时的时间 -->
      <property name="maxWait" value="${jdbc.maxWait}" />


      <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
      <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />


      <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
      <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />


      <property name="validationQuery" value="${jdbc.validationQuery}" />
      <property name="testWhileIdle" value="${jdbc.testWhileIdle}" />
      <property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
      <property name="testOnReturn" value="${jdbc.testOnReturn}" />


      <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
      <property name="poolPreparedStatements" value="${jdbc.poolPreparedStatements}" />
      <property name="maxPoolPreparedStatementPerConnectionSize" value="${jdbc.maxPoolPreparedStatementPerConnectionSize}" />


      <!-- 配置监控统计拦截的filters -->
      <property name="filters" value="stat" />
</bean>
<bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
             <list>  
       <value>classpath:properties/druid.properties</value>  
   </list>  
        </property>
    </bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatisConfig.xml" />
</bean>


<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<constructor-arg index="1" value="BATCH" />
</bean>


<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:aspectj-autoproxy proxy-target-class="true" />
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

empDaoImp的简单配置如下:

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

 @Repository
public class EmpDaoImpl extends SqlSessionDaoSupport implements EmpDao {
     
public List<Emp> listEmp() {
return getSqlSession().selectList("EmpDao.listEmp");
}
}

问题解释:
当时采用的是传统的继承 SqlSessionDaoSupport 的方式构建 EmpDaoImpl ,然后构建各个功能dao来继承这个EmpDaoImpl ,可以直接使用SqlSessionDaoSupport中继承来的 getSqlSession()方法来获取sqlSession,用来执行sql,原本都没有问题,但是这次用maven添加依赖时候,选择的是最新版本的 mybatis-spring-1.2.2.jar。由于mybatis-spring的这个基类不会再直接将spring配置文件中配好的 sqlSessionFactory 或者 sqlSessionTemplate注入到这个基类中,而在使用时候,SqlSessionDaoSupport的方法

解决办法:
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

 @Repository
public class EmpDaoImpl extends SqlSessionDaoSupport implements EmpDao {
     @Autowired
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        super.setSqlSessionTemplate(sqlSessionTemplate);
    }
public List<Emp> listEmp() {
return getSqlSession().selectList("EmpDao.listEmp");
}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值