spring jdbctemplate事务配置

很久没有新建一个spring项目了,一些配置都忘记了,今天新建了一个项目半天跑不起来。怎么新建web项目的我就不介绍了,总结如下:

1.首先是很简单的配置了一下web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/config/applicationContext.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
   		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
	
	<servlet>
		<servlet-name>springServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/config/applicationContext.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
  <welcome-file-list>
    <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

2.然后是配置一下applictionContext.xl文件。

<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/jdbc
	http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://www.springframework.org/schema/jee
	http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
		http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/data/jpa
	http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
		http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
	default-lazy-init="true">
	
	<context:component-scan base-package="mall.*"></context:component-scan>

	<!-- 数据库包装类 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg ref="dataSource" />
	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
			destroy-method="close">
			<property name="driverClassName" value="com.mysql.jdbc.Driver" />
			<property name="url" value="jdbc:mysql://localhost:3307/pipimall" />
			<property name="username" value="root" />
			<property name="password" value="pifeng" />
			<property name="maxActive" value="5" />
			<property name="maxIdle" value="40" />
			<property name="defaultAutoCommit" value="false" />
			<property name="timeBetweenEvictionRunsMillis" value="3600000" />
			<property name="minEvictableIdleTimeMillis" value="3600000" />
	</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="add*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true" />
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<aop:pointcut id="allManagerMethod" expression="execution(* mall.service..*.*(..))" />
		<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
	</aop:config>

	<!-- 定义JSP -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>


 这里简单的说一下,刚开始的时候因为没有配置jdbc事务,导致虽然代码执行了sql插入语句,但是始终数据库没有数据的结果,配置之后进行测试就ok了,主要是下面一段代码:

<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="add*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true" />
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<aop:pointcut id="allManagerMethod" expression="execution(* mall.service..*.*(..))" />
		<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
	</aop:config>

3.上面的一段代码是声明式事务的配置方式,还有一种方式就是编程式事务,需要代码去新建事务。

这里有两篇好文章,大家可以去看看:

http://www.cnblogs.com/Fskjb/archive/2009/11/29/1612920.html

http://www.cnblogs.com/rushoooooo/archive/2011/08/28/2155960.html

4..然后复制一下,自己的jar包,可能有些spring jar包是没有用到的,因为从官网上down下来的,不太认识几个,就一起弄进来了,有兴趣的朋友可以自行筛选

 

5.在这里还想说一下spring的jar包下载方法,因为我也找了老半天,自从spring网站改版后就不认得了。

先进入网址:http://projects.spring.io/spring-framework/,然后按下图步骤就行了;

图1:点击猫一样的按钮;

图2:点击图中链接

图3:点击图中链接

图4:

图5:点击圈圈中链接

图6:

然后就可以点击下载了,相信大家看得懂的。

ok,到这里基本结束,大家有什么指正的请留言。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值