Spring:带动态参数的SimpleJdbcCall
Spring: SimpleJdbcCall with dynamic parameters
我有一个使用 SimpleJdbcCall 调用 postgres 函数的 dao:
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。
我有一个使用 SimpleJdbcCall 调用 postgres 函数的 dao:
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。