Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a Java Spring declarative transaction

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about what is the declarative transaction of Java Spring. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Project structure:

Table structure:

Declarative transaction configuration based on 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) {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) }} ApplicationContext context = new ClassPathXmlApplicationContext ("tx.xml") IAccountService accountService = context.getBean ("accountService", IAccountService.class); accountService.tran ("Xiaoming", "Xiao Hong", new BigDecimal (500)); fully annotated (zero XML) configuration

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;@Repositorypublic 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@Transactionalpublic 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") @ EnableTransactionManagementpublic class TXConfig {/ * configuration data source * / @ 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;} / * create transaction manager * / @ Bean public DataSourceTransactionManager getTransactionManager () {return new DataSourceTransactionManager (getDataSource ());} / * configure jdbcTemplate object * / @ 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 ("Xiao Ming", "Xiao Hong", new BigDecimal (500)); transaction parameters

No-rollback-for

Specify which exceptions do not need to be rolled back

Rollback-for

Specify which exceptions need to be rolled back

Read-only

Set the transaction to read-only

Timeout

Sets the transaction to be rolled back automatically after the specified time is exceeded in seconds

The default is-1, that is, no matter how long the transaction runs, it will not be rolled back.

Isolation

Isolation level of the transaction

The default is DEFAULT, even with the isolation level of the current database

Propagation

The propagation behavior of transactions

Default is REQUIRED

This is what the Java Spring declarative transaction shared by the editor is. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report