admin 管理员组

文章数量: 1086057

注解mysql事物管理

Spring事务管理-注解版

一、拷贝必要的jar包到工程的lib目录

二、创建spring的配置文件并导入约束

xmlns:xsi=""

xmlns:aop=""

xmlns:tx=""

xsi:schemaLocation="

.xsd

.xsd

.xsd">

三、准备数据库表和实体类

创建数据库:

create database spring;

use spring;

创建表:

create table account(

id int primary key auto_increment,

name varchar(40),

money float

)character set utf8 collate utf8_general_ci;

package com.yiidian.domain;

import java.io.Serializable;

/**

* @author

*

*/

public class Account implements Serializable{

private Integer id;

private String name;

private Float 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;

}

@Override

public String toString() {

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

+ "]";

}

}

/p>

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

".0.dtd">

四、编写业务层接口和实现类

AccountService:

package com.yiidian.service;

import com.yiidian.domain.Account;

/**

* @author

*

*/

public interface AccountService {

/**

* 根据id查询账户信息

* @param id

* @return

*/

Account findAccountById(Integer id);//查

/**

* 转账

* @param sourceName转出账户名称

* @param targeName转入账户名称

* @param money转账金额

*/

void transfer(String sourceName,String targeName,Float money);//增删改

}

AccountServiceImpl:

package com.yiidian.service.impl;

import org.springframework.transaction.annotation.Transactional;

import com.yiidian.dao.AccountDao;

import com.yiidian.domain.Account;

import com.yiidian.service.AccountService;

/**

* @author

*

*/

@Transactional

public class AccountServiceImpl implements AccountService {

private AccountDao accountDao;

public void setAccountDao(AccountDao accountDao) {

this.accountDao = accountDao;

}

@Override

public Account findAccountById(Integer id) {

return accountDao.findAccountById(id);

}

@Override

public void transfer(String sourceName, String targeName, Float money) {

// 1.根据名称查询两个账户

Account source = accountDao.findAccountByName(sourceName);

Account target = accountDao.findAccountByName(targeName);

// 2.修改两个账户的金额

source.setMoney(source.getMoney() - money);// 转出账户减钱

target.setMoney(target.getMoney() + money);// 转入账户加钱

// 3.更新两个账户

accountDao.updateAccount(source);

//模拟异常

int i = 1 / 0;

accountDao.updateAccount(target);

}

}

五、编写Dao接口和实现类

AccountDao:

package com.yiidian.dao;

import com.yiidian.domain.Account;

/**

*

* @author

*

*/

public interface AccountDao {

/**

* 根据id查询账户信息

* @param id

* @return

*/

Account findAccountById(Integer id);

/**

* 根据名称查询账户信息

* @return

*/

Account findAccountByName(String name);

/**

* 更新账户信息

* @param account

*/

void updateAccount(Account account);

}

AccountDaoImpl:

package com.yiidian.dao.impl;

import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.yiidian.dao.AccountDao;

import com.yiidian.domain.Account;

/**

*

* @author

*

*/

public class AccountDaoImpl extends HibernateDaoSupport implements AccountDao{

@Override

public Account findAccountById(Integer id) {

return this.getHibernateTemplate().get(Account.class, id);

}

@Override

public Account findAccountByName(String name) {

return (Account) this.getHibernateTemplate().find("from Account where name = ?", name).get(0);

}

@Override

public void updateAccount(Account account) {

this.getHibernateTemplate().update(account);

}

}

六、配置业务层和持久层,加上Spring事务管理

xmlns:xsi="" xmlns:aop=""

xmlns:tx=""

xsi:schemaLocation="

.xsd

.xsd

.xsd">

true

true

org.hibernate.dialect.MySQL5Dialect

com/yiidian/domain/Account.hbm.xml

class="org.springframework.orm.hibernate5.HibernateTransactionManager">

七、编写测试类

package com.yiidian.test;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yiidian.service.AccountService;

/**

* @author

*

*/

public class HibernateTemplateDemo {

@Test

public void test1() {

ApplicationContext ac = new ClassPathXmlApplicationContext(

"applicationContext.xml");

AccountService accountService = (AccountService)ac.getBean("accountService");

accountService.transfer("小苍", "小泽", 300f);

}

}

本文标签: 注解mysql事物管理