话说:
各位读者朋友,晚上好!这里简短总结下Spring的事务管理。数据库比较简单,就不在赘述。
目录:
1.搭建环境
导包及applicationConfig.xml
2.编写dao层\service层
3.编写测试类
4.配置文件
5.总结
1.搭建环境
导包及applicationConfig.xml
主要包:
4个核心包:core bean context expression
2个日志包:log4j 和 commons-logging
4个面向切面包:aop aspect aspectj aopalliance
1个连接MySQL数据库包:mysql-jdbc
3个事务管理相关包: c3p0 tx(Spring jdbc 用) spring-jdbc
1个测试包 test
大体15个包
配置文件放在最后,依托于各层。
2.编写dao层\service层
整体架构如下:
AccountDao
AccountDaoImpl
AccountDao
package com.hmc.dao;
/**
*
*2018年1月11日
*User:Meice
*下午4:01:33
*/
public interface AccountDao {
//少钱
void lessAccount(int userId,double money);
//多钱
void addAccount(int userId,double money);
}
AccountDaoImpl
package com.hmc.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
*
*2018年1月11日
*User:Meice
*下午4:03:26
*/
@Repository("accountDao")
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
/*@Autowired
private JdbcTemplate jdbcTemplate;*/
@Override
public void lessAccount(int userId, double money) {
String sql = "update account set money = money - ? where userId = ?";
super.getJdbcTemplate().update(sql,money,userId);
//jdbcTemplate.update(sql,money,userId);
}
@Override
public void addAccount(int userId, double money) {
String sql = "update account set money = money +? where userId = ?";
super.getJdbcTemplate().update(sql,money,userId);
//jdbcTemplate.update(sql,money,userId);
}
}
Service层
AccountService
AccountServiceImpl
AccountService
package com.hmc.service;
/**
*
*2018年1月11日
*User:Meice
*下午4:43:55
*/
public interface AccountService {
void doAccount(int from ,int to,double money);
}
AccountServiceImpl
package com.hmc.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import com.hmc.dao.AccountDao;
/**
*
*2018年1月11日
*User:Meice
*下午4:46:00
*/
@Service
public class AccountServiceImpl implements AccountService{
@Autowired
private AccountDao accountDao;
@Autowired
private TransactionTemplate transactionTemplate;
@Override
public void doAccount(int from, int to, double money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
accountDao.lessAccount(from, money);
int a = 1/0;//一旦程序发生异常,就要用到事务处理!
accountDao.addAccount(to, money);
}
});
}
}
3.编写测试类
TestAccount
package com.hmc.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.hmc.service.AccountService;
import com.hmc.service.AccountServiceImpl;
/**
*
*2018年1月11日
*User:Meice
*下午4:50:51
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationConfig.xml")
public class TestAccount {
@Autowired
private AccountService accountService;
@Test
public void test() {
accountService.doAccount(1, 2, 500);
}
}
4.配置文件
applicationConfig.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!--扫描注解 -->
<context:component-scan base-package="com.hmc"></context:component-scan>
<!--连接数据库 -->
<bean name="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_spring01"></property>
<property name="user" value="root"></property>
<property name="password" value="119913"></property>
</bean>
<!-- 把jdbcTemplate交给spring管理 -->
<!-- <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> -->
<!-- 这里要特别注意,如果extends jdbcDaoSupport,就必须属性注入dataSource,否则找不到;如果要用自己的jdbcTemplate,
那么,这个类就不要extends jdbcDaoSupport!
-->
<!-- 使用extends jdbcSupport里面自带的getTemplate()方法直接调用数据库 -->
<bean name="accountDao" class="com.hmc.dao.AccountDaoImpl">
<property name="dataSource" ref="ds"></property>
</bean>
<!-- 配置事务 -->
<!--事务管理器dataSourceTransactionManager ,依赖连接池(依赖注入) -->
<bean name="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"></property>
</bean>
<!-- 事务模板类 依赖事务管理器-->
<bean name="tt" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="dataSourceTransactionManager"></property>
<!-- 这里是为属性赋值,要和该类中属性一致
private PlatformTransactionManager transactionManager;
-->
</bean>
</beans>
5.总结
1)注入属性有2个作用:1)方便调用属性中的方法;
2)为属性赋值!
一定要清楚这2个作用,只有分辨清楚后,配置的时候,什么时候name属性不能乱起,什么时候可以乱起,才会心中有数。!
2)Causedby:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘accountDaoImpl’: Unsatisfied dependency expressed through field ‘jdbcTemplate’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘org.springframework.jdbc.core.JdbcTemplate’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
这表明没有这个类,jdbcTemplate,没有这个类的bean的配置!因为这个类初始化要还要配置属性dataSource,不是你自己的类,所以没法加注解,只有在xml中配置。
3)
.Causedby:org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with name ‘accountServiceImpl’: Unsatisfied dependency
expressedthroughfield’transactionTemplate’;nestedexceptionisorg.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘tt’ defined
inclasspathresource[applicationConfig.xml]:Errorsettingpropertyvalues;nestedexceptionisorg.springframework.beans.NotWritablePropertyException:Invalidproperty’aa’ofbeanclass[org.springframework.transaction.support.TransactionTemplate]: Bean property ‘aa’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
表明属性注入的时候,如果是为了把这个属性实例化,调用方法,那么名字可以随意;如果是为属性赋值,那么就要用类中的属性名,也就是set方法中属性名字的小写。
好了,晚安!下期预告:bbs个人资料修改!
本文详细介绍了一个基于Spring框架的转账业务场景实现,包括环境搭建、DAO层与Service层编写、事务处理配置等内容。
2万+

被折叠的 条评论
为什么被折叠?



