Spring Integration MyBatis's things management configuration

Source: Internet
Author: User

First, the basic configuration
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:jdbc=" Http://www.springframework.org/schema/jdbc "xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/conte XT Http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/ JDBC Http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http ://www.springframework.org/schema/tx/spring-tx-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/http Www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!--connectionPool configuration-<bean id= "Hikariconfig" class= "Com.zaxxer.hikari.HikariConfig" > <property name= "poolname" va Lue= "SPRINGHIKARICP"/> <property name= "connectiontestquery" value= "Select 1"/> <property name=            "Datasourceclassname" value= "${jdbc.driverclassname}"/> <property name= "Datasourceproperties" > <props> <prop key= "url" >${jdbc.url}</prop> <prop key= "user" >${jdbc. username}</prop> <prop key= "password" >${jdbc.password}</prop> <prop key                = "Cacheprepstmts" >true</prop> <prop key= "Prepstmtcachesize" >250</prop> <prop key= "Prepstmtcachesqllimit" >2048</prop> </props> </property> <!- -Control autocommit Behavior Default:true-<property name= "autocommit" value= "true"/> <!--connection pool Gets the connection whether read-only default: False--> <pRoperty name= "ReadOnly" value= "false"/> <!--Maximum connection time-out default:30 seconds-to <property name= "Connectionti Meout "value=" 30000 "/> <!--maximum idle time out default:10 minutes--<property name=" idleTimeout "value=" 60000 0 "/> <!--the maximum lifetime of a connection in a connection pool default:30 minutes--<property name=" Maxlifetime "value=" 1800000 "/> & lt;/bean> <bean id= "DataSource" class= "Com.zaxxer.hikari.HikariDataSource" destroy-method= "Close" > < Constructor-arg ref= "Hikariconfig"/> </bean> <!--define the sqlsessionfactory and <bean id= "s Qlsessionfactory "class=" Org.mybatis.spring.SqlSessionFactoryBean "> <property name=" dataSource "ref=" Datasou Rce "/> <property name=" configlocation "value=" Classpath:meta-inf/mybatis/mybatis-config.xml "/> < Property Name= "Mapperlocations" value= "Classpath:meta-inf/mybatis/mapper/*.xml"/> </bean> <!--scan for Mappers and let them is aUtowired--<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "Annota Tionclass "value=" org.springframework.stereotype.Repository "/> <property name=" basepackage "value=" Com.geenk . market.dal "/> </bean> <!--declaration encoding transaction--<bean id=" TransactionManager "class=" Org.springframewo Rk.jdbc.datasource.DataSourceTransactionManager "> <property name=" DataSource "ref=" DataSource "/> </b        ean> <bean id= "transactiontemplate" class= "Org.springframework.transaction.support.TransactionTemplate" > <property name= "TransactionManager" ref= "TransactionManager"/> <!--Isolation_default indicates that the database used is determined by--&gt        ; <property name= "Isolationlevelname" value= "Isolation_default"/> <property name= "PropagationBehaviorName" V Alue= "propagation_required"/> </bean> <!--start support for declarative transaction management with annotations-<tx:annotation-driven transact Ion-manager= "TranSactionmanager "/></beans> 

This configuration scheme is the most basic thing configuration scheme, and MyBatis automatically participates in spring transaction management without additional configuration. Transaction management will not work as long as the data source referenced by Org.mybatis.spring.SqlSessionFactoryBean is consistent with the data source referenced by Datasourcetransactionmanager. But the disadvantage is that the spread behavior of things is consistent, can not be precisely for specific methods to configure the propagation of things.

Second, enhance the configuration
<tx:advice id= "Transactionadvice" transaction-manager= "TransactionManager" > <tx:attributes> <tx:method name= "add*" propagation= "REQUIRED"/> <tx:method name= "insert*" propagation= "REQUIRED"/&gt            ; <tx:method name= "save*" propagation= "REQUIRED"/> <tx:method name= "update*" propagation= "REQUIRED"/&G            T <tx:method name= "modify*" propagation= "REQUIRED"/> <tx:method name= "edit*" propagation= "REQUIRED"/&G            T <tx:method name= "delete*" propagation= "REQUIRED"/> <tx:method name= "remove*" propagation= "REQUIRED"/ > <tx:method name= "get*" propagation= "SUPPORTS"/> <tx:method name= "find*" p ropagation= "SUPPORTS"/> <tx:method name= "load*" propagation= "SUPPORTS"/> <tx:method na Me= "search*" propagation= "SUPPORTS"/> <tx:method name= "datagrid*" propagation= "SUPPORTS"/> <tx:method name=" * "propagation=" SUPPORTS "/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id= "transactionpointcut" expression= "Execution (* com.geenk.market.dal). *impl.* (..)) " /> <aop:advisor pointcut-ref= "transactionpointcut" advice-ref= "Transactionadvice"/> </aop:config&gt ;

The configuration scheme can be precise to the specific method, the operation of the database changes such as new, modified, and deleted. In the way of things, the operation of the database without modification can be done in a non-object way.

Third, annotation-based configuration
<!--configuration transaction Manager-<bean id= "Txmanager" class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager ">     <property name=" DataSource "ref=" DataSource "></property></bean><tx:annotation-driven transaction-manager=" TxManager "/>

Then just add the annotation @Transactional on the class or method you want to use.

Spring Integration MyBatis's things management configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.