2017-04-27 73 views
2

我有一个使用SimpleJdbcCall调用Postgres的功能的道:春:SimpleJdbcCall时动态参数

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来调用不同的函数只传递函数名?

回答

1

I figured it out

如果您使用的数据库不是 Spring支持的数据库,则需要显式声明。目前Spring支持针对以下数据库的存储过程调用的元数据查询 :Apache Derby, DB2,MySQL,Microsoft SQL Server,Oracle和Sybase。我们还支持 对MySQL,Microsoft SQL Server, 和Oracle的存储函数的元数据查找。

而且,omg,我使用Postgres。