编程语言
首页 > 编程语言> > java实验之转账

java实验之转账

作者:互联网

了解基于XML、注解的声明式AspectJ开发案例代码的编写,了解 Spring JDBC 的配置,了解Spring JdbcTemplate 的常用方法,了解基于XML方式和基于Annotation方式的声明式事务管理的实现。

controller类  
package gsg.controller;
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;
import gsg.service.AccountService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AccountController {
	@Resource(name="accountService")
	private AccountService accountService;
@Test
	public void demo1(){
		accountService.insertaccount("张三", 100000d);
		System.out.println("存款成功");
	}
	@Test
	public void demo2(){
		accountService.transfer("张三", "张二", 2d);
		System.out.println("转账成功");
	}
	@Test
	public void demo3(){
		accountService.insertaccount("张六", 100000d);
		accountService.transfer("张56", "张5", 1d);
		System.out.println("转账失败");}	}
  dao类
package gsg.dao;

public interface AccountDao {
	public void insertaccount(String name, Double money);
	public String findaccount(String name);
	public void outMoney(String from ,Double money);
	public void inMoney(String to ,Double money);
}
package gsg.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import gsg.entity.Account;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
	@Override
	public void outMoney(String from, Double money) {
		this.getJdbcTemplate().update("update account set money = money - ? where name = ?", money, from);
	}
	@Override
	public void inMoney(String to, Double money) {
		this.getJdbcTemplate().update("update account set money = money + ? where name = ?", money, to);
	}
	@Override
	public void insertaccount(String name, Double money) {
		// TODO Auto-generated method stub
		this.getJdbcTemplate().update("insert into account values (null,?,?)", name, money);
	}
	@Override
	public String findaccount(String name) {
		// TODO Auto-generated method stub
		Account account = this.getJdbcTemplate().queryForObject("select * from account where name = ?",
				new MyRowMapper(), name);
		return account.toString();
	}

	class MyRowMapper implements RowMapper<Account> {

		@Override
		public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
			Account account = new Account();
			account.setId(rs.getInt("id"));
			account.setName(rs.getString("name"));
			account.setMoney(rs.getDouble("money"));
			return account;	}	}}
entity类
package gsg.entity;
public class Account {
	@Override
	public String toString() {
		return "Account [id=" + id + ", name=" + name + ", money=" + money + "]";
	}
	private int id;
	private String name;
	private double money;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	}
service类
package gsg.service;
public interface AccountService {
	public void insertaccount(String name,Double money);
	public String findaccount(String name);
	public void transfer(String from,String to,Double money);
	
}
package gsg.service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import gsg.dao.AccountDao;
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)
public class AccountServiceImpl implements AccountService {
	// 注入DAO:
	private AccountDao accountDao;
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	@Override
	public void transfer( String from,  String to,  Double money) {
		
		accountDao.outMoney(from, money);
		//int d = 1/0;
		accountDao.inMoney(to, money);
			}
	@Override
	public void insertaccount(String name, Double money) {
		// TODO Auto-generated method stub
		accountDao.insertaccount(name, money);
	}
	@Override
	public String findaccount(String name) {
		// TODO Auto-generated method stub
		String text=accountDao.findaccount(name);
		return text;	}}
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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd">
	<context:gsgponent-scan
		base-package="gsg" />
	<aop:aspectj-autoproxy />
	<bean id="accountDao" class="gsg.dao.AccountDaoImpl">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<bean id="accountService" class="gsg.service.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"/>
	</bean>
	<context:property-placeholder
		location="classpath:jdbc.properties" />
	<bean id="dataSource"
	class="gsg.mchange.v2.c3p0.gsgboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<bean id="transactionManager"	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<tx:annotation-driven
		transaction-manager="transactionManager" />

</beans>
jdbc配置文件
jdbc.driverClass=gsg.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///practice1?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
log4j.properties配置文件



log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.rootLogger= info, stdout

标签:转账,java,String,money,void,实验,import,public,name
来源: https://blog.csdn.net/aiaias/article/details/120983637