简体   繁体   English

如何使用JdbcTemplate在spring mvc中对存储过程或函数进行简单调用

[英]How to make a simple call to a stored procedure or function in spring mvc using JdbcTemplate

I have read a lot of questions about how to call a stored procedure using JdbcTemplate there are a lot of methods like using a beanMapper, creating a rowMapper, using callableStatement, but I have seen a lot of persons that say this: 我已经阅读了很多关于如何使用JdbcTemplate调用存储过程的问题,有很多方法比如使用beanMapper,创建一个rowMapper,使用callableStatement,但是我看到很多人这样说:

For simple procedures you may use jdbcTemplate's update method: 对于简单的过程,您可以使用jdbcTemplate的更新方法:

jdbcTemplate.update("call SOME_PROC (?, ?)", param1, param2);

I tried doing that at and my jdbcTemplate variable is always null, this is my stored procedure 我尝试这样做,我的jdbcTemplate变量始终为null,这是我的存储过程

CREATE OR REPLACE PROCEDURE addProce
    (
       num1 IN number,
       num2 IN number,
       result OUT number
    )
IS
BEGIN
    result := num1 + num2;

END;
/

and this is the class where I call it 这就是我称之为的课程

public class UsuerDAOImpl implements UserDAO {

    private JdbcTemplate jdbcTemplate;

    public UsuerDAOImpl () {}

    public UsuerDAOImpl (DataSource datasource) {
        this.jdbcTemplate  =  new JdbcTemplate(datasource);
    }

     public int addmethodJdbc() 
     {
        int result= 0;
         int someValue= jdbcTemplate.update("addProce(?, ?, ?)", 2, 1, result);

        return someValue;
    }
}

I have this method in my class and it works my jdbctemplate is not null in there 我在我的类中有这个方法,它的工作原理我的jdbctemplate不是null

public void insertUser(User user) {

    try {
        String sql = "INSERT INTO USER"
                + "(a, b, c, d)"
                + " VALUES (?, ?, ?, ?)";

        jdbcTemplate.update(sql, user.getA(),
                usaurio.getB(),
                usaurio.getC(),
                usaurio.getD());
    } 
    catch (DataAccessException dataAccessException) 
    {

        dataAccessException.printStackTrace();
    }
    catch(Exception e) 
    {

        e.printStackTrace();
    }
}   

I also try with a function here it is : 我也尝试使用这里的函数:

CREATE OR REPLACE FUNCTION addFunct
    (
       num1 IN number,
       num2 IN number
    )
    return number
IS
resultado number;
BEGIN
    result := num1 + num2;
    return (result);
END;
/

but still dont work my jdbcTemplate is null too 但仍然不工作我的jdbcTemplate也是null

I already know how to call them with the other ways, but I wanted to know how to call them in this easy way 我已经知道如何用其他方式调用它们,但我想知道如何以这种简单的方式调用它们

jdbcTemplate.update("call SOME_PROC (?, ?)", param1, param2);

use SimpleJdbcCall#withProcedureName() to call stored procedure. 使用SimpleJdbcCall#withProcedureName()来调用存储过程。 pass the name of the stored procedure as a parameter. 将存储过程的名称作为参数传递。

for example your method: 例如你的方法:

public int addmethodJdbc() {
   SimpleJdbcCall jdbcCall =  new SimpleJdbcCall(jdbcTemplate).withProcedureName("addFunct");
   SqlParameterSource in = new MapSqlParameterSource().addValue("num1", 2).addValue("num2", 1);

   Map<String, Object> out = jdbcCall.execute(in)
   int result = (Integer)out.get("result");
   return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Spring JdbcTemplate中的调用来调用存储的函数 - Calling stored function using call in Spring JdbcTemplate 如何使用 JdbcTemplate 从 Spring 创建存储的 function? - How to create stored function from Spring using JdbcTemplate? Spring JdbcTemplate 在调用 SQLserver 存储过程时非常慢 - Spring JdbcTemplate is extremely slow when call SQLserver Stored procedure 如何使用spring和hibernate调用存储过程 - how to call stored procedure using spring and hibernate Spring JdbcTemplate可以等待oracle存储过程完成多长时间 - how long can Spring JdbcTemplate wait for an oracle stored procedure to finish 如何在spring框架中使用HibernateTemplate调用存储过程? - How to call a stored procedure using HibernateTemplate in spring framework? 如何使用实体管理器在 spring 中调用 oracle 存储过程? - How to call oracle stored procedure in spring using entity manager? 如何使用hibernate在spring boot中调用MySQL存储过程? - how to call MySQL stored procedure in spring boot using hibernate? 如何使用Spring调用带有ref游标作为输出参数的存储过程? - How to call a stored procedure with ref cursor as an output parameter using Spring? 如何在 JdbcTemplate 中创建 mySQL 存储过程 - How to create a mySQL stored procedure in a JdbcTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM