Spring+Mybatis集成,出现java.lang.ClassNotFoundException: ${jdbc.driverClass}

2 篇文章 0 订阅
2 篇文章 0 订阅
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<context:component-scan base-package="com.ssm.aop,com.ssm.dao.*,com.ssm.service.*"></context:component-scan>

	<context:property-placeholder location="classpath:jdbc-oracle.properties" />

	<!-- 使用c3p0配置数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
	</bean>

	<!-- 配置sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="mapperLocations">
			<list>
				<value>classpath:mybatis/UsersMapper.xml</value>
			</list>
		</property>
	</bean>

	<!-- 配置sqlSessionTemplate -->
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg ref="sqlSessionFactory"></constructor-arg>
	</bean>

	<!-- 配置MapperScannerConfigurer,它 将 会 查 找 类 路 径 下 的 映 射 器 并 自 动 将 它 们 创 建 
		成 MapperFactoryBean。 -->
	<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 为映射器接口文件设置基本的包路径。 你可以使用分号或逗号作为分隔符设置多于一个的包路径。每个映射器将会在指定的包路径中递归地被搜索到。 -->
		<property name="basePackage" value="com.ssm.dao"></property>
		<!-- 
			http://www.mybatis.org/spring/zh/mappers.html#MapperScannerConfigurer
			注意:如果配置sqlSessionFactory JavaBean时使用如下配置 <property name="sqlSessionFactory" 
			ref="sqlSessionFactory"></property> 将会出现java.lang.ClassNotFoundException: 
			${jdbc.driverClass}异常。MapperScannerConfigurer属性不支持使用了 PropertyPlaceholderConfigurer 
			的属性替换,因为会在 Spring容器中之前来加载它。但是,你可以使用 PropertiesFactoryBean和SpEL表达式来作为替代。 注 
			意 , 没 有 必 要 去 指 定 SqlSessionFactory或SqlSessionTemplate,因 为 MapperScannerConfigurer 
			将会创建 MapperFactoryBean,之后自动装配。但是,如果你使用了一个 以上的 DataSource,那 么自动装配可能会失效 。这种 
			情况下,你可 以使用 sqlSessionFactoryBeanName或sqlSessionTemplateBeanName属性来设置正确的 bean名 
			称来使用。这就是它如何来配置的,注意 bean的名称是必须的,而不是 bean的引用,因 此,value属性在这里替代通常的 ref:<property 
			name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
		<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"></property>
	</bean>

	<!-- 配置spring声明式事物管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

	<!-- 通过AOP配置事物增强 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED"
				rollback-for="java.lang.Thread" />
			<tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Thread" />
			<tx:method name="update" propagation="REQUIRED"
				rollback-for="java.lang.Thread" />
			<tx:method name="find*" read-only="true" rollback-for="java.lang.Thread" />
			<tx:method name="*" propagation="REQUIRED" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:pointcut expression="execution(* com.ssm.service..*.*(..))"
			id="txPointcut" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
	</aop:config>

</beans>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值