我有仅带有OUT参数的Mysql存储过程,如何通过仅带有OUT参数的休眠状态调用此存储过程?

CREATE PROCEDURE DBNAME.getStatistics (OUT A BIGINT UNSIGNED, OUT B BIGINT UNSIGNED, OUT C BIGINT UNSIGNED)
    BEGIN

        SELECT count(*) into A   from  DBNAME.Table1 where id =0;
        SELECT count(*) into B   from DBNAME.Table2 where id>0 and id<4;
        SELECT count(*) into C   from  DBNAME.Table3 where id>4;
    END


编辑

我在Java中使用以下代码通过hibernate(Java)测试MySQL存储过程(仅具有OUT参数)的调用:

Session session = HibernateUtil.getSessionFactory().openSession();

Connection connection = session.connection();
CallableStatement callable = null;

try {

callable = connection.prepareCall("{Call DBNAME.getStatistics(?,?,?)}");

// three ?,?,? means three OUT parameter.
// If any parameters is IN type of
// Lets say the first one then use : callable.setInt(1, 10);

    callable.registerOutParameter(1, Types.BIGINT);
    callable.registerOutParameter(2, Types.BIGINT);
    callable.registerOutParameter(3, Types.BIGINT);
    callable.executeUpdate();

    int value1 = callable.getInt(1);
    int value2 = callable.getInt(2);
    int value3 = callable.getInt(3);


    System.out.println(value1);
    System.out.println(value2);
    System.out.println(value3);
   } catch (SQLException e) {
       e.printStackTrace();
 }

最佳答案

我认为您无法使用Hibernate做到这一点。我以前看过

Connection connection = session.connection();
CallableStatement callable = null;
callable = connection.prepareCall("execute [procedure] ?");
callable.registerOutParameter(1, Types.INTEGER);
callable.execute();
int id = callable.getInt(1);


哪个有效。有关更多上下文,请参见Retrieving a value from Stored Procedure using Native SQL Hibernate

08-29 00:22