1、MySQL方面,已经准备好了存储过程,参考:MYSQL存储过程(含入参、出参)-CSDN博客
2、pom.xml文件内容如下:
4.0.0org.springframework.bootspring-boot-starter-parent2.6.4 com.hmblogshmblogs0.0.1-SNAPSHOThmblogshmblogs81.2.81.167.9.2com.alibabadruid-spring-boot-starter${druid.version}org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-testtestcom.baomidoumybatis-plus-boot-starter3.5.3.1org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testorg.projectlomboklomboktruemysqlmysql-connector-javaruntimeorg.projectlomboklomboktrueorg.bgee.log4jdbc-log4j2log4jdbc-log4j2-jdbc4.1${log4jdbc.version}com.alibabafastjson1.2.9redis.clientsjedisorg.apache.kafkakafka-clientsorg.springframework.kafkaspring-kafkaorg.elasticsearch.clientelasticsearch-rest-high-level-client${es.version}org.elasticsearchelasticsearchorg.elasticsearch.clientelasticsearch-rest-clientorg.elasticsearchelasticsearch${es.version}org.elasticsearch.clientelasticsearch-rest-client${es.version}junitjunitorg.springframework.bootspring-boot-starter-data-mongodborg.springframework.bootspring-boot-starter-securityorg.springframework.security.oauth.bootspring-security-oauth2-autoconfigure2.4.0org.springframework.bootspring-boot-maven-plugin
3、application.yml文件内容如下:
server:port: 8081servlet.context-path: /#配置数据源spring:datasource:druid:db-type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpyurl: jdbc:log4jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=falseusername: ${DB_USER:root}password: ${DB_PWD:demo}redis:host: localhostport: 6379password: demodatabase: 10data:mongodb:host: 43.138.0.199port: 27017username: hmblogspassword: demodatabase: hmblogsauthentication-database: admines:host: 43.138.0.199port: 9200scheme: httpuser: elasticpassword: demo
4、BackendApplication文件内容如下:
package com.hmblogs.backend;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class BackendApplication {public static void main(String[] args) {SpringApplication.run(BackendApplication.class, args);}}
5、StockMapper.xml文件内容如下:
id, quantityselectfrom t_stockselectfrom t_stockwhere id=#{id}update t_stock set quantity=quantity-1 where id=#{id}CALL stock_produdure(#{id1,mode=IN,jdbcType=INTEGER},#{quantity1,mode=IN,jdbcType=INTEGER},#{id2,mode=IN,jdbcType=INTEGER},#{quantity2,mode=IN,jdbcType=INTEGER},#{result,mode=OUT,jdbcType=INTEGER});
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;@Mapperpublic 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@Servicepublic class StockServiceImpl {@Autowiredprivate 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)@SpringBootTestpublic class ProcedureTest {@Autowiredprivate StockServiceImpl stockServiceImpl;@Testpublic void testProcedure() {// 请求参数stockServiceImpl.operateProcedure();}}
执行该方法,
查看数据:
10、StockServiceImpl内代码改动一下,
执行测试方法,结果如下:
查看数据,没有id为8000的记录