One of the main reasons for using mybatis-spring is:
Mybatis-spring allows MyBatis to participate in spring transactions.
Mybatis-spring Leverage[use (something) to maximum advantage)] The existence of Datasourcetransactionmanager,
Without intentionally creating a new transaction management for MyBatis.
Once spring Tansaction Manager is configured, you can configure transactions as you normally would in spring.
Two configuration modes are supported:
[Email protected] Comments
2.AOP style Configuration
During the transaction, a separate Sqlsession object is created and used.
When the transaction is complete, the session is either committed or rolled back.
Standard configuration
Creating the data source Transaction manager Datasourcetransactionmanager in the spring XML configuration file will allow spring to do transaction management.
<id= "TransactionManager" class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager "> < name = "DataSource" ref = "DataSource" /> </ Bean >
The data source is configured by any JDBC datasource. (see Sqlsessionfactorybean's article).
This includes the connection pool and the data source (obtained by the Jndi query).
Note: The datasource used by the transaction manager must be consistent with the datasource of Sqlsessionfactorybean.
The following sections are not very useful to me at present, just by the way.
Container Managed Transactions
If you are using a JEE container and want spring to participate in container managed transactions (CMT),
Then spring should configure Jtatransactionmanager or one of its container-specific subclasses.
The easiest way to do this is to use the Spring transaction namespace:
</>
In this configuration, MyBatis will behave like other spring transaction sources configured by CMT.
Spring automatically uses any existing container transactions and formulates a sqlsession to it.
If no transaction is started, but one is required based on the transaction configuration, spring launches a new single CMT.
Note that if you use CMT but do not want to use spring transaction management, you cannot configure the Spring transaction manager,
And the Sqlsessionfactorybean must be configured using the MyBatis base managedtransactionfactory.
<BeanID= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean"> < Propertyname= "DataSource"ref= "DataSource" /> < Propertyname= "Transactionfactory"> <Beanclass= "Org.apache.ibatis.transaction.managed.ManagedTransactionFactory" /> </ Property> </Bean>
With ↑ The part is of little use to me at present, just by the way translated.
Finally there is a piece: programmatic Transaction Management have time to engage.
[Mybatis-spring] Transaction transaction/Transaction/transaction Manager