首页 > 其他分享 >17_Spring_事务环境搭建

17_Spring_事务环境搭建

时间:2023-03-04 10:22:05浏览次数:50  
标签:17 int Spring public money import com msb 搭建

 通过张三给李四转账案例演示事务的控制

1 数据库中准备表格

applicationContext.xml
jdbc.properties
见上节课


2 项目中准备实体类
 

package com.msb.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
 * @Author: Ma HaiYang
 * @Description: MircoMessage:Mark_7001
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Account implements Serializable {
    private Integer id;
    private String name;
    private Integer money;
}

 

3 准备DAO层,创建一个根据id修改money的方法

 

package com.msb.dao;
/**
 * @Author: Ma HaiYang
 * @Description: MircoMessage:Mark_7001
 */
public interface AccountDao {
    int transMoney(int id,int money);
}

 

package com.msb.dao.impl;
import com.msb.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
/**
 * @Author: Ma HaiYang
 * @Description: MircoMessage:Mark_7001
 */
@Repository
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int transMoney(int id, int money) {
        String sql ="update account set money =money +? where id =?";
        return jdbcTemplate.update(sql,money,id);
    }
}

 


4 准备Service,创建一个转账的业务方法

package com.msb.service;
/**
 * @Author: Ma HaiYang
 * @Description: MircoMessage:Mark_7001
 */
public interface AccountService {
    int transMoney(int from ,int to,int money);
}

 

package com.msb.service.impl;
import com.msb.dao.AccountDao;
import com.msb.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @Author: Ma HaiYang
 * @Description: MircoMessage:Mark_7001
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;
    @Override
    public int transMoney(int from, int to, int money) {
        int rows=0;
        rows+=accountDao.transMoney(from, 0 - money);       
        rows+=accountDao.transMoney(to, money);        
        return rows;
    }
}

 


5 测试代码,测试转账
 

package com.msb.test;
import com.msb.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @Author: Ma HaiYang
 * @Description: MircoMessage:Mark_7001
 */
public class TestTx {
    @Test()
    public void testTransaction(){
        ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = context.getBean(AccountService.class);
        int rows = accountService.transMoney(1, 2, 100);
        System.out.println(rows);
    }
    
}


标签:17,int,Spring,public,money,import,com,msb,搭建
From: https://www.cnblogs.com/89564f/p/17177739.html

相关文章

  • 15_Spring_JDBCTemplate批操作
    ​ 一次连接,操作表格里的多条数据,就是批量操作1批量增加2批量修改3批量删除实体类 packagecom.msb.pojo;importlombok.AllArgsConstructor;importlomb......
  • 15_Spring_JDBCTemplate批操作
    ​ 一次连接,操作表格里的多条数据,就是批量操作1批量增加2批量修改3批量删除实体类 packagecom.msb.pojo;importlombok.AllArgsConstructor;importlomb......
  • 16_Spring_事务回顾
    1.  事务的概念事务(Transaction)指的是一个操作序列,该操作序列中的多个操作要么都做,要么都不做,是一个不可分割的工作单位,是数据库环境中的逻辑工作单位,由DBMS中的事务......
  • 16_Spring_事务回顾
    1.  事务的概念事务(Transaction)指的是一个操作序列,该操作序列中的多个操作要么都做,要么都不做,是一个不可分割的工作单位,是数据库环境中的逻辑工作单位,由DBMS中的事务......
  • java-spring
    1、介绍2、架构图......
  • Spring Boot应用如何快速接入Prometheus监控
    1.Micrometer简介Micrometer为Java平台上的性能数据收集提供了一个通用的API,它提供了多种度量指标类型(Timers、Guauges、Counters等),同时支持接入不同的监控系统,例如Influ......
  • prometheus + grafana对 springboot 项目进行监控
    1.prometheus接入springbootprometheus安装后,在安装目录有一个默认的配置文件prometheus.yml#myglobalconfigglobal:scrape_interval:15s#Setthescrapeinte......
  • docker+jenkins搭建allure-commadline
    安装allure命令行:1、下载将allure-commandline-2.20.1.tgz将安装包cp到Jenkins挂载目录下将文件下载下来:https://repo.maven.apache.org/maven2/io/qameta/allure/al......
  • 手把手教你搭建Windows 搭建Prometheus + Grafana + Jmeter可视化监控平台
    下载安装包Prometheuswindows_exporterGrafana下载地址:https://share.weiyun.com/D9sdiWoC工作原理Exporter监控工具,获取数据Prometheus普罗米修斯时序数据库......
  • Spring Boot如何自定义监控指标
    1.创建项目pom.xml引入相关依赖<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="htt......