SpringIOC整合dbUtil做的增删改查以及转账业务的实现

news2024/9/21 19:06:28

目录

一、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() 查询
@Override
    public 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;


    @Before
    public void beforeMethod(){
        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        controller = (IAccountController) applicationContext.getBean("controller");
    }


    @After
    public void afterMethod(){
        applicationContext.close();
    }


    @Test
    public void show1(){
        controller.save(new Account("张甜甜",2000));
        controller.save(new Account("许娜",2000));
    }

    @Test
    public 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 {
    @Autowired
    IAccountController controller;


    @Test
    public void show1(){
        controller.save(new Account("张甜甜",2000));
        controller.save(new Account("许娜",2000));
    }

    @Test
    public void show2(){
        List<Account> all = controller.findAll();
        for (int i = 0; i < all.size(); i++) {
            Account account =  all.get(i);
            System.out.println(account);
        }
    }
    @Test
    public void show3(){
        controller.transfer("张甜甜","许娜",100);
    }
}

二、annotation注解方式实现

1.控制层(cotroller)
@Controller("controller")
public class AccountControllerImp implements IAccountController {
    @Autowired
    IAccountService service;
2.业务层(service)
@Service
public class AccountServiceImp implements IAccountService{
    @Autowired
    IAccountMapper mapper;
3.数据访问层(dao)
@Repository
public class AccountMapperImp implements IAccountMapper{

    //操作数据库的核心类
    @Autowired
    QueryRunner 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>

    @Bean
    public 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>

    @Bean
    public 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;
    }

    @Override
    public 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;
    }

    @Override
    public 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>

测试方法:

@Test
    public void show3(){
        controller.transfer("张甜甜","许娜",100);
    }
5.项目总结:

1.事务管理应该由service层进行实现

代码优化:

目的:业务层进行事务管理

1.同一个业务方法的多个dao方法公用一个connection对象

2.ThreadLocal

3.通过连接对象进行事务的统一管理

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1990501.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Material-UI】Button Group 中的 Disabled Elevation 功能

文章目录 一、Button Group 组件概述二、什么是 Elevation&#xff1f;三、为什么需要禁用 Elevation&#xff1f;四、使用 disableElevation 属性五、属性解析1. disableElevation 属性2. variant 属性3. aria-label 属性 六、应用场景1. 表单操作2. 工具栏3. 导航按钮 七、样…

vue中v-html 后端返回html + script js中click事件不生效

效果图&#xff1a; 需求&#xff1a;点击加号执行后端返回的script中的代码 后端返回的html&#xff1a; <!DOCTYPE html> <html langzh> <head> <title>xxx</title> <style>body{font-size: 14px}p{text-indent: 30px;}textarea{width…

PythonStudio 控件使用常用方式(十三)TScrollBox

PythonStudio是一个极强的开发Python的IDE工具&#xff0c;它使用的是Delphi的控件&#xff0c;常用的内容是与Delphi一致的。但是相关文档并一定完整。现在我试试能否逐步把它的控件常用用法写一点点&#xff0c;也作为PythonStudio的参考。 从1.2.1版开始&#xff0c;Python…

(Qt) QThread 信号槽所在线程

文章目录 &#x1f481;&#x1f3fb;前言&#x1f481;&#x1f3fb;Code&#x1f481;&#x1f3fb;‍♂️Code&#x1f481;&#x1f3fb;‍♂️环境 &#x1f481;&#x1f3fb;当前线程信号&#x1f481;&#x1f3fb;‍♂️默认效果&#x1f481;&#x1f3fb;‍♂️Qt::…

RTOS(7)队列

1.队列的理论知识 下面的结构体里包含了&#xff1a;头部指针&#xff0c;写指针&#xff0c;读指针&#xff0c;长度&#xff0c;项目大小&#xff0c;两个链表&#xff1b; 写队列的时候&#xff0c;写指针指向头部&#xff0c;写进去之后&#xff0c;itemsize&#xff0c;移…

MySQL中的日志

错误日志 错误日志是MySQL中最重要的日志之一默认是开启的&#xff0c;它记录了MySQL启动和停止时&#xff0c;以及入伍再运行过程中发发生任何严重错误时的相关信息&#xff0c;当数据库出现任何故障无法正常运行时可以查看此日志。 二进制日志 二进制日志记录了所有的DDL语…

http跨域网络请求中的CORS(跨源资源共享) 那些事 -- HTTP跨域请求, chrome插件跨域请求使用详解, origin格式,origin通配符等

在我们进行网络应用开发的时候&#xff0c;如果用到了跨域网络请求&#xff0c;则不可避免的就会遇到http跨域网络请求 CORS的问题&#xff0c;今天就和大家来聊聊跨域网络请求中的CORS的那些事。 跨源资源共享&#xff08;CORS&#xff09; CORS 是一种基于 HTTP 头的机制&a…

ISO26262-MBD-静态验证在V左的布局考量

一、ISO26262-MBD-静态验证的迷惑 模型的开发方法&#xff08;Model-Based Design&#xff0c;MBD&#xff09;在汽车行业嵌入式软件开发中扮演着重要的角色&#xff0c;功能安全ISO26262要求对我们搭建的模型进行规范检查。合规检查我们可以借助第三方工具来实现静态检查&…

基于Django的图书管理系统【万能模板框架可调整增加】

文章目录 有需要本项目的代码或文档以及全部资源&#xff0c;或者部署调试可以私信博主框架介绍项目框架每文一语 有需要本项目的代码或文档以及全部资源&#xff0c;或者部署调试可以私信博主 框架介绍 Django是一个用于快速开发Web应用程序的高级Python开源框架。它遵循MVC…

去除富文本框的边框

<style lang"scss" scoped>::v-deep .textareaDeep .el-textarea__inner {border: none !important;box-shadow: none !important;padding: 0px; }</style> //添加类名 <el-inputclass"textareaDeep"type"textarea":rows"…

Go语言实现依赖注入

文章目录 前言依赖注入是什么依赖注入的好处是什么结构图应用程序上下文接口上下文管理器暴露的功能使用示例最后 前言 你好&#xff0c;我是醉墨居士&#xff0c;欢迎来到我的博客&#xff0c;今天带领大伙使用Go语言实现依赖自动注入&#xff0c;我们不会使用其它的第三方库…

域控安全:多种方式提取ntds.dit

ntdsutils.exe提取ntds.dit vssadmin提取ntds.dit vssown提取ntds.dit IFM ntds.dit: ntds.dit为AD的数据库&#xff0c;内容有域用户、域组、用户hash等信息&#xff0c;域控上的ntds.dit只有可以登录到域控的用户&#xff08;如域管用户、DC本地管理员用户&#xff09;可以…

HexView 刷写文件脚本处理工具-基本功能介绍(一)-基本界面

HexView主要可以显示不同文件格式的内容&#xff0c;包括Intel-HEX、Motorola S-record二进制文件或其他特定汽车制造商的文件格式。此外&#xff0c;它还可以执行多种数据处理操作&#xff0c;如校验和计算、签名生成、数据加密/解密或压缩/解压缩&#xff0c;甚至重新排列文件…

ubuntu创建txt

点击模版 右键 输入下面代码 sudo gedit txt文档.txt 然后就可以右键新建文本文件了 解开权限 sudo chmod -R 777 /home/sjxy/CQ

探索下一代互联网协议:IPv6的前景与优势

探索下一代互联网协议&#xff1a;IPv6的前景与优势 文章目录 探索下一代互联网协议&#xff1a;IPv6的前景与优势**IPv6 的特点****IPv6的基本首部****IPv6的地址****总结** 互联网的核心协议&#xff1a;从IPv4到IPv6 互联网的核心协议IP&#xff08;Internet Protocol&#…

【Nacos无压力源码领读】(一) Nacos 服务注册与订阅原理

本文将详细介绍 Nacos 客户端在启动时进行自动注册原理, 以及Nacos服务器是如何处理客户端的注册与订阅请求的; 本文会附带源码解读, 但不会死抠每一行代码, 主要是梳理整个流程, 过程中的关键步骤, 都会由思维导图的形式展现出来; 如果在阅读过程中对文中提到的 SpringBoot …

Comsol 弧形声学换能器声聚焦仿真

弧形声学换能器声聚焦是指将声波能量集中在弧形声学换能器的特定区域内&#xff0c;以实现更强的声场强度和分辨率。声聚焦在许多应用中非常有用&#xff0c;包括医学超声成像、声纳、声波聚焦破碎等领域。 弧形声学换能器的设计和优化可以通过以下几个因素来实现声聚焦&#…

【网站项目】SpringBoot743中珠商城仓库管理系统

&#x1f64a;作者简介&#xff1a;拥有多年开发工作经验&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的项目或者毕业设计。 代码可以私聊博主获取。&#x1f339;赠送计算机毕业设计600个选题excel文件&#xff0c;帮助大学选题。赠送开题报告模板&#xff…

A股破新低后震荡盘整,后市如何演绎?

今天的A股&#xff0c;让人按耐不住了&#xff0c;你们知道是为什么吗&#xff1f;盘面上出现1个重要信号&#xff0c;一起来看看&#xff1a; 1、今天两市冲高回落&#xff0c;主力压盘、故意洗筹码的意图就更加明显了&#xff0c;一定要拿住&#xff01;地板上卖出的&#xf…

《刚刚问世》系列初窥篇-Java+Playwright自动化测试-4-启动浏览器-基于Maven(详细教程)

1.简介 上一篇文章&#xff0c;宏哥已经在搭建的java项目环境中添加jar包实践了如何启动浏览器&#xff0c;今天就在基于maven项目的环境中给小伙伴们或者童鞋们演示一下如何启动浏览器。 2.eclipse中新建maven项目 1.依次点击eclipse的file - new - other &#xff0c;如下…