1. Configuration:
<context:component-scan base-package= "com.vrvwh.wh01"/>
<BeanID= "DataSource"class= "Com.alibaba.druid.pool.DruidDataSource"Init-method= "Init"Destroy-method= "Close"> < Propertyname= "url"value= "${url}" /> < Propertyname= "username"value= "${username}" /> < Propertyname= "Password"value= "${password}" /> < Propertyname= "Maxactive"value= " the" /> < Propertyname= "InitialSize"value= "1" /> < Propertyname= "Maxwait"value= "60000" /> < Propertyname= "Minidle"value= "1" /> < Propertyname= "Timebetweenevictionrunsmillis"value= " the" /> < Propertyname= "Minevictableidletimemillis"value= "300000" /> < Propertyname= "Validationquery"value= "Select ' x ' from DUAL" /> < Propertyname= "Testwhileidle"value= "true" /> < Propertyname= "Testonborrow"value= "false" /> < Propertyname= "Testonreturn"value= "false" /> <!--MySQL does not support poolpreparedstatements - <!--<property name= "poolpreparedstatements" value= "true"/> - <!--<property name= "maxpoolpreparedstatementperconnectionsize" value= "/> " - <!--turn on the Druid Monitoring statistics function - < Propertyname= "Filters"value= "Stat" /> </Bean> <BeanID= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate"> < Propertyname= "DataSource"ref= "DataSource" /> </Bean> <BeanID= "Sessionfactory"class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean"> < Propertyname= "DataSource"ref= "DataSource" /> < Propertyname= "Packagestoscan"> <List> <value>Com.vrvwh.wh01.domain</value> </List> </ Property> < Propertyname= "Hibernateproperties"> <value>hibernate.dialect=${dialect} hibernate.show_sql=${hibernate.show_sql} hi Bernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto} Cache.provider_class=${hibernate.cache.provider_class} Cache.use_second_level_cache=${hibernate.cache.use_second_level_cache} Cache.use_query_cache=${hi Bernate.cache.use_query_cache} hibernate.jdbc.batch_size=${hibernate.jdbc.batch_size}</value> </ Property> </Bean> <!--Configure Hibernate transaction manager - <BeanID= "TransactionManager"class= "Org.springframework.orm.hibernate4.HibernateTransactionManager"> < Propertyname= "Sessionfactory"ref= "Sessionfactory" /> < Propertyname= "DataSource"ref= "DataSource" /> </Bean> <!--Configure Transaction Exception encapsulation - <BeanID= "Persistenceexceptiontranslationpostprocessor"class= "Org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> <Tx:annotation-drivenTransaction-manager= "TransactionManager"/>
1. function
Public Session getcurrentsession () { return sessionfactory.getcurrentsession (); Public Serializable Save (T instance) { return getcurrentsession (). Save (instance); }
Attention:
Sessionfactory.getcurrentsession () to run in a transaction
Spring declarative transaction configuration and use