【发布时间】:2018-01-05 21:31:29
【问题描述】:
我正在尝试在使用 Spring(非引导)配置的 JUnit 测试中创建不带参数的存储过程。我从文档和示例中了解到,我必须为静态方法设置别名才能响应存储过程调用。但是,我找不到关于它如何与out parameters 一起使用的信息。静态方法的方法签名中有什么内容?
package com.example;
import static java.sql.Types.INTEGER;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.h2.tools.SimpleResultSet;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { H2tests.TestConfiguration.class })
public class H2tests {
@Autowired
DataSource dataSource;
public static ResultSet executeMyProc(int a) {
return new SimpleResultSet();
}
// what should the type of the second parameter be?
public static ResultSet executeMyProc(int a, int b) {
return new SimpleResultSet();
}
@Test // passes
public void worksWithInParam() throws SQLException {
final CallableStatement stmt = dataSource.getConnection().prepareCall("CALL my_proc(?)");
stmt.setInt(1, 44);
stmt.executeQuery();
}
@Test // fails
public void worksWithOutParam() throws SQLException {
final CallableStatement stmt = dataSource.getConnection().prepareCall("CALL my_proc(?, ?)");
stmt.setInt(1, 999);
stmt.registerOutParameter(2, INTEGER);
stmt.executeQuery();
}
public static class TestConfiguration {
@Bean
public DataSource dataSource() throws SQLException {
final EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
final EmbeddedDatabase db = builder //@formatter:off
.setType(EmbeddedDatabaseType.H2)
.setName("MODE=MySQL")
.build(); //@formatter:on
db.getConnection().prepareStatement("CREATE ALIAS my_proc FOR \"com.example.H2tests.executeMyProc\"")
.execute();
return db;
}
}
}
【问题讨论】:
-
我的回答可能有点离题,但在我们的项目中,我们放弃了 H2 以支持 TestContainers 库 (github.com/testcontainers/testcontainers-java)。该库在 Docker 容器中运行实际数据库(在您的情况下为 MySQL),您可以访问您需要的所有功能,而不必为 H2 限制寻找解决方法。因此,如果您有机会使用 Docker,我肯定会建议您看看 TestContainers。
-
感谢您的意见。不幸的是,我对构建过程没有足够的影响力来实现这一点。我需要使用 H2 或类似的东西,将模拟数据库保留在 Java 进程中。
-
现在我正在考虑它 - 是什么阻止您使用 SQL 定义存储过程?
-
Igor,我认为 H2 中也不存在这样的功能。在 Java 中实现它们似乎是在 H2 中存储过程的预期方式。
标签: java stored-procedures h2 out-parameters