springmvc+mybatis Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

原因是因为Mybatis3依赖的jar位 mybatis-spring-1.2.0.jar,这个版本及以上的版本中对SqlSessionDaoSupport类中的'sqlSessionFactory'或'sqlSessionTemplate'注入方式进行了调整。

创建BaseDao

package com.allcam.provider.daoall;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 基础DAO
 * 
 * @author YiZhichao
 * @version [版本号, 2015-5-11]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public abstract class BaseDao<T> extends SqlSessionDaoSupport
{
    @Autowired
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate)
    {
        super.setSqlSessionTemplate(sqlSessionTemplate);
    }
    
    protected <S> S getMapper(Class<S> clazz)
    {
        return getSqlSession().getMapper(clazz);
    }
}

 

dao继承BaseDao

package com.allcam.provider.daoall.impl;

import java.util.Map;

import org.springframework.stereotype.Repository;

import com.allcam.provider.daoall.BaseDao;
import com.allcam.provider.daoall.CameraDao;
import com.allcam.provider.pojo.CameraInfo;
import com.allcam.provider.sys.exception.DBException;

@Repository
public class CameraDaoImpl extends BaseDao<Object> implements CameraDao
{
    /**
     * 获取摄像头信息
     * 
     * @param map 查询条件
     * 
     * @return CameraInfo 查询到的结果
     */
    public CameraInfo getCameraInfo(Map<String, Object> map)
        throws DBException
    {
        CameraInfo cameraInfo = new CameraInfo();
        try
        {
            cameraInfo = getSqlSession().selectOne("com.allcam.provider.daoall.CameraDao.getCameraInfo", map);
        }
        catch (Exception e)
        {
            throw new DBException("CameraDaoImpl getCameraInfo(map) error...", e);
        }
        return cameraInfo;
    } 
}

applicationContext-resource.xml

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	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/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">
	<context:property-placeholder
		ignore-resource-not-found="true" location="classpath:properties/*.properties" />
<!-- <bean id="dataSourcejndi" class="org.springframework.jndi.JndiObjectFactoryBean" p:jndiName="imsdatasource"/> -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="driverClassName" value="${db.driverClassName}" />
		<!-- 基本属性 url、user、password -->
		<property name="url" value="${db.url}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />
		<!-- 配置初始化大小、最小、最大 -->
		<property name="initialSize" value="${database.connection.idle}" />
		<property name="minIdle" value="${database.connection.min}" />
		<property name="maxActive" value="${database.connection.max}" />
		<!-- 配置获取连接等待超时的时间 -->
		<property name="maxWait" value="60000" />
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="validationQuery" value="${db.testsql}" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
		<property name="breakAfterAcquireFailure" value="false" /> <!-- 自动重连接 -->
		<!-- 配置监控统计拦截的filters -->
		<!-- <property name="filters" value="stat" /> -->
	</bean>
	<!-- 数据源Base定义 -->
	<!-- 默认数据源 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"
		p:nativeJdbcExtractor-ref="nativeJdbcExtractor" />
	<!-- 国际化资源文件 -->
	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
		p:useCodeAsDefaultMessage="true">
		<property name="basenames">
			<list>
				<!-- 默认加载的资源文件在src目录下或其他classpath下 -->
				<value>classpath:properties/errMessage</value>
				<value>classpath:properties/messages</value>
				<value>classpath:properties/common</value>
			</list>
		</property>
	</bean>
	<tx:annotation-driven />
	<bean id="nativeJdbcExtractor" lazy-init="true" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" />
	<bean id="lobHandler" lazy-init="true" class="org.springframework.jdbc.support.lob.OracleLobHandler">
		<property name="nativeJdbcExtractor">
			<ref bean="nativeJdbcExtractor" />
		</property>
	</bean>
	<!-- Ibatis 部分配置导入 -->
	<aop:aspectj-autoproxy />
	<aop:aspectj-autoproxy proxy-target-class="true" />
	<import resource="applicationContext-mybatis.xml" />
	<import resource="applicationContext-system.xml" />
	<import resource="dubbo-provider.xml"></import>
</beans>

applicationContext-mybatis.xml

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	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/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">

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis/mybatisconfig.xml" />
		<property name="mapperLocations" value="classpath*:mybatis/sqlxml/*.xml" />
		<property name="failFast" value="true" />
<!-- 		<property name="typeAliasesPackage" value="com.allcam.system.manager" /> -->
		<!-- <property name="typeAliasesSuperType" value="com.raising.system.modules.base.vo.BasePoJoVo" 
			/>  
		<property name="plugins">
			<array>
				<bean id="paginationinterceptor"
					class="com.allcam.system.interceptor.PageInterceptor" />
			</array>
		</property>-->
	</bean>

	<!-- <property name="lobHandler" ref="lobHandler" /> -->
<!-- 	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
		<property name="basePackage" value="com.allcam.provider" />
	</bean> -->
	 
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
		<property name="basePackage" value="com.allcam.provider" />
	</bean>
	
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory" />
		<!-- <constructor-arg index="1" value="SIMPLE" /> -->
		<!--<constructor-arg index="1" value="BATCH" /> --><!-- 如果想要进行批量操作可加入这个属性 -->
	</bean>

	<!-- 注解配置事务 -->
	<!-- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" 
		mode="aspectj" /> -->
	<!-- <tx:annotation-driven /> -->
	<!-- 定义事务管理 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 定义事务管理拦截器 -->
	<!-- <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
		<property name="transactionManager" ref="transactionManager" /> <property 
		name="transactionAttributes"> <props> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> 
		<prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> -->

	<!-- 事务代理拦截器的配置 -->
	<bean id="baseTransactionProxyTemplate" abstract="true"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="save*">PROPAGATION_REQUIRED</prop> 
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
				<prop key="add*">PROPAGATION_REQUIRED</prop> 
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop> 
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> 
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> 
				<prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>  
			</props>
		</property>
	</bean>

	<!-- Aop-声明式事务的配置 -->
	<!-- 以AspectJ方式 定义 AOP -->
	<aop:aspectj-autoproxy proxy-target-class="true"/>
	<!-- <tx:jta-transaction-manager /> -->
	<aop:config>
		<aop:pointcut id="bidMethods"
			expression="execution(* com.allcam.provider.modules.*.impl.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="bidMethods" />
	</aop:config>
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			 <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" /> 
		     <tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED" /> 
		     <tx:method name="select*" read-only="true" propagation="NOT_SUPPORTED" /> 
		     <tx:method name="query*" read-only="true" propagation="NOT_SUPPORTED" /> 
		     <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
		     <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> 
		     <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> 
		     <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> 
		     <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> 
		</tx:attributes>
	</tx:advice>
	<!-- 定义代理自动管理事务 -->
	<!-- <bean id="ProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
		<property name="beanNames"> <list> <value>*testService</value> </list> </property> 
		<property name="interceptorNames"> <list> <value>transactionInterceptor</value> 
		</list> </property> </bean> -->
</beans>

 

 

转载于:https://my.oschina.net/yizhichao/blog/872296

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值