In the Java ee 5 Environment, spring configuration can greatly simplify the transaction configuration, which requires spring 2.0, such
Import org. springframework. transaction. annotation. Transactional;
@ Transactional
Public interface bookdao {
@ Transactional (readonly = true)
Book query (string ID );
@ Transactional (readonly = true)
List <book> queryall ();
Void insert (Book );
Void Update (book );
Void Delete (book );
}
Note that only public classes can be annotated, but not private classes,
In addition, the configuration in the XML file should be as follows:
xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns: Tx = "http://www.springframework.org/schema/tx"
xsi: schemalocation = "
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/ SC Hema/Tx/spring-tx-2.0.xsd "
<Bean id = "datasource" class = "org. springframework. JDBC. datasource. drivermanagerdatasource">
<Property name = "driverclassname" value = "org. HSQLDB. jdbcdriver"/>
<Property name = "url" value = "JDBC: HSQLDB: Mem: Bookstore"/>
<Property name = "username" value = "sa"/>
<Property name = "password" value = ""/>
</Bean>
<! -- Define transactionmanager -->
<Bean id = "transactionmanager" class = "org. springframework. JDBC. datasource. cetcetransactionmanager">
<Property name = "datasource" ref = "datasource"/>
</Bean>
<! -- Define bookdao -->
<Bean id = "bookdao" class = "example. chapter6.bookdaoimpl">
<Property name = "datasource" ref = "datasource"/>
</Bean>
<! -- Automatically configure all beans with @ transactional annotation as declarative transaction support -->
<TX: annotation-driven transaction-Manager = "transactionmanager"/>
</Beans>