我有一个使用 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 来仅传递函数名称来调用不同的函数?
最佳答案
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/