2018.5.26
1、创建项目
spring-05

2.导入包

3、日志文件
4、创建数据库表

5.创建accountDao
package
com.lu.spring.dao;
import
javax.annotation.Resource;
import
org.springframework.jdbc.core.JdbcTemplate;
import
org.springframework.stereotype.Repository;
@Repository
(
"accountDao"
)
public
class
AccountDaoImpl
implements
AccountDao
{
@Resource
(name=
"jdbcTemplate"
)
private
JdbcTemplate jt;
//private 不走set方法 走反射。
/**
*转账汇钱
*/
@Override
public
void
addMoney
(Integer id, Double money) {
String sql =
"update lu_account set money = money + ? where id = ?"
;
jt.update(sql,money,id);
}
@Override
public
void
subMoney
(Integer id, Double money) {
String sql =
"update lu_account set money = money - ? where id = ?"
;
jt.update(sql,money,id);
}
}
|
6、创建service
package
com.lu.spring.service;
import
javax.annotation.Resource;
import
org.springframework.stereotype.Service;
import
com.lu.spring.dao.AccountDao;
@Service
(
"accountService"
)
public
class
AccountServiceImpl
implements
AccountService
{
@Resource
(name=
"accountDao"
)
private
AccountDao accountDao;
@Override
public
void
transfer
(Integer from, Integer to, Double money) {
accountDao.subMoney(from, money);
//
int
a=
9
/
0
; 无事务的用例 报异常之后只有from减钱,to不加钱。
accountDao.addMoney(to, money);
}
}
|
7、创建spring的配置文件
<?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" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <!-- spring容器来完成对象的创建 --> <!-- 注解扫描 --> <context:component-scan base-package="com.lu.spring.dao"></context:component-scan> <context:component-scan base-package="com.lu.spring.service"></context:component-scan> <!-- 加载db.properties --> <context:property-placeholder location="classpath:db.properties"/> <!-- 连接池 直接使用${xxx}引用db配置--> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- jdbcTemplate --> <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- UserDao class为userDao的实现类impl!! <bean name="userDao" class="com.lu.spring.dao.UserDaoImpl"> <property name="jt" ref="jdbcTemplate"></property> </bean> --></beans>
|
8、无事务测试用例
package
com.lu.spring.service;
import
javax.
annotation
.Resource;
import
org.junit.Test;
import
org.junit.runner.RunWith;
import
org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* 测试环境的搭建
*
@author
bitware13
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
//创建容器
@ContextConfiguration("classpath:applicationContext.xml")
public
class
TxTest
{
@Resource(name="accountService")
private
AccountService accountService;
/**
* 1->2转账
*/
@Test
public
void testTransfer() {
accountService.transfer(
1
,
2
,
1000
D);
//如果不带小数点 要加上D 说明是DOUBLE类型
}
}
|
9.测试异常中转账无法执行