[英]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.