1. 程式人生 > >(銀行轉賬)spring框架+jdbc+事務+Junit練習

(銀行轉賬)spring框架+jdbc+事務+Junit練習

完整專案的壓縮包在我的資源裡

第一步 建立表:

create table account(id int PRIMARY KEY auto_increment,name varchar(20) not null,money double not null);insert into account VALUES(null,"zhou",2000);insert into account values(null,"li",2000);

第二步 建立Account類

package cn.sdut.po;

publicclass Account {

intid;

String name;

doublemoney;

public Account() {

super();

}

public Account(intid, String name, doublemoney) {

super();

this.id = id;

this.name = name;

this.money = money;

}

publicint getId() {

returnid;

}

publicvoid setId(intid) {

this.id = id;

}

public String getName() {

returnname;

}

publicvoid setName(String name) {

this.name = name;

}

publicdouble getMoney() {

return

money;

}

publicvoid setMoney(doublemoney) {

this.money = money;

}

@Override

public String toString() {

return"Account [id=" + id + ", name=" + name + ", money=" + money + "]";

}

}

第三步 建立DAO層介面及實現類,並進行測試

package cn.sdut.dao;

package cn.sdut.dao;

import java.util.List;

import cn.sdut.po.Account;

public interface AccountDao {

public int add(Account account);

public int del(int id);

public int update(Account account);

public Account getById(int id);

public List<Account> getAll();

public int update(String name,double change);

}

package cn.sdut.dao.impl;

import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper;

import cn.sdut.dao.AccountDao;

import cn.sdut.po.Account;

publicclass AccountDaoImpl implements AccountDao{

JdbcTemplate template;

publicvoid setTemplate(JdbcTemplate template) {

this.template = template;

}

@Override

publicint add(Account account) {

    String sql="insert into account values(null,?,?)";   

returntemplate.update(sql,account.getName(),account.getMoney());

}

@Override

publicint del(intid) {

 String sql="delete from account where id=?";   

returntemplate.update(sql,id);

}

@Override

publicint update(Account account) {

 String sql="update account set name=?,money=? where id=?";   

returntemplate.update(sql,account.getName(),account.getMoney(),account.getId());

}

@Override

public Account getById(intid) {

Account ac=null;

String sql="select * from account where id=?";

RowMappermapper=(RowMapper) new BeanPropertyRowMapper<Account>(Account.class);

try{ ac=(Account)template.queryForObject(sql,mapper,id);}

catch (EmptyResultDataAccessException e) {    

}

finally{

returnac;

}

}

@Override

public List<Account> getAll() {

String sql="select * from account";

RowMappermapper=(RowMapper) new BeanPropertyRowMapper<Account>(Account.class);

List<Account> accountList=template.query(sql,mapper);

returnaccountList;

}

@Override

publicint update(String name, doublechange) {

 String sql="update account set money=money+? where name=?";   

returntemplate.update(sql,change,name);

}

}

測試:

package cn.sdut.dao.impl;

import java.util.List;

import org.junit.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.sdut.dao.AccountDao;

import cn.sdut.po.Account;

publicclass AccountDaoImplTest {

ClassPathXmlApplicationContext context=

new ClassPathXmlApplicationContext("applicationContext.xml");

AccountDao accountDao = (AccountDao)context.getBean("accountDao");

@Test

publicvoid testAdd() {

Account ac=new Account(0,"SDUT",1000);

intr=accountDao.add(ac);

System.out.println(r);

}

@Test

publicvoid testDel() {

intr=accountDao.del(1001);

System.out.println(r);

}

@Test

publicvoid testUpdateAccount() {

Account ac=new Account(1002,"LILI",1000);

intr=accountDao.update(ac);

System.out.println(r);

}

@Test

publicvoid testGetById() {

Account account = accountDao.getById(1002);

System.out.println(account);

}

@Test

publicvoid testGetAll() {

List<Account> list = accountDao.getAll();

System.out.println(list);

}

@Test

publicvoid testUpdateStringDouble() {

intr1=accountDao.update("wang", -1000);

intr2=accountDao.update("abcd", 1000);

System.out.println(r1*r2);

}

}

第四步 建立Service層介面及其實現類,並進行測試

package cn.sdut.service;

publicinterface BankService {

publicboolean transfer(String out,String in,doublechange);

}

package cn.sdut.service.impl;

import org.springframework.transaction.annotation.Transactional;

import cn.sdut.dao.AccountDao;

import cn.sdut.service.BankService;

public class BankServiceImpl implements BankService {

AccountDao accoutDao;

public void setAccoutDao(AccountDao accoutDao) {

this.accoutDao = accoutDao;

}

@Transactional(noRollbackFor=java.lang.ArithmeticException.class)

@Override

public boolean transfer(String out, String in, double change) {

int r1=accoutDao.update(out,change*(-1));

int r2=accoutDao.update(in,change);

return r1*r2>0?true:false;

}

}

測試:

package cn.sdut.service.impl;

import org.junit.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.sdut.service.BankService;

publicclass BankServiceImplTest {

ClassPathXmlApplicationContext context=

new ClassPathXmlApplicationContext("applicationContext.xml");

BankService bankService= (BankService)context.getBean("bankService");

@Test

publicvoid testTransfer() {

booleanflag=bankService.transfer("LILI", "SDUT", 500);

System.out.println(flag);

}

}

第五步 配置applicationContext.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: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-4.1.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver"/>

<property name="url" value="jdbc:mysql://localhost:3306/test" />

<property name="username" value="root"/>

<property name="password" value="root" />

</bean>

<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">

<property name="dataSource" ref="ds"/>

</bean>

<bean id="accountDao" class="cn.sdut.dao.impl.AccountDaoImpl">

<property name="template" ref="template"/>

</bean>

<bean id="bankService" class="cn.sdut.service.impl.BankServiceImpl">

<property name="accoutDao" ref="accountDao"/>

</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="ds"/>

</bean>

<tx:annotation-driven />

<!--  <tx:advice id="myTx" transaction-manager="transactionManager">

      <tx:attributes>

 <tx:method name="transfer"

  isolation="DEFAULT" propagation="REQUIRED"

  no-rollback-for="java.lang.ArithmeticException"

  />

      </tx:attributes>

   </tx:advice>

   <aop:config>

      <aop:advisor

      advice-ref="myTx"

      pointcut="execution(* transfer(..))"/>

   </aop:config> -->

</beans>