In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to configure spring declarative transaction control based on XML". In daily operation, I believe many people have doubts about how to configure spring declarative transaction control based on XML. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to configure spring declarative transaction control based on XML". Next, please follow the editor to study!
/ * * the business layer implementation class of the account * * transaction control should be in the business layer * / public class AccountServiceImpl implements IAccountService {private IAccountDao accountDao; public void setAccountDao (IAccountDao accountDao) {this.accountDao = accountDao;} @ Override public Account findAccountById (Integer accountId) {return accountDao.findAccountById (accountId) } @ Override public void transfer (String sourceName, String targetName, Float money) {System.out.println ("transfer...."); / / 2.1 query transfer account Account source = accountDao.findAccountByName (sourceName) by name; / / 2.2 query transfer account Account target = accountDao.findAccountByName (targetName) by name / / 2.3Deduct money from the transfer account source.setMoney (source.getMoney ()-money); / 2.4transfer to the account plus money target.setMoney (target.getMoney () + money); / / 2.5UPDAT account accountDao.updateAccount (source); int iaccounts 1University0 / / 2.6 Update transferred to account accountDao.updateAccount (target);}}
Annotation-based declarative transaction Control in spring
-/ * * account business layer implementation class * * transaction control should be configured in the business layer * / @ Service ("accountService") @ Transactional (propagation= Propagation.SUPPORTS,readOnly=true) / / read-only transactions public class AccountServiceImpl implements IAccountService {@ Autowired private IAccountDao accountDao; @ Override public Account findAccountById (Integer accountId) {return accountDao.findAccountById (accountId) } / / what is required is a read-write transaction configuration @ Transactional (propagation= Propagation.REQUIRED,readOnly=false) @ Override public void transfer (String sourceName, String targetName, Float money) {System.out.println ("transfer...."); / / 2.1 query the transferred account Account source = accountDao.findAccountByName (sourceName) by name / / 2.2 transfer account Account target = accountDao.findAccountByName (targetName) according to name query; / / 2.3 transfer account minus money source.setMoney (source.getMoney ()-money); / / 2.4 transfer account plus money target.setMoney (target.getMoney () + money) / / 2.5UPDAT transfer account accountDao.updateAccount (source); int iTunes 1TUBO; / 2.6UPDAT account accountDao.updateAccount (target);}}
Declarative transaction Control based on Pure annotations in spring
Jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/eesyjdbc.username=rootjdbc.password=1234/** * configuration classes related to connecting to the database * / public class JdbcConfig {@ Value ("${jdbc.driver}") private String driver; @ Value ("${jdbc.url}") private String url; @ Value ("${jdbc.username}") private String username; @ Value ("${jdbc.password}") private String password / * create JdbcTemplate * @ param dataSource * @ return * / @ Bean (name= "jdbcTemplate") public JdbcTemplate createJdbcTemplate (DataSource dataSource) {return new JdbcTemplate (dataSource);} / * create a data source object * @ return * / @ Bean (name= "dataSource") public DataSource createDataSource () {DriverManagerDataSource ds = new DriverManagerDataSource () Ds.setDriverClassName (driver); ds.setUrl (url); ds.setUsername (username); ds.setPassword (password); return ds Configuration class of}} / * spring Equivalent to bean.xml * / @ Configuration@ComponentScan ("com.itheima") @ Import ({JdbcConfig.class) TransactionConfig.class}) @ PropertySource ("jdbcConfig.properties") @ EnableTransactionManagement// enables transaction management public class SpringConfiguration {} / * transaction-related configuration class * / public class TransactionConfig {/ * used to create transaction manager object * @ param dataSource * @ return * / @ Bean (name= "transactionManager") public PlatformTransactionManager createTransactionManager (DataSource dataSource) {return new DataSourceTransactionManager (dataSource) The business layer implementation class * * transaction control of the account should all be configured in the business layer * / @ Service ("accountService") @ Transactional (propagation= Propagation.SUPPORTS,readOnly=true) / / read-only transactions public class AccountServiceImpl implements IAccountService {@ Autowired private IAccountDao accountDao; @ Override public Account findAccountById (Integer accountId) {return accountDao.findAccountById (accountId) } / / what is required is a read-write transaction configuration @ Transactional (propagation= Propagation.REQUIRED,readOnly=false) @ Override public void transfer (String sourceName, String targetName, Float money) {System.out.println ("transfer...."); / / 2.1 query the transferred account Account source = accountDao.findAccountByName (sourceName) by name / / 2.2 transfer account Account target = accountDao.findAccountByName (targetName) according to name query; / / 2.3 transfer account minus money source.setMoney (source.getMoney ()-money); / / 2.4 transfer account plus money target.setMoney (target.getMoney () + money) / / 2.5UPDAT account accountDao.updateAccount (source); / / int iSuppli 1Uniplet0; / 2.6UPDAT account accountDao.updateAccount (target);}} at this point, the study on "how to configure spring XML-based declarative transaction control" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.