基于spring中的事务的xml配置方式实现

 dao层:


package com.self.spring.dao;

import com.self.spring.pojo.User;

public interface IUserDao {

	public Integer addUser(User user);
	
	public Integer update(User user);
}
package com.self.spring.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.self.spring.dao.IUserDao;
import com.self.spring.pojo.User;

@Repository
public class UserDaoImpl implements IUserDao{
	
	@Autowired
	private JdbcTemplate template;
	private String sql;

	@Override
	public Integer addUser(User user) {
		sql="insert into accoumt(id,name,money)values(?,?,?)";
		int addResult = template.update(sql, user.getId(),user.getUserName(),user.getMoney());
//		int x=1/0;
		return addResult;
	}

	@Override
	public Integer update(User user) {

		sql="update accoumt set name=?,money=? where id=?";
		int updateResult = template.update(sql, user.getUserName(),user.getMoney(),user.getId());
		int x=1/0;
		return updateResult;
	}
	

}

service 层:

package com.self.spring.service;

import com.self.spring.pojo.User;

public interface IUserService {

	public Integer addUser(User user);
	
	public Integer updata(User user);
	
	public Integer business();
}
package com.self.spring.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.self.spring.dao.IUserDao;
import com.self.spring.pojo.User;
import com.self.spring.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {

	@Autowired
	private IUserDao dao;
	
	@Override
	//事务操作方法
	public Integer addUser(User user) {
		System.out.println("添加成功。。。");
		return dao.addUser(user);
	}

	@Override
	public Integer updata(User user) {
		
		return dao.update(user);
	}

	@Override
	public Integer business() {
		User user=new User(8,"小子",558);
		addUser(user);
		user.setId(5);
		user.setUserName("张铁柱");
		user.setMoney(9000);		
		return updata(user);	
	}

}

入口方法:

package com.self.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.self.spring.service.IUserService;

public class JavaMain {

	public static void main(String[] args) {

		ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		IUserService userService = context.getBean(IUserService.class);
//		User user=new User();
//		user.setId(5);
//		user.setUserName("程雪");
//		user.setMoney(5000);
//		System.out.println(userService.addUser(user));
		
		System.out.println(userService.business());
		
	}

}

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: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.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/aop
   https://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 扫描包配置 -->
    <context:component-scan base-package="com.self.spring.*"/>
    
    <!-- 配置加载属性参数 -->
    <context:property-placeholder location="db.properties"/>
    
    <!-- 配置数据源 -->
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
    	<property name="url"  value="${jdbc_url}"/>
    	<property name="driverClassName"  value="${jdbc_driver}"/>
    	<property name="password"  value="${jdbc_password}"/>
    	<property name="username"  value="${jdbc_username}"/>
    </bean>
    
    <!-- 配置jdbcTemplate -->
    <bean class="org.springframework.jdbc.core.JdbcTemplate" primary="true">
    	<constructor-arg name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 事务配置 -->
     <!-- 事务管理器 ,针对数据库的-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
     <!--事务管理方法 the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!-- 设置事务管理的方法 -->
        <tx:attributes>
            <!-- read-only="true" :表示只读操作;  propagation:传播行为,bus开头的方法都进行管理-->
            <tx:method name="bus*"  propagation="REQUIRED"/>  
             <!-- 其他的方法 -->        
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    
     <!--确保对任何执行都运行上述事务性建议由xxxService接口定义的操作 -->
    <aop:config>
        <aop:pointcut id="tx" expression="execution(* com.self.spring.service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="tx"/>
    </aop:config>
</beans>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值