您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Java Spring聲明式事務是什么,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
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) { accountDao.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); } }
<?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: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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!--配置數據源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="19834044876"/> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> </bean> <!--創建事務管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入數據源--> <property name="dataSource" ref="dataSource"/> </bean> <!--配置jdbcTemplate對象--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!--注入dataSource--> <property name="dataSource" ref="dataSource"/> </bean> <!--將JdbcTemplate注入到AccountDao中--> <bean id="accountDao" class="tx.dao.impl.AccountDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> <!--將AccountDao注入到AccountService中--> <bean id="accountService" class="tx.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> </bean> <!--配置事務通知--> <tx:advice id="txAdvice"> <!--配置事務參數--> <tx:attributes> <!--指定哪些方法上面添加事務--> <tx:method name="tran"/> <!-- name="*", name="tran*", name="*tran", ... --> </tx:attributes> </tx:advice> <!--配置切入點和切面--> <aop:config> <!--配置切入點--> <aop:pointcut id="pointCut" expression="execution(* tx.service.IAccountService.*(..))"/> <!--配置通知--> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/> </aop:config> </beans>
ApplicationContext context = new ClassPathXmlApplicationContext("tx.xml"); IAccountService accountService = context.getBean("accountService", IAccountService.class); accountService.tran("小明", "小紅", new BigDecimal(500));
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));
指定碰到哪些異常不需要回滾
指定碰到哪些異常需要回滾
設置事務為只讀事務
以秒為單位,設置事務超出指定時常后自動回滾
默認為-1,即不管事務運行多久都不回滾
事務的隔離級別
默認為DEFAULT,即使用當前數據庫的隔離級別
事務的傳播行為
默認為REQUIRED
上述就是小編為大家分享的Java Spring聲明式事務是什么了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。