java - Spring:具有动态参数的 SimpleJdbcCall

标签 java spring spring-jdbc

我有一个使用 SimpleJdbcCall 的 dao调用 postgres 函数:

public final class AuthDAO extends UntypedActor {

    private final ActorRef manager;

    private final JdbcTemplate jdbcTemplate;

    private final SimpleJdbcCall jdbcCall;

    public AuthDAO(ActorRef manager) {
        this.manager = manager;
        jdbcTemplate = DBConfig.jdbcTemplate();
        jdbcCall = new SimpleJdbcCall(jdbcTemplate)
                .withSchemaName("auth")
                .withCatalogName("public")
                .withoutProcedureColumnMetaDataAccess();
    }

    public static Props create(ActorRef manager) {
        return Props.create(AuthDAO.class, manager);
    }

    @Override
    public void onReceive(Object o) throws Throwable {
        if (o instanceof DBMessage) {

            DBMessage message = (DBMessage) o;
            jdbcCall.declareParameters(new SqlParameter("login", Types.VARCHAR));
            Map<String, Object> response = jdbcCall
                    .withProcedureName(message.getProcedure())
                    .execute(message.getParams());

            System.out.println(response.toString());
        }
    }
}

但现在我必须通过调用 jdbcCall.declareParameters() 来显式声明函数的参数,否则代码将无法工作。

那么有没有一种方法可以动态检测函数参数的名称和类型,并使用这样的 dao 来仅传递函数名称来调用不同的函数?

最佳答案

figured it out .

Explicit declarations are necessary if the database you use is not a Spring-supported database. Currently Spring supports metadata lookup of stored procedure calls for the following databases: Apache Derby, DB2, MySQL, Microsoft SQL Server, Oracle, and Sybase. We also support metadata lookup of stored functions for MySQL, Microsoft SQL Server, and Oracle.

而且,天哪,我使用 Postgres。

关于java - Spring:具有动态参数的 SimpleJdbcCall,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43656566/

相关文章:

spring - 如何从 spring webapp 中的命令行覆盖属性

java - Spring Boot 应用程序中日志记录概念的行数

java - Spring SimpleJdbcCall 返回错误值

spring-boot - HikariCP 中的 initializationFailTimeout 属性

java - 在 spring jdbctemplate 中维护事务

java - 在 liberty 中动态配置 dev 和 prod 数据源以根据环境加载

java - 如何使Map的特化变得不可修改?

java - 在ManyToMany表, hibernate , Spring 中保存数据的问题

java - nifi自定义处理器中的 session 未关闭异常

java - 处理数据库异常