How to handle transaction operations when Ibatis is used in conjunction with MySQL database

Source: Internet
Author: User

Ibatis is the predecessor of MyBatis, which is an open source persistence layer framework. At its core, sqlmap--maps the entity bean to the relational database, separating the business code from the writing of the SQL statement. Ibatis is the "semi-automated" ORM persistence layer framework. This "semi-automation" is the "fully automated" ORM implementation that provides a comprehensive database encapsulation mechanism relative to hibernate, and the "fully automatic" ORM implements the mapping between the Pojo and the database table fields and enables the automatic generation and execution of SQL. The focus of Ibatis is the mapping between Pojo and SQL, which means that Ibatis does not automatically generate and execute SQL for programmers at run time, and specific SQL statements need to be written by programmers. The parameters required by the SQL statement and the returned result fields are then mapped to the specified Pojo through the mapping configuration file. This blog demonstrates how to handle transactional operations when Ibatis is used in conjunction with a MySQL database:

Engineering structures such as:


As this example introduces the more comprehensive, the file is more, here only the marked two files in the code, the full source can be downloaded by clicking the hyperlink at the bottom of this article:

Code in the Bankcarddao.java file:

Package Com.ghj.dao.imp;import Java.io.ioexception;import Java.io.reader;import java.sql.sqlexception;import Java.util.hashmap;import Java.util.map;import Com.ghj.dao.ibankcarddao;import Com.ibatis.common.resources.resources;import Com.ibatis.sqlmap.client.sqlmapclient;import  com.ibatis.sqlmap.client.sqlmapclientbuilder;/** * Bank card Management data Access Layer Interface implementation class * * @author Gao Yingjie */public class Bankcarddao implements Ibankcarddao {private sqlmapclient sqlmapclient;public Bankcarddao () {String resource = "Config/sqlmapconfig.xml"; try { Reader reader = Resources.getresourceasreader (Resource);//Read config file Sqlmapclient = Sqlmapclientbuilder.buildsqlmapclient (reader);} catch (IOException e) {e.printstacktrace ();}} /** * transfer * @param outaccount transfer account * @param inaccount transfer to account * @param amountofmoney amount * * @author Gao Yingjie * * @Overridepublic Boo Lean transferaccounts (String outaccount, String inaccount, long Amountofmoney) {try {sqlmapclient.starttransaction (); Long outaccountdeposit = Finddepositbyaccount (Outaccount);//Transfer Account deposit longInaccountdeposit = Finddepositbyaccount (Inaccount);//Deposit into account if (Updatedepositbyaccount (Outaccountdeposit- Amountofmoney, Outaccount)) {//Update transfer Account Deposit Updatedepositbyaccount (inaccountdeposit + Amountofmoney, inaccount);// Update Transfer Account Deposit//outaccount = null;//system.out.println (Outaccount.equals (Inaccount));//Intentional exception to test whether the transaction is rolled back} Sqlmapclient.committransaction (); return true;} catch (SQLException e) {System.err.println ("Transfer failed, transaction rollback"); E.printstacktrace ();} finally {try { Sqlmapclient.endtransaction ();} catch (SQLException e) {System.err.println ("Transfer failed, end transaction unexpected, transaction rollback"); E.printstacktrace ();}} return false;} /** * Deposit Enquiry * @param Deposit Deposit * * @author Gao Yingjie */private long Finddepositbyaccount (String account) throws Sqlexceptio N{return (Long) sqlmapclient.queryforobject ("Finddepositbyaccount", account);} /** * Update deposit based on account * @param deposit deposit * @param account * * @author Gao Yingjie */private boolean updatedepositbyaccount (Long Depo Sit, String account) throws Sqlexception{map<string, object> params = new Hashmap<strinG, object> ();p arams.put ("deposit", deposit);p arams.put ("account", account); return Sqlmapclient.update (" Updatedepositbyaccount ", params) > 0;}}

Code in the Bankcard.xml file:

<?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><!--Account Enquiry Deposit--><select id= "Finddepositbyaccount" parameterclass= "string" resultclass= "long ">select deposit from Lm_bank_card where account= #account # </select><!--Update deposit by account--><update id=" Updatedepositbyaccount "parameterclass=" Java.util.HashMap ">        update lm_bank_card set deposit= #deposit # where account= #account # </update></sqlMap>

" 0 min Download the sample code "

How to handle transaction operations when Ibatis is used in conjunction with MySQL database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.