为了账号安全,请及时绑定邮箱和手机立即绑定

Java Spring Boot 项目中的存储过程返回 null 作为输出

Java Spring Boot 项目中的存储过程返回 null 作为输出

Qyouu 2023-06-08 14:14:17
我在 Spring Boot 项目中使用存储过程并尝试获取输出值,但在我的项目中它总是返回 null。但是,如果我通过 HeidiSQL 调用该过程,它会起作用并为我提供正确的值。所以它必须用我的java代码做一些事情。我调试了受影响的方法,但找不到返回 null 的原因。我已经尝试查找其他帖子,但找不到与我的特定问题相匹配的内容。这是我尝试使用存储过程的方法:公司资源服务实现@Servicepublic class CompanyResourceServiceImpl implements CompanyResourceService {@PersistenceContext    private EntityManager entityManager;...private int getMetalResourceByPlayerId(int theId) {        StoredProcedureQuery theQuery = entityManager.createStoredProcedureQuery("getAllMetalFromCompaniesByPlayerId");        theQuery.registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN);        theQuery.registerStoredProcedureParameter(2, BigDecimal.class, ParameterMode.OUT);        theQuery.setParameter(1, theId);        theQuery.execute();        BigDecimal outAmount = (BigDecimal) theQuery.getOutputParameterValue(2);        return outAmount.intValue();    }...}以下是存储过程:getAllMetalFromCompaniesByPlayerIdCREATE DEFINER=`root`@`localhost` PROCEDURE `getAllMetalFromCompaniesByPlayerId`(    IN `playerId` INT,    OUT `metalSum` DECIMAL(19,2))LANGUAGE SQLNOT DETERMINISTICCONTAINS SQLSQL SECURITY DEFINERCOMMENT ''BEGINSELECT sum(cr.amount) as metalSumFROM company_resource crJOIN company c ON (c.id = cr.company_id) WHERE c.player_id = playerId and cr.resource_id = 1;END我的目标是获取输出值并在@Scheduled方法中使用它。正如我所说,在 HeidiSQL 中,存储过程有效。
查看完整描述

1 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

经过几个小时的尝试,我找到了一种让它工作的方法。


首先我添加@NamedStoredProcedureQuery到我的CompanyResource实体类:


CompanyResource.java


@Entity

@Table(name = "company_resource")

@NamedStoredProcedureQueries({

        @NamedStoredProcedureQuery(name = "getAllMetalFromCompaniesByPlayerId",

                                    procedureName = "getAllMetalFromCompaniesByPlayerId",

                                    parameters = {

                                        @StoredProcedureParameter(mode = ParameterMode.IN, name = "playerId", type = Integer.class),

                                        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "metalSum", type = BigDecimal.class)

                                    })

})

@IdClass(CompanyResourcePK.class)

public class CompanyResource {

...

}

然后我改变了我的getMetalResourceByPlayerId()方法CompanyResourceServiceImpl如下:


CompanyResourceServiceImpl.java


@Service

public class CompanyResourceServiceImpl implements CompanyResourceService {


@PersistenceContext

    private EntityManager entityManager;


...


private int getMetalResourceByPlayerId(int theId) {


        StoredProcedureQuery theQuery = entityManager.createNamedStoredProcedureQuery("getAllMetalFromCompaniesByPlayerId");


        theQuery.setParameter("Param1", theId);


        BigDecimal outAmount = (BigDecimal) theQuery.getSingleResult();


        return  outAmount.intValue();

    }


...


}


查看完整回答
反对 回复 2023-06-08
  • 1 回答
  • 0 关注
  • 119 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信