1. Affairs
The essence of Spring transactions is actually the database's support for transactions. Without database transaction support, spring cannot provide transaction functions. In the end, it is all called database connection to complete the transaction opening, committing and rolling back.
2. Module
So for spring transactions, several indispensable modules are data sources, transaction managers and transaction programming.
3. xml configuration
<!--Transaction Manager--> <bean id="springTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!--Data Source--> <bean id="dataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <!-- Specify the sqlMapConfig total configuration file. The customized environment is not effective in the spring container--> <!-- Specify the entity class mapping file. You can specify the specified package and all configuration files under the subpackage at the same time. There is one mapper and configLocation. When you need to specify an alias for the entity class, you can specify the configLocation property, and then use mapper to introduce the entity class mapping file in the mybatis total configuration file--> <!-- <property name="configLocation" value="classpath:fwportal/beans/dbconfig/mybatis.xml" />--> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!--Register DAO interface as BEAN--> <bean> <property name="basePackage" value="TRANSACTION.DAO" /> </bean>
4. Transaction programming
@Test public void testDelete() throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext("mysqltransaction.xml");DataSourceTransactionManager springTransactionManager = (DataSourceTransactionManager) context.getBean("springTransactionManager");DefaultTransactionDefinition def = new DefaultTransactionDefinition();def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);//Open the transaction TransactionStatus status = springTransactionManager.getTransaction(def);final StudentDAO dao = (StudentDAO)context.getBean("studentDAO");try {dao.delete(2L);}catch (Exception ex) {springTransactionManager.rollback(status);//Transaction rollback throw ex;}springTransactionManager.commit(status);//Transaction commit}5. Summary
The above is a brief case of using mybatis and spring to complete transaction operations. The database transaction isolation level can be configured, and the database isolation level of mysql is in the connection dimension.
You can also set the timeout time of the transaction, that is, the timeout transaction will automatically roll back.
The above is all the content of this article about mybatis opening spring transaction code analysis, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!