当前位置: 首页 > news >正文

SpringIOC和SpringAOC

lombok插件
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><!-- 连接工具类 --><bean id="connectionUtil" class="com.xn.util.ConnectionUtil"><property name="dataSource" ref="dataSource"/></bean><!-- 事务工具类 --><bean id="transactionUtil" class="com.xn.util.TransactionUtil"><property name="connectionUtil" ref="connectionUtil"/></bean><!-- 注入dao --><bean id="mapperImp" class="com.xn.dao.AccountMapperImp"><property name="queryRunner" ref="queryRunner"></property><property name="connectionUtil" ref="connectionUtil"></property></bean><!-- 注入service --><bean id="service" class="com.xn.service.AccountServiceImp"><property name="mapper" ref="mapperImp"/><property name="transactionUtil" ref="transactionUtil"></property></bean><!-- 注入controller --><bean id="controller" class="com.xn.controller.AccountControllerImp"><property name="service" ref="service"/></bean>
功能:对实体类自动,动态生成getset,无参有参.....
步骤:1.idea安装插件(只做一次)2.添加坐标
     <!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.26</version></dependency></dependencies>
   3.编写注解dbUtil-阿帕奇提供操作数据库的插件
核心类:QueryRunner.query()  查询.update() 增删改
//操作数据库的核心类QueryRunner queryRunner;public void setQueryRunner(QueryRunner queryRunner) {this.queryRunner = queryRunner;}@Overridepublic void save(Account account) {try {queryRunner.update(connectionUtil.createCon(),"insert into account(aname,amoney) value(?,?)",account.getAname(),account.getAmoney());} catch (SQLException throwables) {throwables.printStackTrace();}}@Overridepublic void updateById(Account account) {try {queryRunner.update(connectionUtil.createCon(),"update  account set aname=?,amoney=? where aid=?",account.getAname(),account.getAmoney(),account.getAid());} catch (SQLException throwables) {throwables.printStackTrace();}}@Overridepublic void deleteById(int id) {try {queryRunner.update(connectionUtil.createCon(),"delete from account where aid=?",id);} catch (SQLException throwables) {throwables.printStackTrace();}}
 <!-- 注入QueryRunner --><bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"><constructor-arg name="ds" ref="dataSource"></constructor-arg></bean>
junit测试
使用步骤:1.坐标
     单元测试--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency>
   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);}}}

注解

 <!-- 加载资源文件 --><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.xn"></context:component-scan>
</beans>

配置工具

项目总结:1.事务管理应该由service层进行实现
代码优化:目的:业务层进行事务管理
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();//查询数据Account sourceAccount=mapper.findByName(sourceName);Account targetAccount=mapper.findByName(targetName);//转账sourceAccount.setAmoney(sourceAccount.getAmoney()-money);targetAccount.setAmoney(targetAccount.getAmoney()+money);//修改数据库mapper.updateById(sourceAccount);
//            int a=10/0;//模拟异常mapper.updateById(targetAccount);transactionUtil.commitTx();} catch (Exception e) {e.printStackTrace();transactionUtil.rollbackTx();} finally {transactionUtil.closeTx();}}
    1.同一个业务方法的多个dao方法公用一个connection对象
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;}@Overridepublic void save(Account account) {try {queryRunner.update(connectionUtil.createCon(),"insert into account(aname,amoney) value(?,?)",account.getAname(),account.getAmoney());} catch (SQLException throwables) {throwables.printStackTrace();}}
   2.ThreadLocal
<!-- 事务工具类 --><bean id="transactionUtil" class="com.xn.util.TransactionUtil"><property name="connectionUtil" ref="connectionUtil"/></bean><!-- 注入dao --><bean id="mapperImp" class="com.xn.dao.AccountMapperImp"><property name="queryRunner" ref="queryRunner"></property><property name="connectionUtil" ref="connectionUtil"></property></bean><!-- 注入service --><bean id="service" class="com.xn.service.AccountServiceImp"><property name="mapper" ref="mapperImp"/><property name="transactionUtil" ref="transactionUtil"></property></bean>
    3.通过连接对象进行事务的统一管理
public class ConnectionUtil {//装配数据源DataSource dataSource;public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}//线程区域对象ThreadLocal<Connection> threadLocal=new ThreadLocal<Connection>();//获取连接public Connection createCon(){try {//获取线程内的连接对象Connection connection=threadLocal.get();//判断if(connection==null){connection=dataSource.getConnection();//创建连接threadLocal.set(connection);//保存}return  connection;} catch (SQLException throwables) {throwables.printStackTrace();}return null;}//移除连接public void removeCon(){threadLocal.remove();//一处连接对象}
}
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();}}}

相关文章:

SpringIOC和SpringAOC

lombok插件 XML<!-- 加载资源文件 --><context:property-placeholder location"classpath:jdbc.properties"></context:property-placeholder><!-- 注入数据源 --><bean id"dataSource" class"com.mchange.v2.c3p0.ComboP…...

static关键字详解

文章目录 static使用示例static底层原理静态初始化顺序静态与线程安全 static static是Java中的一个关键字&#xff0c;用于定义类级别的成员&#xff0c;类级别的成员是指那些属于整个类&#xff0c;而不是特定对象实例的成员。在Java中&#xff0c;类级别的成员包括静态变量…...

使用 Java RestClient 与 Elasticsearch 进行索引管理的示例

文章目录 准备工作测试连接创建索引查询索引是否存在删除索引总结 在这篇博客中&#xff0c;我将和大家分享如何使用 Java RestClient 与 Elasticsearch 进行简单的索引管理操作。如果你在开发过程中需要对海量数据进行高效搜索和分析&#xff0c;Elasticsearch 可能是个不错的…...

编程-设计模式 10:外观模式

设计模式 10&#xff1a;外观模式 定义与目的 定义&#xff1a;外观模式&#xff08;Facade Pattern&#xff09;提供了一个统一的接口&#xff0c;用来访问子系统中的一群接口。它定义了一个高层接口&#xff0c;让子系统更容易使用。目的&#xff1a;简化复杂的子系统的使用…...

非范型ArrayList和泛型List<T>

ArrayList 是 C# 中的一个非泛型集合类&#xff0c;它属于 System.Collections 命名空间。它提供了动态数组的功能&#xff0c;允许你在运行时添加、删除和访问元素。然而&#xff0c;需要注意的是&#xff0c;ArrayList 并不是类型安全的&#xff0c;因为你可以向其中添加任何…...

魔众文库系统v7.0.0版本推荐店铺功能,管理菜单逻辑优化

推荐店铺功能&#xff0c;管理菜单逻辑优化 [新功能] RandomImageProvider 逻辑升级重构&#xff0c;支持更丰富的随机图片生成 [新功能] 资源篮订单参数字段 [新功能] 首页推荐店铺功能&#xff0c;需要在后台 文库系统 → 文库店铺 开启推荐 [系统优化] Grid 快捷编辑请求…...

03、流程控制语句

01、位运算符 一、位运算符:是针对二进制数据(补码)的运算。(0看成false&#xff0c;1看成true) &:按位与——求出两个数字对应的二进制&#xff0c;有0则0 | :按位或——求出两个数字对应的二进制&#xff0c;有1则1 ^ :按位异或 ——求出两个数字对应的二进制&#xff0c…...

[Android] [解决]Bottom Navigation Views Activity工程带来的fragment底部遮盖的问题

创建了Bottom Navigation Views Activity之后&#xff0c;在fragment_home.xml&#xff0c;加了一个RecyclerView&#xff0c; 后来添加了item之后发现底部会被盖住一部分。 解决&#xff1a;在layout里面加两句&#xff1a; android:paddingBottom"?attr/actionBarSize&…...

Armv8/Armv9架构中的原子性

B2.2 Arm 架构中的原子性 原子性是内存访问的一个特性,描述为原子访问。Arm 架构描述涉及两种类型的原子性:单次复制原子性和多次复制原子性。在 Arm 架构中,内存访问的原子性要求取决于内存类型,以及访问是显式的还是隐式的。有关更多信息,请参见: 单次复制原子性的要求…...

读零信任网络:在不可信网络中构建安全系统15协议和过滤

1. 协议 1.1. IKE/IPSec 1.1.1. 因特网密钥交换协议&#xff08;Internet Key Exchange,IKE&#xff09;用于执行IPSec认证和密钥交换 1.1.1.1. 通常以后台守护进程的方式实现&#xff0c;使用预共享密钥或X.509证书来认证对端并创建一个安全会话 1.1.2. IKEv1与IKEv2 1.1.2.1…...

C语言学习笔记 Day11(指针--中2)

Day11 内容梳理&#xff1a; 目录 Chapter 7 指针 7.6 指针 & 函数 &#xff08;1&#xff09;形参改变实参的值 &#xff08;2&#xff09;字符数组作为函数参数 1&#xff09;合并字符串 2&#xff09;删掉字符串中空格 &#xff08;3&#xff09;指针作为函数返…...

Golang 并发编程

Golang 并发编程 Goroutine 什么是协程 创建 Goroutine 主 goroutine &#xff08;main函数&#xff09;退出后&#xff0c;其它的工作 goroutine 也会自动退出 package mainimport ("fmt""time" )func myFunc() {i : 0for {ifmt.Println("func: …...

【数据结构详解】——选择排序(动图详解)

目录 &#x1f552; 1. 直接选择排序&#x1f552; 2. 堆排序 &#x1f552; 1. 直接选择排序 &#x1f4a1; 算法思想&#xff1a;第一次从待排序的数据元素中选出最小&#xff08;或最大&#xff09;的一个元素&#xff0c;存放在序列的起始&#xff08;末尾&#xff09;位置…...

杂项命令(笔记)

ifconfig &#xff1a;http://t.csdnimg.cn/gT2AR echo :http://t.csdnimg.cn/6DSoO ps和top的区别 http://t.csdnimg.cn/f1XWt...

代码随想录算法训练营Day38||完全背包问题、leetcode 518. 零钱兑换 II 、 377. 组合总和 Ⅳ 、70. 爬楼梯 (进阶)

一、完全背包问题 相较于01背包&#xff0c;完全背包的显著特征是每个物品可以用无数次&#xff0c;遍历顺序也不需要为了保证每个物品只去一次而倒序遍历。 #include<iostream> #include<vector> using namespace std; int main(){int N,V;cin>>N>>V…...

超越链端:Web3的无边界技术革命

Web3&#xff0c;作为互联网技术的第三代变革&#xff0c;正以其去中心化、开放透明的特性&#xff0c;重新定义着我们的数字生活。在这一背景下&#xff0c;“链端”概念逐渐成为热点&#xff0c;意味着我们不仅仅局限于区块链技术本身&#xff0c;而是探索其在更广泛领域的应…...

127. Go反射基本原理

文章目录 反射基础 - go 的 interface 是怎么存储的&#xff1f;iface 和 eface 的结构体定义&#xff08;runtime/iface.go&#xff09;&#xff1a;_type 是什么&#xff1f;itab 是什么&#xff1f; 反射对象 - reflect.Type 和 reflect.Value反射三大定律Elem 方法reflect.…...

提高PDF电子书的分辨率

解决方法出处 1. 安装ImageMagick brew install imagemagick brew install ghostscript2. 按流程进行 convert -density 600 your_pdf_filename.pdf output-%02d.jpg convert output*.jpg -normalize -threshold 80% final-%02d.jpg convert final*.jpg my_new_highcontras…...

Spring Cloud全解析:注册中心之zookeeper注册中心

zookeeper注册中心 使用zookeeper作为注册中心就不需要像eureka一样&#xff0c;在写一个eureka-server的服务了&#xff0c;因为zookeeper本身就是一个服务端&#xff0c;只需要编写需要进行服务注册的客户端即可 依赖 <!-- zookeeper 注册中心 --> <dependency&g…...

解决戴尔台式电脑休眠后无法唤醒问题

近期发现有少量戴尔的台式机会有休眠后无法唤醒的问题&#xff0c;具体现象就是电脑在休眠后&#xff0c;电源指示灯以呼吸的频率闪烁&#xff0c;无论怎么点鼠标和键盘都没有反应&#xff0c;并且按开机按钮也没法唤醒&#xff0c;只能是长按开机键强制关机再重启才行&#xf…...

【大模型RAG】拍照搜题技术架构速览:三层管道、两级检索、兜底大模型

摘要 拍照搜题系统采用“三层管道&#xff08;多模态 OCR → 语义检索 → 答案渲染&#xff09;、两级检索&#xff08;倒排 BM25 向量 HNSW&#xff09;并以大语言模型兜底”的整体框架&#xff1a; 多模态 OCR 层 将题目图片经过超分、去噪、倾斜校正后&#xff0c;分别用…...

eNSP-Cloud(实现本地电脑与eNSP内设备之间通信)

说明&#xff1a; 想象一下&#xff0c;你正在用eNSP搭建一个虚拟的网络世界&#xff0c;里面有虚拟的路由器、交换机、电脑&#xff08;PC&#xff09;等等。这些设备都在你的电脑里面“运行”&#xff0c;它们之间可以互相通信&#xff0c;就像一个封闭的小王国。 但是&#…...

JavaSec-RCE

简介 RCE(Remote Code Execution)&#xff0c;可以分为:命令注入(Command Injection)、代码注入(Code Injection) 代码注入 1.漏洞场景&#xff1a;Groovy代码注入 Groovy是一种基于JVM的动态语言&#xff0c;语法简洁&#xff0c;支持闭包、动态类型和Java互操作性&#xff0c…...

全球首个30米分辨率湿地数据集(2000—2022)

数据简介 今天我们分享的数据是全球30米分辨率湿地数据集&#xff0c;包含8种湿地亚类&#xff0c;该数据以0.5X0.5的瓦片存储&#xff0c;我们整理了所有属于中国的瓦片名称与其对应省份&#xff0c;方便大家研究使用。 该数据集作为全球首个30米分辨率、覆盖2000–2022年时间…...

MVC 数据库

MVC 数据库 引言 在软件开发领域,Model-View-Controller(MVC)是一种流行的软件架构模式,它将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller)。这种模式有助于提高代码的可维护性和可扩展性。本文将深入探讨MVC架构与数据库之间的关系,以…...

vue3 定时器-定义全局方法 vue+ts

1.创建ts文件 路径&#xff1a;src/utils/timer.ts 完整代码&#xff1a; import { onUnmounted } from vuetype TimerCallback (...args: any[]) > voidexport function useGlobalTimer() {const timers: Map<number, NodeJS.Timeout> new Map()// 创建定时器con…...

html-<abbr> 缩写或首字母缩略词

定义与作用 <abbr> 标签用于表示缩写或首字母缩略词&#xff0c;它可以帮助用户更好地理解缩写的含义&#xff0c;尤其是对于那些不熟悉该缩写的用户。 title 属性的内容提供了缩写的详细说明。当用户将鼠标悬停在缩写上时&#xff0c;会显示一个提示框。 示例&#x…...

MySQL 部分重点知识篇

一、数据库对象 1. 主键 定义 &#xff1a;主键是用于唯一标识表中每一行记录的字段或字段组合。它具有唯一性和非空性特点。 作用 &#xff1a;确保数据的完整性&#xff0c;便于数据的查询和管理。 示例 &#xff1a;在学生信息表中&#xff0c;学号可以作为主键&#xff…...

淘宝扭蛋机小程序系统开发:打造互动性强的购物平台

淘宝扭蛋机小程序系统的开发&#xff0c;旨在打造一个互动性强的购物平台&#xff0c;让用户在购物的同时&#xff0c;能够享受到更多的乐趣和惊喜。 淘宝扭蛋机小程序系统拥有丰富的互动功能。用户可以通过虚拟摇杆操作扭蛋机&#xff0c;实现旋转、抽拉等动作&#xff0c;增…...

Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement

Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement 1. LAB环境2. L2公告策略2.1 部署Death Star2.2 访问服务2.3 部署L2公告策略2.4 服务宣告 3. 可视化 ARP 流量3.1 部署新服务3.2 准备可视化3.3 再次请求 4. 自动IPAM4.1 IPAM Pool4.2 …...