In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how springboot starts declarative transactions, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.
It's easy for springboot to start a transaction, as long as an annotation @ Transactional is needed. Because transaction is enabled by default for jpa, jdbc, and mybatis in springboot, when their dependencies are introduced, things are turned on by default. Of course, if you need to use another orm, such as beatlsql, you need to configure the related transaction manager yourself.
Preparation stage
The code of the previous article is an example, that is, springboot integrates mybatis. The previous article is based on annotations to implement mybatis's data access layer. This article is based on xml to implement and open declarative transactions.
Environmental dependence
Introduce mybatis startup dependencies in the pom file:
Org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.0
Introduce mysql dependency
Mysql mysql-connector-java runtime com.alibaba druid 1.0.29 initialization database script-create table `roomt` # DROP TABLE `roomt` IF EXISTSCREATE TABLE `Secrett` (`id` int (11) NOT NULL AUTO_INCREMENT, `name` varchar (20) NOT NULL, `money`double DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 INSERT INTO `roomt` VALUES ('1th,' aaa', '1000'); INSERT INTO `roomt` VALUES (' 2years, 'bbb',' 1000'); INSERT INTO `roomt` VALUES ('3years,' ccc', '1000'); configure data source spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Drivermybatis.mapper-locations=classpath*:mybatis/*Mapper.xmlmybatis.type-aliases-package=com.forezp.entity
Configure mybatis.mapper-locations to indicate the location of mapper's xml file, which I put under the resources/mybatis file. Mybatis.type-aliases-package to indicate the package of the entity mapped to the database.
After the above steps, springboot can access the database through mybatis.
Create entity class public class Account {private int id; private String name; private double money; getter.. Setter.. } data access dao layer
Interface:
Public interface AccountMapper2 {int update (@ Param ("money") double money, @ Param ("id") int id);}
Mapper:
UPDATE account set money=# {money} WHERE id=# {id} service layer @ Servicepublic class AccountService2 {@ Autowired AccountMapper2 accountMapper2; @ Transactional public void transfer () throws RuntimeException {accountMapper2.update (90Lab 1); / / user 1 minus 10 pieces of user 2 plus 10 pieces of int iTunes 1max 0; accountMapper2.update (110Let2);}}
@ Transactional, declare the transaction, and design a transfer method, user 1 minus 10, user 2 plus 10. After user 1 minus 10, an exception is thrown, that is, user 2 plus 10 yuan cannot be executed, and when the annotation @ Transactional is added, neither person's money increases or decreases. When @ Transactional is not added, user 1 decreases by 10, and user 2 does not increase, that is, there is no data to manipulate user 2. You can see that the @ Transactional annotation opens things up.
Springboot is easy to open things, only need to add a line of comments, as long as you are using jdbctemplate, jpa, mybatis, this common orm.
So much for sharing about how springboot starts declarative transactions. I hope the above can help you and learn more. If you think the article is good, you can share it for more people to see.
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.