如何从java调用pl / pgsql

时间:2017-04-06 11:39:35

标签: java postgresql spring-boot plpgsql

我的PL / pgSQL函数运行正常。我测试了它

    CREATE OR REPLACE FUNCTION topAlerte()
          RETURNS void AS
          $$

          DECLARE

            max_var_risk varchar(70);
            max_mvar_risk varchar(70);
            max_incvar_risk varchar(70);
            max_cvar_risk varchar(70);
            amount varchar(70);
            delta varchar(70);
            net_exposure varchar(70);

          BEGIN

            truncate tops;

            select names from risk where risk.var_mc_risk =(select                max(risk.var_mc_risk) from risk) into max_var_risk ;
            select names from risk where risk.mvar_mc_risk =(select max(risk.mvar_mc_risk) from risk) into max_mvar_risk ;
            select names from risk where risk.inc_var_mc_risk =(select max(risk.inc_var_mc_risk) from risk) into max_incvar_risk ;
            select names from risk where risk.cvar_mc_risk =(select max(risk.cvar_mc_risk) from risk) into max_cvar_risk ;
            select names from risk where risk.amount =(select max(risk.amount) from risk) into amount ;
            select names from risk where risk.delta =(select max(risk.delta) from risk) into delta;
            select names from risk where risk.net_exposure =(select max(risk.net_exposure) from risk) into net_exposure  ;

            INSERT INTO tops VALUES 
              (max_var_risk, max_mvar_risk, max_incvar_risk,   max_cvar_risk,amount,delta,net_exposure);

          END ;

          $$

          LANGUAGE 'plpgsql';

我想从春季启动调用它,这是我的DAO:

public interface TopRepository {
    public List getTopsAlert()throws Throwable;
}

这是我的服务(接口实现):

public class TopAlertMetier implements TopRepository{

        @Override
        public List getTopsAlert() throws Throwable {
        List<String> myList= new ArrayList<>();
        Class.forName("org.postgresql.Driver");
        Connection connect= null;
        connect = (Connection)           DriverManager.getConnection("jdbc:postgresql://localhost:5432/bourse","postgres","123456");
        java.sql.CallableStatement proc =  connect.prepareCall("{topAlerte()}");
        proc.registerOutParameter(1, java.sql.Types.VARCHAR);
        proc.executeQuery(); 
        ResultSet results = (ResultSet) proc.getObject(1);
        myList.add(results.getString(0));
        myList.add(results.getString(1));
        myList.add(results.getString(2));
        myList.add(results.getString(3));
        myList.add(results.getString(4));
        myList.add(results.getString(5));
        myList.add(results.getString(6));

        return myList;
  }

和我的控制器:

  @RestController
  public class topController {

      @Autowired
      TopRepository topRepository;

      @RequestMapping(value="/alert",method = RequestMethod.GET)
      public  @ResponseBody List alert() throws Throwable{
          return topRepository.getTopsAlert();
      }

列表显示了最大值的名称,因此它必须包含表格的值&#34; tops&#34; 当运行服务器时:mvn spring-boot:run 日志显示异常:

  

字段topRepository中   com.Friendly_road.Flight.controller.topController需要一个bean   键入&#39; com.Friendly_road.Flight.dao.TopRepository&#39;那不可能   找到。

1 个答案:

答案 0 :(得分:0)

这种在DAO中使用存储过程的方式是某种“手工制造”且危险的。您实现的配置步骤在每次方法调用时完成,并且非常容易出错。它甚至不再可配置与数据库配置等相关。

文中说你正在使用Spring Boot,那么为什么不使用DataSourceAutoConfiguration并在application.properties / application.yml文件中维护你的数据库配置呢?这样你就可以让Spring管理你的数据源。这样可以简化事务处理并使用连接池,这样更方便。

这是一个很好的article

除此之外,如果您使用Spring Data JPA存储库接口,则无需自行实现过程调用。您可以通过类似的方式轻松实现存储过程调用(假设您有类似Top Reality的内容):

@Repository
public interface TopRepository extends CrudRepository<Top, Long> {

    ...

    @Procedure(procedureName = "topAlerte")
    List getTopAlerte();

    ...

}