如何在使用SpringBoot REST API的 React应用中调用名为“updatebalance”的无参数MySQL存储过程?
我在MySQL 8.0中有一个存储过程,可以更新Checkbook表中的运行余额字段。我已经在SpringBoot应用程序和React前端中引用了它。出于某种原因,它并未从我的React前端执行。以下是MYSQL存储过程、SpringBoot Repository与 Controller,最后是React Service与调用例程。 我可以添加记录、删除记录、编辑记录,只是存储例程并没有从React前端被调用,好像引用不正确。 我错过了什么?
MYSQL:
CREATE DEFINER=`root`@`localhost` PROCEDURE `updatebalance`()
BEGIN
UPDATE checkbook cb
JOIN (select
checkbookid,
SUM(amount) OVER(
ORDER BY checkbookid
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS balance
FROM checkbook
)r
ON cb.checkbookid = r.checkbookid
SET cb.balance = r.balance;
END
SPRINGBOOT REPOSITORY ENTRIES:
@Repository public interface CheckbookRepository extends JpaRepository<Checkbook, Long>{
public List<Checkbook> findAll();
public @Procedure(procedureName="updatebalance")
void updateBalance();
SPRING BOOT CONTROLLER ENTRY:
@PostMapping("/updatebalance")
public void updateBalance (){
checkbookRepository.updateBalance();
}
REACT SERVICES ENTRY:
updateBalance(){
return axios.post(myConstants.USER_API_BASE_URL + "updatebalance");
}
REACT CALLING CODE:
await CheckbookService.updateBalance
mysqlspring-bootreact-nativestored-proceduresaxios
解决方案
your React code is not calling the function, you wrote await CheckbookService.updateBalance without ()
你写了 await CheckbookService.updateBalance 而没有 (),因此你的React代码并没有调用该函数。
add parentheses so it actually executes: await CheckbookService.updateBalance();
给它加上括号,这样它才会真正执行:await CheckbookService.updateBalance();
then the POST will hit your Spring Boot endpoint and run the stored procedure
然后POST请求就会命中你的Spring Boot端点并执行存储过程
everything else (Spring Boot repo/controller and MySQL procedure) is fine
其他部分(Spring Boot的仓库/控制器和MySQL存储过程)都没问题