第10讲. SpringBoot事物管理

1.新建一个数据库,db_bank.编码集:utf8


2,复制9.3的项目,重命名为SpringDataTransaction,这一步主要是为了不影响之前的代码,  更新一下项目:右键项目名->maven->Update Projec...,


3,新建一个实体,Account,指定表明为:t_account  加上注解。float不需要长度注解, 修改配置文件,启动项目,查看是否生成表,如果生成了表t_account 则成功。为了不影响代码 删除 t_book 表,。

package com.cruise.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_account")
public class Account {
    
    @Id
    @GeneratedValue
    private Integer id;
    @Column(length=50)
    private String userName;

    private float balance;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public float getBalance() {
        return balance;
    }
    public void setBalance(float balance) {
        this.balance = balance;
    }
}

配置文件:

server:
  port: 8888
  
  context-path: /
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_bank
    password: root
    username: root
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

4, 新建一个AccountDao,继承接口JpaRepository Integer 为主键的类型

package com.cruise.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.cruise.entity.Account;

public interface AccountDao extends JpaRepository{

}

5, 新建com.cruise.service包,新建AccountService接口,写一个转账接口

package com.cruise.service;

public interface AccountService {

    public void transferAccounts(int fromUser,int toUser,float account);
}

6,写Service实现类,AccountServiceImpl,实现AccountService接口,加注解,注入dao接口,

从 fromAccount 转账到 toAccount ,fromAccount 减少account , toAccount增加 account

package com.cruise.service.impl;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.cruise.dao.AccountDao;
import com.cruise.entity.Account;
import com.cruise.service.AccountService;

/**
 * 
 * @author pengc
 *
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountDao accountDao;
    
    public void transferAccounts(int fromUser, int toUser, float account) {
        
        Account fromAccount = accountDao.findById(fromUser).get();
        fromAccount.setBalance(fromAccount.getBalance()-account);
        accountDao.save(fromAccount);
        
        Account toAccount = accountDao.findById(toUser).get();
        toAccount.setBalance(toAccount.getBalance()+account);
        accountDao.save(toAccount);
    }
}

7,AccountController ,使用RestController注解,注入service接口。删除book相关的代码,以防有影响。

使用try{...}catch(Exception e){...}来判断是否执行成功,如果成功返回OK,如果失败返回NO

package com.cruise.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cruise.service.AccountService;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Resource
    private AccountService accountService;
    
    @RequestMapping("/transfer")
    public String transferAccount(){
        try{
            accountService.transferAccounts(1, 2, 200);
            return "OK";
        }catch(Exception e){
            return "NO";
        }
    }
}

8,测试,

10._SpringBoot事物管理


9,在service中设置异常代码,测试,


10,在service实现类上添加事物注解,测试,注意导哪个包

猜你喜欢

转载自blog.csdn.net/u010393325/article/details/83957909