如何处理Spring、Ibatis结合MySQL数据库使用时的事务操作
2015-03-04 08:34
701 查看
Ibatis是MyBatis的前身,它是一个开源的持久层框架。它的核心是SqlMap——将实体Bean跟关系数据库进行映射,将业务代码和SQL语句的书写进行分开。Ibatis是“半自动化”的ORM持久层框架。这里的“半自动化”是相对Hibernate等提供了全面的数据库封装机制的“全自动化”ORM实现而言的,“全自动”ORM实现了POJO与数据库表字段之间的映射并且实现了SQL的自动生成和执行。而Ibatis的着力点,则在于POJO与SQL之间的映射关系,即Ibatis并不会为程序员在运行期自动生成并执行SQL,具体的SQL语句需要程序员编写,然后通过映射配置文件将SQL语句所需的参数和返回的结果字段映射到指定POJO中。本篇博客演示了如何处理Spring、Ibatis结合MySQL数据库使用时的事务操作:
工程结构如下图:
由于该例子介绍的比较全面,文件比较多,这里只给出上图标出的三个文件中的代码,完整的源码可通过点击本文最下面的超链接下载:
BankCardDao.java文件中的代码:
package com.ghj.dao.imp;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import com.ghj.dao.IBankCardDao;
/**
* 银行卡管理数据访问层接口实现类
*
* @author 高焕杰
*/
public class BankCardDao extends SqlMapClientDaoSupport implements IBankCardDao {
/**
* 转账
* @param outAccount 转出账户
* @param inAccount 转入账号
* @param amountOfMoney 金额
*
* @author 高焕杰
*/
@Override
@Transactional//采用注释的方式实现事务操作
public boolean transferAccounts(String outAccount, String inAccount, long amountOfMoney){
try {
long outAccountDeposit = findDepositByAccount(outAccount);//转出账户的存款
long inAccountDeposit = findDepositByAccount(inAccount);//转入账户的存款
if(updateDepositByAccount(outAccountDeposit - amountOfMoney, outAccount)){//更新转出账号存款
updateDepositByAccount(inAccountDeposit + amountOfMoney, inAccount);//更新转入账号存款
// outAccount = null;
// System.out.println(outAccount.equals(inAccount));//故意出现异常以测试事务是否回滚
}
return true;
} catch (SQLException e) {
System.err.println("转账失败,事务回滚");
e.printStackTrace();
}
return false;
}
/**
* 依据账号查询存款
* @param deposit 存款
*
* @author 高焕杰
*/
private long findDepositByAccount(String account) throws SQLException{
return (Long)getSqlMapClientTemplate().queryForObject("findDepositByAccount", account);
}
/**
* 依据账号更新存款
* @param deposit 存款
* @param account 账号
*
* @author 高焕杰
*/
private boolean updateDepositByAccount(long deposit, String account) throws SQLException{
Map<String, Object> params = new HashMap<String, Object>();
params.put("deposit", deposit);
params.put("account", account);
return getSqlMapClientTemplate().update("updateDepositByAccount", params) > 0;
}
}
application-context.xml文件中的代码:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" default-autowire="byName" default-lazy-init="false">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="false" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property>
<property name="acquireIncrement" value="5"></property>
<property name="initialPoolSize" value="5"></property>
<property name="minPoolSize" value="5"></property>
<property name="maxPoolSize" value="20"></property>
<property name="maxStatements" value="100"></property>
<property name="numHelperThreads" value="10"></property>
<property name="maxIdleTime" value="60"></property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:config/sqlMapConfig.xml</value>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="bankCardDao" class="com.ghj.dao.imp.BankCardDao">
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>
<!-- Spring中配置事务操作:在Spring中实现事务操作有多种方式,其中以注解的方式最为常用,该种方式需要在需要配置事务操作的方法上添加@Transactional注释 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="dataSourceTransactionManager" />
</beans>
bankcard.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>
<!-- 依据账号查询存款 -->
<select id="findDepositByAccount" parameterClass="string" resultClass="long">
select deposit from lm_bank_card where account=#account#
</select>
<!-- 依据账号更新存款 -->
<update id="updateDepositByAccount" parameterClass="java.util.HashMap">
update lm_bank_card set deposit=#deposit# where account=#account#
</update>
</sqlMap> 【0分下载该示例代码】
工程结构如下图:
由于该例子介绍的比较全面,文件比较多,这里只给出上图标出的三个文件中的代码,完整的源码可通过点击本文最下面的超链接下载:
BankCardDao.java文件中的代码:
package com.ghj.dao.imp;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import com.ghj.dao.IBankCardDao;
/**
* 银行卡管理数据访问层接口实现类
*
* @author 高焕杰
*/
public class BankCardDao extends SqlMapClientDaoSupport implements IBankCardDao {
/**
* 转账
* @param outAccount 转出账户
* @param inAccount 转入账号
* @param amountOfMoney 金额
*
* @author 高焕杰
*/
@Override
@Transactional//采用注释的方式实现事务操作
public boolean transferAccounts(String outAccount, String inAccount, long amountOfMoney){
try {
long outAccountDeposit = findDepositByAccount(outAccount);//转出账户的存款
long inAccountDeposit = findDepositByAccount(inAccount);//转入账户的存款
if(updateDepositByAccount(outAccountDeposit - amountOfMoney, outAccount)){//更新转出账号存款
updateDepositByAccount(inAccountDeposit + amountOfMoney, inAccount);//更新转入账号存款
// outAccount = null;
// System.out.println(outAccount.equals(inAccount));//故意出现异常以测试事务是否回滚
}
return true;
} catch (SQLException e) {
System.err.println("转账失败,事务回滚");
e.printStackTrace();
}
return false;
}
/**
* 依据账号查询存款
* @param deposit 存款
*
* @author 高焕杰
*/
private long findDepositByAccount(String account) throws SQLException{
return (Long)getSqlMapClientTemplate().queryForObject("findDepositByAccount", account);
}
/**
* 依据账号更新存款
* @param deposit 存款
* @param account 账号
*
* @author 高焕杰
*/
private boolean updateDepositByAccount(long deposit, String account) throws SQLException{
Map<String, Object> params = new HashMap<String, Object>();
params.put("deposit", deposit);
params.put("account", account);
return getSqlMapClientTemplate().update("updateDepositByAccount", params) > 0;
}
}
application-context.xml文件中的代码:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" default-autowire="byName" default-lazy-init="false">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="false" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property>
<property name="acquireIncrement" value="5"></property>
<property name="initialPoolSize" value="5"></property>
<property name="minPoolSize" value="5"></property>
<property name="maxPoolSize" value="20"></property>
<property name="maxStatements" value="100"></property>
<property name="numHelperThreads" value="10"></property>
<property name="maxIdleTime" value="60"></property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:config/sqlMapConfig.xml</value>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="bankCardDao" class="com.ghj.dao.imp.BankCardDao">
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>
<!-- Spring中配置事务操作:在Spring中实现事务操作有多种方式,其中以注解的方式最为常用,该种方式需要在需要配置事务操作的方法上添加@Transactional注释 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="dataSourceTransactionManager" />
</beans>
bankcard.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>
<!-- 依据账号查询存款 -->
<select id="findDepositByAccount" parameterClass="string" resultClass="long">
select deposit from lm_bank_card where account=#account#
</select>
<!-- 依据账号更新存款 -->
<update id="updateDepositByAccount" parameterClass="java.util.HashMap">
update lm_bank_card set deposit=#deposit# where account=#account#
</update>
</sqlMap> 【0分下载该示例代码】
相关文章推荐
- 如何处理Ibatis结合MySQL数据库使用时的事务操作
- Spring学习心得(22)-- spring声明式事务处理(使用jdbc操作数据库)
- 使用spring的事务控制,这种情况如何处理呢
- Spring、Ibatis结合MySQL数据库的使用方法
- spring对循环操作数据库时的事务处理如何配置
- 如何使用Transact-SQL进行事务处理[示例]
- 使用Spring+Ibatis的项目,关于如何使用事务、如何提升性能的几点总结
- ASP.NET MVC3 中整合 NHibernate3.3、Spring.NET2.0 使用AOP执行事务处理
- 使用Spring+Ibatis的项目,关于如何使用事务、如何提升性能的几点总结
- 在ASP中使用事务处理数据库增/删/改操作
- android listview综合使用示例_结合数据库操作和listitem单击长按等事件处理
- android listview综合使用示例_结合数据库操作和listitem单击长按等事件处理
- Spring整合iBatis之三:从Spring角度看两框架整合(包括事务处理)
- spring开发_JDBC操作MySQL数据库_使用xml配置事务管理
- ASP中对数据库表的操作,可使用事务处理,并支持多事务处理
- ASP.NET MVC3 中整合 NHibernate3.3、Spring.NET2.0 使用AOP执行事务处理
- 操作分布式文件之八:如何批量并行读写远程文件和事务补偿处理
- C# 使用DataSet+dataAdapter操作后台数据库之事务处理方法1,欢迎拍砖
- MyBatis使用MySQL数据库如何在执行insert操作后返回自增的主键
- Spring 下事务管理 - 使用 AOP XML 配置管理(iBatis 为例)