mybatis中运用事物转账功能实现案例

一,转账逻辑分析
1,需要转账人的用户名密码
2,收款人的账户名
3,转账金额
首先将jsp表单的中的上述信息,通过Account类进行封装,一个对象为转账人包括:用户名,密码,和金额
另一个对象为收款人设置:用户名
同时调用service层中的方法,获取转账信息反馈。
service层通过逻辑判断进行处理。
二,代码实现

对账户信息进行封装的类:
	
public class Account {
	private int id;//用户id
	private String account;//用户账号
	private String password;//用户密码
	private int balance;//用户金额
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getBalance() {
		return balance;
	}
	public void setBalance(int balance) {
		this.balance = balance;
	}

}

service接口层:

import java.io.IOException;
public interface AccountService{
	int OUT_ACCOUNT_ERROR=1;//转账人的用户或密码不正确
	int IN_ACCOUNT_ERROR=2;//收款人的用户不正确
	int BALANCE_NOTENOUGH_ERROR=3;//转账人的余额不足
	int SUCCESS=4;//转账成功
	public int tranfer(Account accountOut,Account accountIn) throws IOException ;
}


service层具体实现类:

	import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


public class AccountServiceImp implements AccountService{
	@Override
	public int tranfer(Account accountOut, Account accountIn) throws IOException {
		InputStream is=Resources.getResourceAsStream("mybatis.xml");
		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
		SqlSession session = factory.openSession();
		//判断一下收付款人账号是否正确
		Account ao=session.selectOne("a.b.selectOut",accountOut);
		Account ai=session.selectOne("a.b.selectIn",accountIn);
		//所转金额,对下面的收款人balance进行设置
		int i=accountOut.getBalance();
		//判断是否存在付款人
		if(ao!=null)
		{	
			//判断账户余额
			if(ao.getBalance()<i)
				return BALANCE_NOTENOUGH_ERROR;
			accountOut.setBalance(-i);
			session.update("a.b.transfer", accountOut);
			//收款人是否存在
			if(ai!=null) {
				accountIn.setBalance(i);
				session.update("a.b.transfer", accountIn);
				session.commit();
				session.close();
				return SUCCESS;
			}else {
				//事物回滚,由于在这时候付款人金额已经减少,因此在这回滚
				session.rollback();
				return IN_ACCOUNT_ERROR;
			}
		}else {
			return OUT_ACCOUNT_ERROR;
		}	
	}

}


servlet层:
	import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TransferServlet
 */
@WebServlet("/TransferServlet")
public class TransferServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   @Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
	Account accountOut=new Account();
	Account accountIn=new Account();
	//对收付款人的信息进行设置
	accountOut.setAccount(req.getParameter("accountOut"));
	accountOut.setPassword(req.getParameter("accountPassword"));
	int balance=Integer.parseInt(req.getParameter("balance"));
	accountOut.setBalance(balance);
	accountIn.setAccount(req.getParameter("accountIn"));
	AccountService as=new AccountServiceImp();
	int i=as.tranfer(accountOut, accountIn);
	//打印转账反馈信息
	System.out.println(i);
}

}



简单的jsp测试:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
 <form action="TransferServlet" method="post">
	转账人账户:<input id="user" type="text"  name="accountOut" ></br>
	转账人密码:<input id="user"  type="text"  name="accountPassword" ></br>
	转账金额:<input id="user"  type="text" name="balance" ></br>
	收款人账户:<input id="user" type="text" name="accountIn" ></br>
	<input type="submit" name="提交">
	</form>
</body>
</html>


mapper文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  <!--编写需要执行的sql语句,文件名最后+mapper  -->
  <mapper namespace="a.b">
  <!--id相当于方法名   
  	   parameterType参数类型
  	   resultType返回值类型`	
  -->	
	
	<select id="selectOut" parameterType="com.henau.account.Account" resultType="com.henau.account.Account">
		select * from account where account=#{account} and password=#{password}
	</select>
	<select id="selectIn" parameterType="com.henau.account.Account" resultType="com.henau.account.Account">
		select * from account where account=#{account}
	</select>
	<update id="transfer" parameterType="com.henau.account.Account" >
		update account set balance=balance+#{balance} where account=#{account}	
	</update>
  </mapper>

三,总结:
关键是转账功能的逻辑判断,以及事物的提交提交,回滚时机。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值