ibatis集成spring 声明式事务管理
找到的教程都是将事务配置在DAO的,可是我现在需要配置事务到业务层,也就是接口实现类的那一层,这个层会调用许多的dao,如何配置呢?我修改过一点,但是事务失败没有回滚。希...
找到的教程都是将事务配置在DAO的,可是我现在需要配置事务到业务层,也就是接口实现类的那一层,这个层会调用许多的dao,如何配置呢?我修改过一点,但是事务失败没有回滚。希望谁能给个例子看看……
呃,能不能具体点? 展开
呃,能不能具体点? 展开
展开全部
我提供一个实例:
数据库是MYSQL , 数据源是C3P0 ,
-----------
1) 建表语句 :
----------------
DROP TABLE IF EXISTS book ;
CREATE TABLE book(
id int(11) NOT NULL auto_increment COMMENT 'primary key',
name varchar(60) default NULL COMMENT 'book name',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='my book table';
----------------
注意了,在MYSql 中 表的引擎一定要用InnoDB ,如果用MyISAM 的话,是不支持事务的。
2) JAVA Bean
Book.java
-------------------
package com.spring.demo.bean;
import java.io.Serializable;
public class Book implements Serializable {
public Book(){}
public Book(String name){
this.name = name;
}
/**
* ID
*/
private Integer id ;
/**
* 书名
*/
private String name ;
// getter and setter
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
-------------------
3)Book.xml 配置
---------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Book" >
<typeAlias alias="book" type="com.spring.demo.bean.Book"/>
<resultMap id="result_map_book" class="book" >
<result property="id" column="id" javaType="integer"/>
<result property="name" column="name" javaType="string"/>
</resultMap>
<insert id="insertBook" parameterClass="book">
insert book(name) values(#name#)
</insert>
<update id="updateBook" parameterClass="book">
update book set name=#name# where id=#id#
</update>
<delete id="deleteBook" parameterClass="int">
delete from book where id=#value#
</delete>
<select id="selectBookById" parameterClass="int" resultMap="result_map_book">
select id , name from book where id=#value#
</select>
<select id="selectBookByName" parameterClass="string" resultMap="result_map_book">
select id , name from book where name like #value#
</select>
</sqlMap>
---------------------
4)spring 的 applicationContext.xml
---------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- 引入参数配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass"><value>${jdbc.driverClass}</value></property>
<property name="jdbcUrl"><value>${jdbc.url}</value></property>
<property name="user"><value>${jdbc.user}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
<property name="initialPoolSize"><value>${jdbc.initialPoolSize}</value></property>
<property name="minPoolSize"><value>${jdbc.minPoolSize}</value></property>
<property name="maxPoolSize"><value>${jdbc.maxPoolSize}</value></property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<!-- 事务代理拦截器的配置 -->
<bean id="baseTransactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- ibatis sqlMapClient 配置 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:sqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<!-- dao配置 -->
<bean id="ibatis_BookDao" class="com.spring.demo.dao.Ibatis_BookDao">
<property name="sqlMapClient" >
<ref bean="sqlMapClient"/>
</property>
</bean>
<!-- 添加了事务的管理类 -->
<bean id="ibatis_BookManager" parent="baseTransactionProxy">
<property name="target">
<bean class="com.spring.demo.manager.Ibatis_BookManager">
<property name="dao">
<ref bean="ibatis_BookDao"/>
</property>
</bean>
</property>
</bean>
</beans>
---------------
5) 数据源参数配置文件:jdbc.properties
【说明 :按照你的具体情况来配置】:
------------
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springdemo?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=root
jdbc.initialPoolSize=1
jdbc.minPoolSize=1
jdbc.maxPoolSize=10
------------
6) IBATIS 的初始化配置 sqlMapConfig.xml
------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true" />
<sqlMap resource="com/spring/demo/bean/Book.xml"/>
</sqlMapConfig>
------------
配置完毕,下面是DAO 和 测试方法:
Ibatis_BookDao.java
----------------
package com.spring.demo.dao;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.spring.demo.bean.Book;
public class Ibatis_BookDao extends SqlMapClientDaoSupport {
/**
* 添加一本书
* @param book
*/
public void insertBook(Book book){
this.getSqlMapClientTemplate().insert("Book.insertBook", book);
}
/**
* 修改一本书
* @param book
*/
public void updateBook(Book book){
this.getSqlMapClientTemplate().update("Book.updateBook", book);
}
/**
* 根据ID来删除一本书
* @param id
*/
public void deleteBook(Integer id){
this.getSqlMapClientTemplate().delete("Book.deleteBook", id);
}
/**
* 根据ID来查询书对象
* @param id
* @return
*/
public Book selectBookById(Integer id){
return (Book)this.getSqlMapClientTemplate().queryForObject("Book.selectBookById", id);
}
/**
* 根据书名来查询相关书记录列表
* @param name
* @return
*/
public List selectBookByName(String name){
return this.getSqlMapClientTemplate().queryForList("Book.selectBookByName", "%"+name+"%" );
}
}
----------------
Ibatis_BookManager.java
-------------------
package com.spring.demo.manager;
import java.util.List;
import com.spring.demo.bean.Book;
import com.spring.demo.dao.Ibatis_BookDao;
public class Ibatis_BookManager {
private Ibatis_BookDao dao = null ;
/**
* 批量添加书本,如果列表长度超过3,就抛出异常
* @param books
*/
public void insertBooks(List books){
for(int i=0 ; i<books.size() ;i++){
if(i>2){
System.out.println("列表太长,中断事务");
throw new RuntimeException("中断事务异常,当列表长度大于3的时候故意抛出,看看事务是否回滚");
}
Book book = (Book)books.get(i);
dao.insertBook(book);
}
}
/**
* 添加一本书
* @param book
*/
public void insertBook(Book book){
this.dao.insertBook(book);
}
/**
* 修改一本书
* @param book
*/
public void updateBook(Book book){
this.dao.updateBook(book);
}
/**
* 根据ID来删除一本书
* @param id
*/
public void deleteBook(Integer id){
this.dao.deleteBook(id);
}
/**
* 根据ID来查询书对象
* @param id
* @return
*/
public Book selectBookById(Integer id){
return this.dao.selectBookById(id);
}
/**
* 根据书名来查询相关书记录列表
* @param name
* @return
*/
public List selectBookByName(String name){
return this.dao.selectBookByName(name);
}
// getter and setter //
public Ibatis_BookDao getDao() {
return dao;
}
public void setDao(Ibatis_BookDao dao) {
this.dao = dao;
}
-------------------
测试类:
IbatisDemo.java
------------------
package com.spring.demo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.demo.bean.Book;
import com.spring.demo.manager.Ibatis_BookManager;
import com.spring.demo.manager.Jdbc_BookManager;
public class IbatisDemo {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml") ;
Ibatis_BookManager manager = (Ibatis_BookManager)context.getBean("ibatis_BookManager");
List books = new ArrayList();
books.add(new Book("book_10"));
books.add(new Book("book_11"));
books.add(new Book("book_12"));
books.add(new Book("book_13"));
manager.insertBooks(books);
System.out.println("OK");
}
}
------------------
说明:MYSQL 的事务完整性我是测试过的。
之前有个朋友吧数据库换成 SQL SERVER 的时候事务完整性就出现问题。
这个问题有待研究。
数据库是MYSQL , 数据源是C3P0 ,
-----------
1) 建表语句 :
----------------
DROP TABLE IF EXISTS book ;
CREATE TABLE book(
id int(11) NOT NULL auto_increment COMMENT 'primary key',
name varchar(60) default NULL COMMENT 'book name',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='my book table';
----------------
注意了,在MYSql 中 表的引擎一定要用InnoDB ,如果用MyISAM 的话,是不支持事务的。
2) JAVA Bean
Book.java
-------------------
package com.spring.demo.bean;
import java.io.Serializable;
public class Book implements Serializable {
public Book(){}
public Book(String name){
this.name = name;
}
/**
* ID
*/
private Integer id ;
/**
* 书名
*/
private String name ;
// getter and setter
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
-------------------
3)Book.xml 配置
---------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Book" >
<typeAlias alias="book" type="com.spring.demo.bean.Book"/>
<resultMap id="result_map_book" class="book" >
<result property="id" column="id" javaType="integer"/>
<result property="name" column="name" javaType="string"/>
</resultMap>
<insert id="insertBook" parameterClass="book">
insert book(name) values(#name#)
</insert>
<update id="updateBook" parameterClass="book">
update book set name=#name# where id=#id#
</update>
<delete id="deleteBook" parameterClass="int">
delete from book where id=#value#
</delete>
<select id="selectBookById" parameterClass="int" resultMap="result_map_book">
select id , name from book where id=#value#
</select>
<select id="selectBookByName" parameterClass="string" resultMap="result_map_book">
select id , name from book where name like #value#
</select>
</sqlMap>
---------------------
4)spring 的 applicationContext.xml
---------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- 引入参数配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass"><value>${jdbc.driverClass}</value></property>
<property name="jdbcUrl"><value>${jdbc.url}</value></property>
<property name="user"><value>${jdbc.user}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
<property name="initialPoolSize"><value>${jdbc.initialPoolSize}</value></property>
<property name="minPoolSize"><value>${jdbc.minPoolSize}</value></property>
<property name="maxPoolSize"><value>${jdbc.maxPoolSize}</value></property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<!-- 事务代理拦截器的配置 -->
<bean id="baseTransactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- ibatis sqlMapClient 配置 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:sqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<!-- dao配置 -->
<bean id="ibatis_BookDao" class="com.spring.demo.dao.Ibatis_BookDao">
<property name="sqlMapClient" >
<ref bean="sqlMapClient"/>
</property>
</bean>
<!-- 添加了事务的管理类 -->
<bean id="ibatis_BookManager" parent="baseTransactionProxy">
<property name="target">
<bean class="com.spring.demo.manager.Ibatis_BookManager">
<property name="dao">
<ref bean="ibatis_BookDao"/>
</property>
</bean>
</property>
</bean>
</beans>
---------------
5) 数据源参数配置文件:jdbc.properties
【说明 :按照你的具体情况来配置】:
------------
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springdemo?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=root
jdbc.initialPoolSize=1
jdbc.minPoolSize=1
jdbc.maxPoolSize=10
------------
6) IBATIS 的初始化配置 sqlMapConfig.xml
------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true" />
<sqlMap resource="com/spring/demo/bean/Book.xml"/>
</sqlMapConfig>
------------
配置完毕,下面是DAO 和 测试方法:
Ibatis_BookDao.java
----------------
package com.spring.demo.dao;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.spring.demo.bean.Book;
public class Ibatis_BookDao extends SqlMapClientDaoSupport {
/**
* 添加一本书
* @param book
*/
public void insertBook(Book book){
this.getSqlMapClientTemplate().insert("Book.insertBook", book);
}
/**
* 修改一本书
* @param book
*/
public void updateBook(Book book){
this.getSqlMapClientTemplate().update("Book.updateBook", book);
}
/**
* 根据ID来删除一本书
* @param id
*/
public void deleteBook(Integer id){
this.getSqlMapClientTemplate().delete("Book.deleteBook", id);
}
/**
* 根据ID来查询书对象
* @param id
* @return
*/
public Book selectBookById(Integer id){
return (Book)this.getSqlMapClientTemplate().queryForObject("Book.selectBookById", id);
}
/**
* 根据书名来查询相关书记录列表
* @param name
* @return
*/
public List selectBookByName(String name){
return this.getSqlMapClientTemplate().queryForList("Book.selectBookByName", "%"+name+"%" );
}
}
----------------
Ibatis_BookManager.java
-------------------
package com.spring.demo.manager;
import java.util.List;
import com.spring.demo.bean.Book;
import com.spring.demo.dao.Ibatis_BookDao;
public class Ibatis_BookManager {
private Ibatis_BookDao dao = null ;
/**
* 批量添加书本,如果列表长度超过3,就抛出异常
* @param books
*/
public void insertBooks(List books){
for(int i=0 ; i<books.size() ;i++){
if(i>2){
System.out.println("列表太长,中断事务");
throw new RuntimeException("中断事务异常,当列表长度大于3的时候故意抛出,看看事务是否回滚");
}
Book book = (Book)books.get(i);
dao.insertBook(book);
}
}
/**
* 添加一本书
* @param book
*/
public void insertBook(Book book){
this.dao.insertBook(book);
}
/**
* 修改一本书
* @param book
*/
public void updateBook(Book book){
this.dao.updateBook(book);
}
/**
* 根据ID来删除一本书
* @param id
*/
public void deleteBook(Integer id){
this.dao.deleteBook(id);
}
/**
* 根据ID来查询书对象
* @param id
* @return
*/
public Book selectBookById(Integer id){
return this.dao.selectBookById(id);
}
/**
* 根据书名来查询相关书记录列表
* @param name
* @return
*/
public List selectBookByName(String name){
return this.dao.selectBookByName(name);
}
// getter and setter //
public Ibatis_BookDao getDao() {
return dao;
}
public void setDao(Ibatis_BookDao dao) {
this.dao = dao;
}
-------------------
测试类:
IbatisDemo.java
------------------
package com.spring.demo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.demo.bean.Book;
import com.spring.demo.manager.Ibatis_BookManager;
import com.spring.demo.manager.Jdbc_BookManager;
public class IbatisDemo {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml") ;
Ibatis_BookManager manager = (Ibatis_BookManager)context.getBean("ibatis_BookManager");
List books = new ArrayList();
books.add(new Book("book_10"));
books.add(new Book("book_11"));
books.add(new Book("book_12"));
books.add(new Book("book_13"));
manager.insertBooks(books);
System.out.println("OK");
}
}
------------------
说明:MYSQL 的事务完整性我是测试过的。
之前有个朋友吧数据库换成 SQL SERVER 的时候事务完整性就出现问题。
这个问题有待研究。
展开全部
在Spring中配置
<!-- Transaction-创建事务管理对象 -->
<bean id="bizTransManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- Aop-声明式事务的配置 -->
<tx:advice id="txAdvice" transaction-manager="bizTransManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS"
read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="bidMethods"
expression="execution(* cn.apatch.ordersys.biz.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="bidMethods" />
</aop:config>
<!-- Transaction-创建事务管理对象 -->
<bean id="bizTransManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- Aop-声明式事务的配置 -->
<tx:advice id="txAdvice" transaction-manager="bizTransManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS"
read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="bidMethods"
expression="execution(* cn.apatch.ordersys.biz.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="bidMethods" />
</aop:config>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
需要找本书来看!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询