package com.by.util;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* JDBCTemplate工具类
*/
public class JDBCUtils {
private static final DataSource dataSource;
static {
Properties p = new Properties(); //创建集合
try ( //FileInputStream fis=new FileInputStream("src/JDBCUtils.properties")
InputStream is=JDBCUtils.class.getResourceAsStream("/JDBCUtils.properties") //将文件路径与项目结构脱离
) {
p.load(is); //利用IO流将配置文件的内容读取到集合中
} catch (Exception e) {
System.out.println("未知异常!");
e.printStackTrace();
}
dataSource = new DriverManagerDataSource(p.getProperty("url"), p.getProperty("username"), p.getProperty("password"));
}
/**
* 获取JDBCTemplate对象
* @return JDBCTemplate对象
*/
public static JdbcTemplate getJDBCTemplate(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); //创建JDBCTemplate
return jdbcTemplate;
}
/**
* 获取数据库连接池对象
* @return
*/
public static DataSource getDataSource(){
return dataSource;
}
/**
* 获取连接,开启事务
* @return
*/
public static Connection startTransaction(){
if (!TransactionSynchronizationManager.isSynchronizationActive()) { //判断是否开启了线程绑定
TransactionSynchronizationManager.initSynchronization(); //开启线程绑定
}
Connection conn = DataSourceUtils.getConnection(dataSource);
try {
conn.setAutoCommit(false);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return conn;
}
/**
* 提交事务
* @param conn 用来提交事务的数据库连接
*/
public static void commit(Connection conn){
try {conn.commit();
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{clear(conn);}
}
/**
* 回滚事务
* @param conn
*/
public static void rollback(Connection conn){
try {conn.rollback();
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{clear(conn);}
}
public static void clear(Connection conn){
TransactionSynchronizationManager.clear(); //清理资源
TransactionSynchronizationManager.unbindResourceIfPossible(dataSource); //解除绑定
if(conn!=null){ //归还连接
DataSourceUtils.releaseConnection(conn,dataSource);
}
}
}
srccombyserviceAccountService.java
package com.by.service;
public interface AccountService {
/**
* 转账功能
* @param fromName 转出人姓名
* @param toName 转入人姓名
* @param money 转账金额
* @return 转账结果
*/
boolean transfer(String fromName, String toName, double money);
}
srccombyserviceimplAccountServiceImpl.java
package com.by.service.impl;
import com.by.dao.AccountDao;
import com.by.dao.impl.AccountDaoImpl;
import com.by.entity.Account;
import com.by.service.AccountService;
import com.by.util.JDBCUtils;
import org.springframework.jdbc.datasource.DataSourceUtils;
import java.sql.Connection;
import java.sql.SQLException;
public class AccountServiceImpl implements AccountService {
private AccountDao ad = new AccountDaoImpl();
@Override
public boolean transfer(String fromName, String toName, double money) {
boolean boo=false;//用来返回
Connection conn=null;
try{
//开启事务
conn = JDBCUtils.startTransaction();
//功能实现: 调用DAO+逻辑控制
//判断转出人是否存在
Account from = ad.selectAccountByUsername(fromName);
//判断转出人是否存在
if (from == null) {
throw new RuntimeException("转出人不存在!");
} else {
//判断余额
if (from.getBalance() < money) {
throw new RuntimeException("余额不足");
} else {
//查询转入人
Account to = ad.selectAccountByUsername(toName);
if (to == null) {
throw new RuntimeException("转入人不存在");
} else {
//执行转账
int n = ad.updateAccountByUsername(new Account(null, fromName, null, from.getBalance() - money));
if (true) {
throw new RuntimeException("这是我的测试异常");
}
n += ad.updateAccountByUsername(new Account(null, toName, null, to.getBalance() + money));
if (n == 2) {
boo= true;
}
}
}
}
JDBCUtils.commit(conn); //提交事务
}catch(Exception e){
try{ //回滚事务
System.out.println("事务正在回滚。。。");
JDBCUtils.rollback(conn);
}catch(RuntimeException ex){
throw ex;
}
throw new RuntimeException(e);
}
return boo;
}
}
srccombyviewview3UpdateAccountTest.java
package com.by.view.view3;
import com.by.service.AccountService;
import com.by.service.impl.AccountServiceImpl;
import java.util.Scanner;
public class UpdateAccountTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//接收转出人的账号名
System.out.println("请输入转出人账号名:");
String fromName = sc.next();
System.out.println("请输入转账金额:");
double money = sc.nextDouble();
//存在,接收转入人账号名
System.out.println("请输入转入人姓名:");
String toName = sc.next();
//创建业务层对象
AccountService as = new AccountServiceImpl();
try {
if (as.transfer(fromName, toName, money)) {
System.out.println("转账成功");
} else {
System.out.println("转账失败");
}
} catch (Exception e) {
System.out.println("转账失败!原因为:"+e.getMessage());
}
}
}
原文地址:https://blog.csdn.net/webxscan/article/details/134699025
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_33147.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。