java事物的注解,事务注解配置

## 配置

~~~

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">

~~~

~~~

package com.like.domain;

import java.io.Serializable;

public class Account implements Serializable

{

private Integer id;

private String name;

private Float money;

@Override

public String toString()

{

return "Account{" +

"id=" + id +

", name='" + name + '\'' +

", money=" + money +

'}';

}

public Integer getId()

{

return id;

}

public void setId(Integer id)

{

this.id = id;

}

public String getName()

{

return name;

}

public void setName(String name)

{

this.name = name;

}

public Float getMoney()

{

return money;

}

public void setMoney(Float money)

{

this.money = money;

}

}

~~~

~~~

package com.like.dao;

import com.like.domain.Account;

public interface IAccountDao

{

Account findAccountByName(String name);

void updateAccount(Account account);

}

~~~

~~~

package com.like.dao.impl;

import com.like.dao.IAccountDao;

import com.like.domain.Account;

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

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Repository;

import java.util.List;

@Repository

public class AccountDaoImpl implements IAccountDao

{

@Autowired

private JdbcTemplate jt;

public Account findAccountByName(String name)

{

List accounts = jt.query("select * from account where name = ?", new BeanPropertyRowMapper(Account.class), name);

if (accounts.isEmpty()) {

return null;

}

if (accounts.size() > 1) {

throw new RuntimeException("结果集不唯一");

}

return accounts.get(0);

}

public void updateAccount(Account account)

{

jt.update("update account set money = ? where name = ?", account.getMoney(), account.getName());

}

}

~~~

~~~

package com.like.service;

public interface IAccountService

{

void transfer(String sourceName,String targetName,Float money);

}

~~~

~~~

package com.like.service.impl;

import com.like.dao.IAccountDao;

import com.like.domain.Account;

import com.like.service.IAccountService;

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

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

@Service

@Transactional

public class AccountServiceImpl implements IAccountService

{

@Autowired

private IAccountDao accountDao;

public void transfer(String sourceName, String targetName, Float money)

{

Account source = accountDao.findAccountByName(sourceName);

Account target = accountDao.findAccountByName(targetName);

source.setMoney(source.getMoney() - money);

target.setMoney(target.getMoney() + money);

accountDao.updateAccount(source);

int a = 1/0;

accountDao.updateAccount(target);

}

}

~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值