標籤:
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分下載該範例程式碼】
如何處理Spring、Ibatis結合MySQL資料庫使用時的事務操作