函数返回(MySQL + Spring)
我在使用通过使用 Spring JdbcTemplate 类的代码在 MySQL 中创建的函数时遇到问题。
当我直接在工作台中测试该函数是否正常工作时,根据过去的条目返回 true 或 false。然而,当我尝试通过 Java 代码获取结果时,总是返回 false,这让我相信这是某种类型转换失败。
为什么变量“结果”总是假?
函数
CREATE DEFINER=`root`@`localhost` 函数“COMPARA_QTDE_EMPRESA”(AG INT、P INT、ANO INT、AINSERIR INT) 返回tinyint(1) 确定性 开始 声明 TOTAL_EMPRESA_PESQUISA INT; 声明 TOTAL_EMPRESA_AGENTE INT; 选择 IF(COUNT(*), EP.TOTAL, 0) INTO TOTAL_EMPRESA_PESQUISA 来自 CTRLPRODUCAO.EMPRESA_PESQUISA EP 其中 EP.AGENCIA = AG 和 EP.Pesquisa = P 且 EP.ANO = ANO; 选择 IFNULL(SUM(EA.TOTAL), 0) INTO TOTAL_EMPRESA_AGENTE 来自 CTRLPRODUCAO.EMPRESA_AGENTE EA 其中 EA.AGENCIA = AG 且 EA.PESQUISA = P AND EA.ANO = ANO; 返回 如果(总计_EMPRESA_PESQUISA = 0 , 错误的 ,(TOTAL_EMPRESA_PESQUISA < (TOTAL_EMPRESA_AGENTE) + AINSERIR));
END
Java 代码
<前><代码>(...) 对象[] args = 新对象[4]; int[] tipos = 新 int[4]; args[0] = ag.getId(); Tipos[0] = Types.INTEGER; args[1] = p.getId(); Tipos[1] = Types.INTEGER; args[2] = ano.getAno(); Tipos[2] = Types.INTEGER; args[3] =total.getTotal(); Tipos[3] = Types.INTEGER; 布尔结果 = (Boolean)getJdbcTemplate().queryForObject("SELECT COMPARA_QTDE_EMPRESA (?, ?, ?, ?)"), args, Tipos, Boolean.class);
I'm having problem in using a function created in MySQL through code that uses the class Spring JdbcTemplate.
When I test the function directly in the Workbench works correctly, returning true or false according to past entries. However, when I try to get the result via Java code always returns false, which leads me to believe this is some type conversion failed.
Why is variable 'result' always false?
function
CREATE DEFINER=`root`@`localhost` FUNCTION `COMPARA_QTDE_EMPRESA`(AG INT, P INT, ANO INT, AINSERIR INT) RETURNS tinyint(1) DETERMINISTIC BEGIN DECLARE TOTAL_EMPRESA_PESQUISA INT; DECLARE TOTAL_EMPRESA_AGENTE INT; SELECT IF(COUNT(*), EP.TOTAL, 0) INTO TOTAL_EMPRESA_PESQUISA FROM CTRLPRODUCAO.EMPRESA_PESQUISA EP WHERE EP.AGENCIA = AG AND EP.PESQUISA = P AND EP.ANO = ANO; SELECT IFNULL(SUM(EA.TOTAL), 0) INTO TOTAL_EMPRESA_AGENTE FROM CTRLPRODUCAO.EMPRESA_AGENTE EA WHERE EA.AGENCIA = AG AND EA.PESQUISA = P AND EA.ANO = ANO; RETURN IF(TOTAL_EMPRESA_PESQUISA = 0 , FALSE ,(TOTAL_EMPRESA_PESQUISA < (TOTAL_EMPRESA_AGENTE) + AINSERIR));
END
Java code
(...) Object[] args = new Object[4]; int[] tipos = new int[4]; args[0] = ag.getId(); tipos[0] = Types.INTEGER; args[1] = p.getId(); tipos[1] = Types.INTEGER; args[2] = ano.getAno(); tipos[2] = Types.INTEGER; args[3] = total.getTotal(); tipos[3] = Types.INTEGER; boolean result = (Boolean)getJdbcTemplate().queryForObject("SELECT COMPARA_QTDE_EMPRESA (?, ?, ?, ?)"), args, tipos, Boolean.class);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MySQL
boolean
是tinyint(1)
的别名。这意味着您使用的是正确的类型。
请注意,1 = true,0 = false。
因此,如果您想在 Java 代码中获得结果,您只需针对
0
测试返回值即可。将java代码更改为:
MySQL
boolean
is an alias fortinyint(1)
.This means that you are using the correct type.
Note that 1 = true, 0 = false.
So if you want to get the result in your java code, all you have to do this test the return value against
0
.Change the java code to: