c语言sscanf函数的用法是什么
274
2022-12-06
Java Spring 声明式事务详解
目录项目结构:表结构:基于xml的声明式事务配置完全注解(零xml)方式配置事务参数no-rollback-forrollback-forread-onlytimeoutisolationpropagation总结
项目结构:
表结构:
基于xml的声明式事务配置
IAccountDao.java:
package tx.dao;
import java.math.BigDecimal;
public interface IAccountDao {
void add(String name, BigDecimal money);
void sub(String name, BigDecimal money);
}
AccountDaoImpl.java:
package tx.service.impl;
import tx.dao.IAccountDao;
import tx.service.IAccountService;
import java.math.BigDecimal;
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void tran(String from, String to, BigDecimal money) {
ahttp://ccountDao.sub(from, money);
accountDao.add(to, money);
}
}
IAccountService.java:
package tx.service;
import java.math.BigDecimal;
public interface IAccountService {
void tran(String from, String to, BigDecimal money);
}
AccountDaoImpl.java:
package tx.dao.impl;
import org.springframework.jdbc.core.JdbcTemplate;
import tx.dao.IAccountDao;
import java.math.BigDecimal;
public class AccountDaoImpl implements IAccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void add(String name, BigDecimal money) {
jdbcTemplate.update("update account set balance = balance + ? where name = ? ", money.toString(), name);
}
@Override
public void sub(String name, BigDecimal money) {
jdbcTemplate.update("update account set balance = balance - ? where name = ? ", money.toString(), name);
}
}
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop" iFcbDrAhBd xsi:schemaLocation=" http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd ">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:tx="http://springframework.org/schema/tx"
xmlns:aop="http://springframework.org/schema/aop"
iFcbDrAhBd xsi:schemaLocation="
http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd
http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd
">
ApplicationContext context = new ClassPathXmlApplicationContext("tx.xml");
IAccountService accountService = context.getBean("accountService", IAccountService.class);
accountService.tran("小明", "小红", new BigDecimal(500));
完全注解(零xml)方式配置
IAccountDao.java:
package tx.dao;
import java.math.BigDecimal;
public interface IAccountDao {
void add(String name, BigDecimal money);
void sub(String name, BigDecimal money);
}
AccountDaoImpl.java:
package tx.dao.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import tx.dao.IAccountDao;
import java.math.BigDecimal;
@Repository
public class AccountDaoImpl implements IAccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void add(String name, BigDecimal money) {
jdbcTemplate.update("update account set balance = balance + ? where name = ? ", money.toString(), name);
}
@Override
public void sub(String name, BigDecimal money) {
jdbcTemplate.update("update account set balance = balance - ? where name = ? ", money.toString(), name);
}
}
IAccountService.java:
package tx.service;
import java.math.BigDecimal;
public interface IAccountService {
void tran(String from, String to, BigDecimal money);
}
AccountServiceImpl.java:
package tx.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tx.dao.IAccountDao;
import tx.service.IAccountService;
import java.math.BigDecimal;
@Service
@Transactional
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
@Override
public void tran(String from, String to, BigDecimal money) {
accountDao.sub(from, money);
accountDao.add(to, money);
}
}
TXConfig.java
package tx.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tx.service.IAccountService;
import tx.service.impl.AccountServiceImpl;
import javax.sql.DataSource;
@Configuration
@ComponentScan(basePackages = "tx")
@EnableTransactionManagement
public class TXConfig {
/**
* 配置数据源
*/
@Bean
public DataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mysql://localhost:3306/test?useSSL=false");
dataSource.setUsername("root");
dataSource.setPassword("19834044876");
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
return dataSource;
}
/**
* 创建事务管理器
*/
@Bean
public DataSourceTransactionManager getTransactionManager() {
return new DataSourceTransactionManager(getDataSource());
}
/**
* 配置jdbcTemplate对象
*/
@Bean
public JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(getDataSource());
}
@Bean(name = "accountService")
public IAccountService getAccountService() {
return new AccountServiceImpl();
}
}
ApplicationContext context = new AnnotationConfigApplicationContext(TXConfig.class);
IAccountService accountService = context.getBean("accountService", IAccountService.class);
accountService.tran("小明", "小红", new BigDecimal(500));
事务参数
no-rollback-for
指定碰到哪些异常不需要回滚
rollback-for
指定碰到哪些异常需要回滚
read-only
设置事务为只读事务
timeout
以秒为单位,设置事务超出指定时常后自动回滚
默认为-1,即不管事务运行多久都不回滚
isolation
事务的隔离级别
默认为DEFAULT,即使用当前数据库的隔离级别
propagation
事务的传播行为
默认为REQUIRED
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注我们的更多内容!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~