This is the 21st day of my participation in the August Text Challenge.More challenges in August
Spring transaction environment setup instance
Example: Purchase trans_sale items
This example to realize the purchase of goods, simulate the user to place an order, add sales records to the order table, reduce inventory from the goods table.
Implementation steps:
Step0: Create a database table
Create two database tables sale, goods
Sale sales table
Goods goods table
Goods table data
Step1: maven relies on pom.xml
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> < artifactId > spring - the context < / artifactId > < version > 4.3.16. RELEASE < / version > < / dependency > < the dependency > < the groupId > org. Springframework < / groupId > < artifactId > spring - tx < / artifactId > < version > 4.3.16. RELEASE < / version > </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> < version > 4.3.16. RELEASE < / version > < / dependency > < the dependency > < groupId > org. Mybatis < / groupId > <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> < artifactId > mysql connector - Java < / artifactId > < version > 5.1.9 < / version > < / dependency > < the dependency > <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory><! <includes><! Properties <include>**/. Properties </include> <include>**/. XML </include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>Copy the code
Step2: create the entity class
Create the entity classes Sale and Goods
Step3: define the dao interface
Define interface SaleDao and GoodsDao for two DAOs
Step4: define the SQL mapping file corresponding to the dao interface
SaleDao.xml
GoodsDao.xml
Step5: define exception classes
Define the NotEnoughException class that the Service layer may throw
Step6: define the Service interface
Define the Service interface BuyGoodsService
Step7: define the implementation class of service
The implementation class BuyGoodsServiceImpl defines the service layer interface
When a class definition
When Dao properties
Low Buy method
Step8: modify the Spring configuration file
- Declare the Mybatis object
- Declare business layer objects
Step9: define test classes
Define the test class MyTest. You are now ready to run without transaction agents.
Use Spring’s transaction annotations to manage transactions
The @Transactional annotation enables transaction management by weaving transactions into corresponding public methods.
All the optional attributes of @Transactional look like this:
● Propagation: Used to set transaction propagation properties. This attribute is of type Propagation enumeration and the default value is Propagation.REQUIRED.
● Isolation: Used to set the isolation level of a transaction. This property is of type Isolation enumeration and the DEFAULT value is Isolation.default.
● readOnly: Used to set whether the operation of this method on the database is read-only. This property is Boolean and the default value is false.
● timeout: used to set the timeout period between the operation and the database. The unit is second and the type is int. The default value is -1, that is, there is no time limit.
● rollbackFor: Specifies the exception class to be rolled back. The type is Class[] and the default value is an empty array. Of course, if you only have one exception class, you don’t have to use arrays.
● rollbackForClassName: Specifies the name of the exception class to be rolled back. The type is String[] and the default value is an empty array. Of course, if you only have one exception class, you don’t have to use arrays.
● noRollbackFor: Specifies the exception class that does not need to be rolled back. The type is Class[] and the default value is an empty array. Of course, if you only have one exception class, you don’t have to use arrays.
● noRollbackForClassName: Specifies the name of the exception class that does not need to be rolled back. The type is String[] and the default value is an empty array. Of course, if you only have one exception class, you don’t have to use arrays.
Note that @Transactional, when used on methods, can only be used on public methods. For other non-public methods, if annotated @Transactional, Spring does not report an error but does not weave the specified transaction into the method. Because Spring ignores @Transaction annotations on all non-public methods.
If the @Transaction annotation is on a class, it means that all methods on that class will be woven into a Transaction at execution time.
Transaction steps to implement annotations:
Copy the trans_sale project, new project trans_sale_annotation
Declare the transaction manager
2. Turn on the annotation driver
Transaction-manager: ID of the transaction manager bean
Add transaction attributes to business layer public methods