SpringIOC整合dbUtil做的增删改查以及转账业务的实现
目录
一、xml方式实现
1.介绍lombok插件
2.功能
3.步骤
3.1 idea安装插件(只做一次)
3.2 添加坐标
3.3 编写注解
4.核心类
4.1 QueryRunner
4.2 query() 查询
4.3 update() 增删改
5.配置文件applicationContext.xml
6.junit测试
6.1使用步骤
6.1.1 坐标
6.1.2 注解(修饰方法)
二、annotation注解方式实现
1.控制层(cotroller)
2.业务层(service)
3.数据访问层(dao)
4.配置文件applicationContext.xml
三、configuration配置类方式实现
1.ApplicationConfig
2.DataConfig 替换applicationContext.xml
3.测试类
四、在xml基础上实现转账业务
1.同一个业务方法的多个dao方法公用一个connection对象
2.ThreadLocal
3.通过连接对象进行事务的统一管理
5.项目总结:
一、xml方式实现
1.介绍lombok插件
dbUtil-阿帕奇提供操作数据库的插件
2.功能
对实体类自动,动态生成getset,无参有参 toString.....
3.步骤
3.1 idea安装插件(只做一次)

3.2 添加坐标
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
3.3 编写注解
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Account implements Serializable {private int aid;private String aname;private int amoney;public Account(String aname, int amoney) {this.aname = aname;this.amoney = amoney;}
}
4.核心类
4.1 QueryRunner
//操作数据库的核心类QueryRunner queryRunner;public void setQueryRunner(QueryRunner queryRunner) {this.queryRunner = queryRunner;}
4.2 query() 查询
@Overridepublic void save(Account account) {try {queryRunner.update("insert into account(aname,amoney) value(?,?)",account.getAname(),account.getAmoney());} catch (SQLException throwables) {throwables.printStackTrace();}}
@Override
public void updateById(Account account) {try {queryRunner.update("udpate account set aname=?,amoney=? where aid=?",account.getAname(),account.getAmoney(),account.getAid());} catch (SQLException throwables) {throwables.printStackTrace();}
}@Override
public void deleteById(int id) {try {queryRunner.update("delete from account where aid=?",id);} catch (SQLException throwables) {throwables.printStackTrace();}
}
4.3 update() 增删改
@Override
public Account findByName(String name) {try {return queryRunner.query("select * from account where aname=?",new BeanHandler<Account>(Account.class),name);} catch (SQLException throwables) {throwables.printStackTrace();}return null;
}@Override
public List<Account> findAll() {try {return queryRunner.query("select * from account",new BeanListHandler<Account>(Account.class));} catch (SQLException throwables) {throwables.printStackTrace();}return null;
}
5.配置文件applicationContext.xml
<!--加载资源文件--><context:property-placeholder location="jdbc.properties"></context:property-placeholder><!--注入数据源--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${msg1}"></property><property name="jdbcUrl" value="${msg2}"></property><property name="user" value="${msg3}"></property><property name="password" value="${msg4}"></property></bean><!--注入QueryRunner--><bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"><constructor-arg name="ds" ref="dataSource"></constructor-arg></bean><!--注入dao--><bean id="mapperImp" class="com.ztt.dao.AccountMapperImp"><property name="queryRunner" ref="queryRunner"></property></bean><!--注入service--><bean id="service" class="com.ztt.service.AccountServiceImp"><property name="mapper" ref="mapperImp"></property></bean><!--注入controller--><bean id="controller" class="com.ztt.controller.AccountControllerImp"><property name="service" ref="service"></property></bean>
6.junit测试
6.1使用步骤
6.1.1 坐标
<!--单元测试-->
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope>
</dependency>
<!--数据源--><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version>
</dependency>
6.1.2 注解(修饰方法)
@Test======>可以运行的方法
@Before====>@Test运行之前
@After=====>@Test运行之后
方式一:
public class Test01 {ClassPathXmlApplicationContext applicationContext =null;IAccountController controller = null;@Beforepublic void beforeMethod(){applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");controller = (IAccountController) applicationContext.getBean("controller");}@Afterpublic void afterMethod(){applicationContext.close();}@Testpublic void show1(){controller.save(new Account("张甜甜",2000));controller.save(new Account("许娜",2000));}@Testpublic void show2(){List<Account> all = controller.findAll();for (int i = 0; i < all.size(); i++) {Account account = all.get(i);System.out.println(account);}}}
方式二:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Test02 {@AutowiredIAccountController controller;@Testpublic void show1(){controller.save(new Account("张甜甜",2000));controller.save(new Account("许娜",2000));}@Testpublic void show2(){List<Account> all = controller.findAll();for (int i = 0; i < all.size(); i++) {Account account = all.get(i);System.out.println(account);}}@Testpublic void show3(){controller.transfer("张甜甜","许娜",100);}
}
二、annotation注解方式实现
1.控制层(cotroller)
@Controller("controller")
public class AccountControllerImp implements IAccountController {@AutowiredIAccountService service;
2.业务层(service)
@Service
public class AccountServiceImp implements IAccountService{@AutowiredIAccountMapper mapper;
3.数据访问层(dao)
@Repository
public class AccountMapperImp implements IAccountMapper{//操作数据库的核心类@AutowiredQueryRunner queryRunner;
4.配置文件applicationContext.xml
<!--加载资源文件--><context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder><!--注入数据源--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${msg1}"></property><property name="jdbcUrl" value="${msg2}"></property><property name="user" value="${msg3}"></property><property name="password" value="${msg4}"></property></bean><!--注入QueryRunner--><bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"><constructor-arg name="ds" ref="dataSource"></constructor-arg></bean><!--扫描--><context:component-scan base-package="com.ztt"></context:component-scan>
测试类同上
三、configuration配置类方式实现
在三层框架的基础上新建一个包config,用来写配置类
1.ApplicationConfig
@Configuration
@ComponentScan(basePackages = "com.ztt")
@Import(DataConfig.class)
public class ApplicationConfig {
}
2.DataConfig 替换applicationContext.xml
@Configuration
@PropertySource(value = "classpath:jdbc.properties")
public class DataConfig {@Value("${msg1}")private String driverClass;@Value("${msg2}")private String jdbcUrl;@Value("${msg3}")private String user;@Value("${msg4}")private String password;// <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
// <property name="driverClass" value="${msg1}"></property>
// <property name="jdbcUrl" value="${msg2}"></property>
// <property name="user" value="${msg3}"></property>
// <property name="password" value="${msg4}"></property>
// </bean>@Beanpublic DataSource dataSource(){try {ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();comboPooledDataSource.setDriverClass(driverClass);comboPooledDataSource.setJdbcUrl(jdbcUrl);comboPooledDataSource.setUser(user);comboPooledDataSource.setPassword(password);return comboPooledDataSource;} catch (PropertyVetoException e) {e.printStackTrace();}return null;}// <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
// <constructor-arg name="ds" ref="dataSource"></constructor-arg>
// </bean>@Beanpublic QueryRunner queryRunner(){return new QueryRunner(dataSource());}}
3.测试类


四、在xml基础上实现转账业务
目的:业务层进行事务管理
1.同一个业务方法的多个dao方法公用一个connection对象
2.ThreadLocal
3.通过连接对象进行事务的统一管理
ConnectionUtil连接工具类:
public class ConnectionUtil {//装配数据源DataSource dataSource;public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}//线程区域对象ThreadLocal<Connection> threadLocal=new ThreadLocal<Connection>();//获取连接public Connection createCon(){Connection connection = null;try {//1.获取线程内的连接对象connection=threadLocal.get();//2.判断if(connection==null){connection=dataSource.getConnection();//创建连接threadLocal.set(connection);//保存}return connection;} catch (SQLException throwables) {throwables.printStackTrace();}return connection;}//移除连接public void removeCon(){threadLocal.remove();//移除连接}}
TransactionUtil事务管理工具类:
public class TransactionUtil {//注入连接工具类ConnectionUtil connectionUtil;public void setConnectionUtil(ConnectionUtil connectionUtil) {this.connectionUtil = connectionUtil;}//开启事务public void beginTx(){try {connectionUtil.createCon().setAutoCommit(false);} catch (SQLException throwables) {throwables.printStackTrace();}}//提交事务public void commitTx(){try {connectionUtil.createCon().commit();} catch (SQLException throwables) {throwables.printStackTrace();}}//回滚事务public void rollbackTx(){try {connectionUtil.createCon().rollback();} catch (SQLException throwables) {throwables.printStackTrace();}}//关闭事务public void closeTx(){try {connectionUtil.createCon().close();//关闭事务connectionUtil.removeCon();//移除事务} catch (SQLException throwables) {throwables.printStackTrace();}}
}
AccountMapperImp:
public class AccountMapperImp implements IAccountMapper{//操作数据库的核心类QueryRunner queryRunner;public void setQueryRunner(QueryRunner queryRunner) {this.queryRunner = queryRunner;}//注入连接工具ConnectionUtil connectionUtil;public void setConnectionUtil(ConnectionUtil connectionUtil) {this.connectionUtil = connectionUtil;}

AccountServiceImp:
public class AccountServiceImp implements IAccountService{IAccountMapper mapper;public void setMapper(IAccountMapper mapper) {this.mapper = mapper;}//装配TransactionUtil transactionUtil;public void setTransactionUtil(TransactionUtil transactionUtil) {this.transactionUtil = transactionUtil;}@Overridepublic void transfer(String sourceName, String targetName, int money) {try {transactionUtil.beginTx();//1.查询数据Account sourceAccount = mapper.findByName(sourceName);Account targetAccount = mapper.findByName(sourceName);//2.转账sourceAccount.setAmoney(sourceAccount.getAmoney()-money);targetAccount.setAmoney(targetAccount.getAmoney()+money);//3.修改数据库mapper.updateById(sourceAccount);int a=10/0;//模拟异常mapper.updateById(targetAccount);transactionUtil.commitTx();} catch (Exception e) {e.printStackTrace();transactionUtil.rollbackTx();} finally {transactionUtil.closeTx();}}
AccountControllerImp:
public class AccountControllerImp implements IAccountController {IAccountService service;public void setService(IAccountService service) {this.service = service;}@Overridepublic void transfer(String sourceName, String targetName, int money) {service.transfer(sourceName,targetName,money);}public void save(Account account) {service.save(account);}
配置文件applicationContext.xml
在原有的基础上注入连接工具类、事务工具类、以及在业务层注入事务管理工具类
<!--连接工具类-->
<bean id="connectionUtil" class="com.ztt.util.ConnectionUtil">
<property name="dataSource" ref="dataSource"></property>
</bean><!--事务工具类-->
<bean id="transactionUtil" class="com.ztt.util.TransactionUtil">
<property name="connectionUtil" ref="connectionUtil"></property>
</bean><!--注入dao-->
<bean id="mapperImp" class="com.ztt.dao.AccountMapperImp">
<property name="queryRunner" ref="queryRunner"></property>
</bean><!--注入service-->
<bean id="service" class="com.ztt.service.AccountServiceImp">
<property name="mapper" ref="mapperImp"></property>
<property name="transactionUtil" ref="transactionUtil"></property>
</bean><!--注入controller-->
<bean id="controller" class="com.ztt.controller.AccountControllerImp">
<property name="service" ref="service"></property>
</bean>
测试方法:
@Testpublic void show3(){controller.transfer("张甜甜","许娜",100);}
5.项目总结:
1.事务管理应该由service层进行实现
代码优化:
目的:业务层进行事务管理
1.同一个业务方法的多个dao方法公用一个connection对象
2.ThreadLocal
3.通过连接对象进行事务的统一管理
相关文章:
SpringIOC整合dbUtil做的增删改查以及转账业务的实现
目录 一、xml方式实现 1.介绍lombok插件 2.功能 3.步骤 3.1 idea安装插件(只做一次) 3.2 添加坐标 3.3 编写注解 4.核心类 4.1 QueryRunner 4.2 query() 查询 4.3 update() 增删改 5.配置文件applicationContext.xml 6.junit测试 6.1使用步骤 6.1.1 坐标 6.1.2…...
【Nacos无压力源码领读】(二) 集成 LoadBanlancer 与 OpenFeign
上一篇文章中, 详细介绍了 Nacos 注册中心的原理, 相信看完后, 大家应该完全掌握了 Nacos 客户端是如何自动进行服务注册的, 以及 Nacos 客户端是如何订阅服务实例信息的, 以及 Nacos 服务器是如何处理客户端的注册和订阅请求的; 本文承上启下, 在订阅服务实例的基础上, 介绍如…...
CP AUTOSAR标准之DefaultErrorTracer(AUTOSAR_SWS_DefaultErrorTracer)(更新中……)
1 简介和功能概述 本规范描述了默认错误跟踪器的API。基础软件中检测到的所有开发和运行时错误都会报告给此模块。API参数允许跟踪错误来源和类型: 检测到错误的模块检测到错误的函数错误类型此模块API背后的功能不在本规范的范围内。软件开发人员和软件集成商应根据其特定应用…...
SpringMVC (发送请求——>参数传递—— >响应数据)
设置请求访问路径 RequestMapper:将请求访问路径和我们业务层的方法联系起来 ResponseBody:将我们业务层方法的返回值转化为json,xml或其他格式的数据返回给页面 两种请求 get请求 post请求 测试案例 RequestMapping("/getNameAndAge&…...
认识Modbus RTU与Modbus TCP
(选自成都纵横智控-Modbus RTU与Modbus TCP协议区别详解 ) Modbus RTU 和 Modbus TCP 是两种常用的工业通信协议,用于连接电子设备,但它们在多方面有所不同。以下是它们的详细比较: Modbus RTU 协议类型: …...
如何在 Kubernetes 中使用 ClickHouse 和 JuiceFS
ClickHouse 结合 JuiceFS 一直是一个热门的组合,社区中有多篇实践案例。今天的文章来自美国公司 Altinity,一家提供 ClickHouse 商业服务的企业,作者是 Vitaliy Zakaznikov,他尝试了这个组合并公开了过程中使用的代码。原文有两篇…...
云计算任务调度优化matlab仿真,对比蚁群优化和蛙跳优化
目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.本算法原理 4.1 ACO蚁群优化 4.2 蛙跳优化 5.完整程序 1.程序功能描述 云计算任务调度优化,优化目标位任务消耗时间,调度后的经济效益以及设备功耗,对比蚁群优化算法和蛙跳优化…...
基于双PI+EKF扩展卡尔曼滤波的PMSM速度控制simulink建模与仿真
目录 1.课题概述 2.系统仿真结果 3.核心程序与模型 4.系统原理简介 4.1 PMSM数学模型 4.2 双PI控制器设计 4.3 扩展卡尔曼滤波器(EKF) 4.4 控制系统实现 5.完整工程文件 1.课题概述 基于双PIEKF扩展卡尔曼滤波的PMSM速度控制simulink建模与仿真。对比基于双PI的扩展卡…...
医疗器械注册资源宝库数屿医械官方平台!
医学影像设备市场作为医疗器械领域的佼佼者,技术门槛高且规模庞大,2021年全球规模达458亿美元,预计2022年逼近500亿美元,增长动力源自技术革新与临床需求攀升。中国市场亦不甘落后,受政策驱动与市场需求双重提振&#…...
Django如何移除数据库字段?
关键步骤: 第一步:python manage.py makemigrations 你的项目名称第二步: python manage.py migrate (.venv) PS D:\python_workpace\django_xitong_shezhi\pythonProject\myproject> python manage.py makemigrations myproject Migra…...
阶段项目——拼图小游戏
Java学习笔记(新手纯小白向) 第一章 JAVA基础概念 第二章 JAVA安装和环境配置 第三章 IntelliJ IDEA安装 第四章 运算符 第五章 运算符联系 第六章 判断与循环 第七章 判断与循环练习 第八章 循环高级综合 第九章 数组介绍及其内存图 第十章 数…...
基于本地消息表实现分布式事务(最终一致性)
前言 传统单体架构下,所有的功能模块都在一个应用下,所有的代码和业务逻辑都在同一个应用下实现,所以保证数据的一致性就很简单,保证相关操作都在同一个本地事务下就可以了。 但是在微服务架构下,将一个应用拆分成了…...
大数据mapper书写范式hdfs
文章目录 1. 大数据mapper书写范式hdfs 1. 大数据mapper书写范式hdfs import json import sysdef read_input(input_stream):for line in input_stream:yield line.rstrip(\n)def load_json_data(json_line):try:data json.loads(json_line)unique_id data.get(id)combined_…...
ubuntu将软件放到任务栏
右键点击这个 pycharm 方法1: 方法2: sudo nano /usr/share/applications/PyCharm.desktop 编辑这个 [Desktop Entry] NamePyCharm CommentPyCharm Integrated Development Environment Exec/path/to/PyCharm.sh Icon/path/to/PyCharm.svg Terminalf…...
Spring Boot 参数校验 Validation 使用
概述 当我们想提供可靠的 API 接口,对参数的校验,以保证最终数据入库的正确性,是必不可少的活。前、后端校验都是保证参数的准确性的手段之一,前端校验并不安全,任何人都可以通过接口来调用我们的服务,就算…...
基于el-table的表格点选和框选功能
开篇 本篇文章旨在实现一个基于el-table的表格点选和框选功能,除此之外,还支持多种模式的切换、自定义勾选日期等。且,该表格后续可能还会持续优化! 功能介绍 表格点选和框选功能(没有点击ctrl键的情况下)…...
LabVIEW压电陶瓷阻抗测试系统
开发了一种基于LabVIEW软件与PXI模块化仪器的压电陶瓷阻抗测试系统。该系统能在高电压工作条件下测量压电陶瓷的阻抗特性,包括阻抗模值与阻抗角的频率特性,为压电陶瓷的进一步分析与应用提供了重要参考。 项目背景 现有的阻抗测试仪大多只能在低电压条件…...
电销机器人能大幅度提升效率
1、安全稳定性能好 营销机器人的稳定性非常强,在使用性能方面会有更好的优势,而且用的过程中也可以不断的这些模块更新和功能升级,所以会不断的满足大家更多的使用要求,在操作使用的时候非常简单和方便,直接就可以给客…...
虚拟机能访问网页但ping不通百度
最近遇到了奇怪的问题,虚拟机能访问网页,但ping不通百度,记录一下问题的排查过程。 能访问网页,说明DNS、TCP和HTTP没有问题,ping不通,说明ICMP应该出了问题。 首先通过traceroute追踪报文的转发过程&…...
RK3588开发笔记-buildroot编译配置
目录 前言 一、buildroot简介 二、buildroot配置编译 buildroot config配置 buildroot 编译 buildroot 如何单独编译某个软件包 何时需要完全重建 如何完全重建 总结 前言 Rockchip RK3588 是一款强大的多核处理器,广泛应用于边缘计算、人工智能、嵌入式系统等领域。为了在…...
VMware macOS虚拟机解锁方案:开源工具Unlocker完整实践指南
VMware macOS虚拟机解锁方案:开源工具Unlocker完整实践指南 【免费下载链接】unlocker VMware Workstation macOS 项目地址: https://gitcode.com/gh_mirrors/unloc/unlocker 你是否想在Windows或Linux系统上运行macOS虚拟机,却苦于VMware不支持…...
蓝桥杯备赛:Day5-P1706 全排列问题
📚 算法笔记:P1706 全排列问题 (DFS 基础) 1. 题目描述 P1706 全排列问题 - 洛谷 输出 1∼N1 \sim N1∼N 的所有全排列,要求每个数字占 5 个场宽,排列按字典序从小到大输出。 2. 核心代码 (C 版本) #include <bits/stdc.h…...
Thor命令行工具包终极指南:从零构建高效CLI应用的完整教程
Thor命令行工具包终极指南:从零构建高效CLI应用的完整教程 【免费下载链接】thor Thor is a toolkit for building powerful command-line interfaces. 项目地址: https://gitcode.com/gh_mirrors/th/thor Thor是一个强大而简洁的Ruby命令行工具包࿰…...
在RK3588上搞定XDMA AXI-Stream回环测试:从Verilog到Rust的完整流程与避坑指南
RK3588平台XDMA AXI-Stream全链路开发实战:从FPGA设计到Rust测试的工程化实现 当我们需要在嵌入式系统中实现高速数据交换时,PCIeAXI-Stream的组合无疑是黄金搭档。RK3588作为一款高性能处理器,配合FPGA的灵活可编程特性,能够构建…...
VideoAgentTrek Screen Filter作品集:处理前后对比展示多行业应用效果
VideoAgentTrek Screen Filter作品集:处理前后对比展示多行业应用效果 最近在整理一些屏幕录像素材时,我发现了一个挺有意思的工具——VideoAgentTrek Screen Filter。它不是什么复杂的视频编辑软件,核心功能很聚焦:自动识别并处…...
3天从零到精通:录播姬全方位实战指南
3天从零到精通:录播姬全方位实战指南 【免费下载链接】BililiveRecorder 录播姬 | mikufans 生放送录制 项目地址: https://gitcode.com/gh_mirrors/bi/BililiveRecorder 你是否曾经因为错过心爱主播的直播而感到遗憾?是否在录制直播时遇到各种技…...
Altium Designer 22 实战:用这个隐藏技巧,为DIP插件焊盘穿上‘防烫服’
Altium Designer 22实战:用丝印层为DIP焊盘打造双重防护 手工焊接DIP插件时,烙铁温度过高或操作不当导致绿油层损坏,进而引发短路问题,是硬件工程师经常遇到的痛点。特别是在生产线上,工人为了效率往往将烙铁温度调至最…...
三场正交相位场统一论2.0——宇宙底层规律与星际航行的终极答案
前言:打破百年物理枷锁,回归宇宙本质逻辑从麦克斯韦完成电磁力的初步统一,到特斯拉触摸到反重力的技术边缘,从爱因斯坦构建时空弯曲的引力假说,到张祥前提出空间螺旋统一场模型,人类为破解宇宙四大力统一的…...
5个核心操作完成HMCL启动器数据无缝迁移:告别重装烦恼
5个核心操作完成HMCL启动器数据无缝迁移:告别重装烦恼 【免费下载链接】HMCL A Minecraft Launcher which is multi-functional, cross-platform and popular 项目地址: https://gitcode.com/gh_mirrors/hm/HMCL 当你的Minecraft世界从一个设备转移到另一个设…...
惊艳音效生成效果:HunyuanVideo-Foley实际作品展示与测评
惊艳音效生成效果:HunyuanVideo-Foley实际作品展示与测评 你肯定有过这样的经历:精心拍摄了一段视频,画面构图、光影、运镜都堪称完美,但导出后总觉得少了点什么。对,就是声音。画面里的人物在奔跑,却听不…...
