`
lwx522
  • 浏览: 35332 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Spring+ibatis+c3p0+aop事务管理整合配置样例

 
阅读更多
<?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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <!-- 加载配置文件 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
    </bean>
        
	<!-- 配置数据源 -->
 	<bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
	    <property name="driverClass" value="com.mysql.jdbc.Driver" />
	    <property name="jdbcUrl">
        	<value><![CDATA[jdbc:mysql://${db.host}:${db.port}/${db.database}?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull]]></value>
	    </property>
		<property name="user" 				value="${db.userName}" />
		<property name="password" 			value="${db.password}" />
		<property name="maxPoolSize" 		value="20" />
		<property name="minPoolSize" 		value="5" />
		<property name="maxStatements" 		value="200" />
		<property name="initialPoolSize" 	value="10" />
		<property name="maxIdleTime" 		value="60"/>
		<property name="idleConnectionTestPeriod" 	value="30" />
	    <property name="testConnectionOnCheckin" 	value="false" />
	    <property name="testConnectionOnCheckout" 	value="false" />
	    <property name="preferredTestQuery" 		value="SELECT 1 FROM DUAL" />
    </bean>   

	<!-- 配置ibatis -->
	<bean id="sqlMapClient"
		class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation"
			value="classpath:sqlMapConfig.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="sqlMapClientTemplate"
		class="org.springframework.orm.ibatis.SqlMapClientTemplate">
		<property name="sqlMapClient" ref="sqlMapClient" />
	</bean>

	<!-- 事务的配置 -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" timeout="20" />
			<tx:method name="add*" 	propagation="REQUIRED" timeout="20" />
			<tx:method name="update*" propagation="REQUIRED" timeout="20" />
			<tx:method name="delete*" propagation="REQUIRED" timeout="20" />
			<tx:method name="*" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:pointcut id="serviceTransaction"
			expression="execution(* com.surfilter.collector.service.*.*.*(..))" />
		<aop:advisor advice-ref="txAdvice"
			pointcut-ref="serviceTransaction" />
	</aop:config>
</beans>

 各框架版本  spring 2.5

                   ibatis-2.3.4.726

                   c3p0  0.9

 

 

事务管理由spring的aop配置完成,事务封装在service层。  Dao层操作数据库由ibatis的模板封装类SqlMapClientTemplate完成。

 

package com.surfilter.collector.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;

import com.surfilter.collector.bean.ForumTopic;
import com.surfilter.collector.dao.base.ForumTopicDao;

@Repository
public class ForumTopicDaoImpl implements ForumTopicDao {

	@Autowired
	private SqlMapClientTemplate sqlMapClientTemplate;
	
	@Override
	public void addForumTopic(ForumTopic forumTopic) {
		Object obj= sqlMapClientTemplate.insert("addForumTopic", forumTopic);
	    if(obj!=null){
	    	forumTopic.setId((Integer)obj);
	    }
	    
	}

	@Override
	public int findForumTopicCount(String keyID) {
		return (Integer) sqlMapClientTemplate.queryForObject("findForumTopicCount", keyID);
	}

	@Override
	public ForumTopic findForumTopicByID(int id) {
		return (ForumTopic)sqlMapClientTemplate.queryForObject("findForumTopicByID", id);
	}

	@Override
	public void updateForumTopic(ForumTopic forumTopic) {
		sqlMapClientTemplate.update("updateForumTopic", forumTopic);
	}

	@SuppressWarnings("unchecked")
    @Override
    public List<ForumTopic> findForumTopics(ForumTopic forumTopic) {
        return sqlMapClientTemplate.queryForList("findForumTopics", forumTopic);
    }
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ForumTopic> findForumTopicsByForumTopic(ForumTopic forumTopic) {
		return sqlMapClientTemplate.queryForList("findForumTopicsByForumTopic",forumTopic);
	}
	
	@Override
	public Integer findForumTopicsByForumTopicCount(ForumTopic forumTopic) {
		return (Integer) sqlMapClientTemplate.queryForObject("findForumTopicsByForumTopicCount",forumTopic);
	}
	
	
}

 

分享到:
评论

相关推荐

    c3p0-0.9.1.2等等

    c3p0-0.9.1.2 cglib-2.1.3 cglib-2.1.3 cglib-nodep-2.1_3 commons-attributes-api commons-attributes-compiler commons-codec commons-collections-2.1.1 commons-dbcp commons-fileupload commons-httpclient ...

    spring applicationContext 配置文件

    &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt; &lt;property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/&gt; ...

    spring mvc开发集合包

    c3p0-0.9.1.2.jar c3p0数据源 commons-fileupload-1.2.2.jar commons-io-2.0.1.jar commons-lang-2.5.jar commons-logging-1.1.1.jar ibatis-2.3.4.726.jar ibatis包 jstl.jar jstl支持包 log4j-1.2.15.jar log4j...

    培训体系管理系统-oracle-ssh

    c3p0-0.9.0.jar c3p0-0.9.1.jar cglib-2.1.3.jar cglib-nodep-2.1_3.jar classes12.jar commons-attributes-api.jar commons-attributes-compiler.jar commons-beanutils.jar commons-codec.jar commons-...

    spring-framework-3.0.5.RELEASE-dependencies-1

    com.mchange.c3p0 com.opensymphony.quartz com.oracle.toplink.essentials com.springsource.bundlor com.springsource.util com.sun.syndication com.thoughtworks.xstream 2号包: net.sourceforge.serp ...

    spring-framework-3.0.5.RELEASE-dependencies-6

    com.mchange.c3p0 com.opensymphony.quartz com.oracle.toplink.essentials com.springsource.bundlor com.springsource.util com.sun.syndication com.thoughtworks.xstream 2号包: net.sourceforge.serp ...

    spring-framework-3.0.5.RELEASE-dependencies-7

    com.mchange.c3p0 com.opensymphony.quartz com.oracle.toplink.essentials com.springsource.bundlor com.springsource.util com.sun.syndication com.thoughtworks.xstream 2号包: net.sourceforge.serp ...

    spring-framework-3.0.5.RELEASE-dependencies-5

    com.mchange.c3p0 com.opensymphony.quartz com.oracle.toplink.essentials com.springsource.bundlor com.springsource.util com.sun.syndication com.thoughtworks.xstream 2号包: net.sourceforge.serp ...

    spring-framework-3.0.5.RELEASE-dependencies-4

    com.mchange.c3p0 com.opensymphony.quartz com.oracle.toplink.essentials com.springsource.bundlor com.springsource.util com.sun.syndication com.thoughtworks.xstream 2号包: net.sourceforge.serp ...

    spring-framework-3.0.5.RELEASE-dependencies-8

    com.mchange.c3p0 com.opensymphony.quartz com.oracle.toplink.essentials com.springsource.bundlor com.springsource.util com.sun.syndication com.thoughtworks.xstream 2号包: net.sourceforge.serp ...

    spring-framework-3.0.5.RELEASE-dependencies-3

    com.mchange.c3p0 com.opensymphony.quartz com.oracle.toplink.essentials com.springsource.bundlor com.springsource.util com.sun.syndication com.thoughtworks.xstream 2号包: net.sourceforge.serp ...

    spring-framework-3.0.5.RELEASE-dependencies-2

    com.mchange.c3p0 com.opensymphony.quartz com.oracle.toplink.essentials com.springsource.bundlor com.springsource.util com.sun.syndication com.thoughtworks.xstream 2号包: net.sourceforge.serp ...

    javaweb项目常用jar包

    c3p0-0.9.1.2.jar cas-client-core-3.3.3.jar cglib-2.2.2.jar commons-beanutils-1.8.0.jar commons-cli-1.2.jar commons-codec-1.9.jar commons-collections-3.2.1.jar commons-dbcp-1.4.jar commons-...

Global site tag (gtag.js) - Google Analytics