spring boot学习第十二篇:mybatis框架中调用存储过程控制事务性

1、MySQL方面,已经准备好了存储过程,参考:MYSQL存储过程(含入参、出参)-CSDN博客

2、pom.xml文件内容如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.hmblogs
    hmblogs
    0.0.1-SNAPSHOT
    hmblogs
    hmblogs
    
        8
        1.2.8
        1.16
        7.9.2
    
    
        
        
            com.alibaba
            druid-spring-boot-starter
            ${druid.version}
        

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.3.1
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
        

        
            org.projectlombok
            lombok
            true
        

        
        
            mysql
            mysql-connector-java
            runtime
        

        
        
            org.projectlombok
            lombok
            true
        

        
        
            org.bgee.log4jdbc-log4j2
            log4jdbc-log4j2-jdbc4.1
            ${log4jdbc.version}
        

        
            com.alibaba
            fastjson
            1.2.9
        

        
            redis.clients
            jedis
        

        
            org.apache.kafka
            kafka-clients

        

        
            org.springframework.kafka
            spring-kafka

        

        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            ${es.version}
            
                
                    org.elasticsearch
                    elasticsearch
                
                
                    org.elasticsearch.client
                    elasticsearch-rest-client
                
            
        

        
        
            org.elasticsearch
            elasticsearch
            ${es.version}
        
        
            org.elasticsearch.client
            elasticsearch-rest-client
            ${es.version}
        
        
            junit
            junit
        

        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        

        
            org.springframework.boot
            spring-boot-starter-security
        

        
            org.springframework.security.oauth.boot
            spring-security-oauth2-autoconfigure
            2.4.0
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


3、application.yml文件内容如下:

server:
  port: 8081
  servlet.context-path: /

#配置数据源
spring:
  datasource:
    druid:
      db-type: com.alibaba.druid.pool.DruidDataSource
      driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
      url: jdbc:log4jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
      username: ${DB_USER:root}
      password: ${DB_PWD:demo}
  redis:
    host: localhost
    port: 6379
    password: demo
    database: 10
  data:
    mongodb:
      host: 43.138.0.199
      port: 27017
      username: hmblogs
      password: demo
      database: hmblogs
      authentication-database: admin

es:
  host: 43.138.0.199
  port: 9200
  scheme: http
  user: elastic
  password: demo

4、BackendApplication文件内容如下:

package com.hmblogs.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(BackendApplication.class, args);
    }

}

5、StockMapper.xml文件内容如下:




    
        
        
    

    
        id, quantity
    

    

    

    
        update t_stock set quantity=quantity-1 where id=#{id}
    

    

spring boot学习第十二篇:mybatis框架中调用存储过程控制事务性_第1张图片

6、StockMapper文件内容如下:

package com.hmblogs.backend.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hmblogs.backend.entity.Stock;
import org.apache.ibatis.annotations.Mapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mapper
public interface StockMapper extends BaseMapper {
    List findAll();

    Stock findById(Stock stock);

    Integer updateStockById(Stock stock);

    Map invokeStockProdudure(Map param);
}

7、Stock文件内容如下:

package com.hmblogs.backend.entity;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("t_stock")
public class Stock {

    @TableId(value="id", type = IdType.AUTO)
    private Integer id;

    private Integer quantity;

}

8、StockServiceImpl代码如下:

package com.hmblogs.backend.service;

import com.hmblogs.backend.dao.StockMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class StockServiceImpl {

    @Autowired
    private StockMapper stockMapper;

    public void operateProcedure(){
        HashMap param = new HashMap<>();
        param.put("id1","7000");
        param.put("quantity1","15");
        param.put("id2","7001");
        param.put("quantity2","18");
        Map result = stockMapper.invokeStockProdudure(param);
        log.info("result:"+result);
    }
}

9、测试类ProcedureTest内容如下:

package com.hmblogs.backend.util;

import com.hmblogs.backend.service.StockServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;

@Slf4j
//@Component
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProcedureTest {

    @Autowired
    private StockServiceImpl stockServiceImpl;

    @Test
    public void testProcedure() {
        // 请求参数
        stockServiceImpl.operateProcedure();
    }
}

执行该方法,

spring boot学习第十二篇:mybatis框架中调用存储过程控制事务性_第2张图片

查看数据:

spring boot学习第十二篇:mybatis框架中调用存储过程控制事务性_第3张图片

10、StockServiceImpl内代码改动一下,

spring boot学习第十二篇:mybatis框架中调用存储过程控制事务性_第4张图片

执行测试方法,结果如下:

spring boot学习第十二篇:mybatis框架中调用存储过程控制事务性_第5张图片

查看数据,没有id为8000的记录

spring boot学习第十二篇:mybatis框架中调用存储过程控制事务性_第6张图片

你可能感兴趣的:(Spring,Boot,spring,boot,mybatis)