Hi everyone,
I am new to MyBatis(I am using version 3.2.5).
This problem may be asked many times before, but still I don't know the answer.
Right now, I am able to call sql, such as select, insert, update, delete in xml.
But unfortunately I couldn't call Oracle stored procedure, and don't know where I am wrong. :-(((
Please help me and point out where I am wrong if possible, thanks guys!
Here is my code, in Java side,
I have a bean class User.java
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
In my DAO class, I am calling the SP by follow sentence.
I want to select all the users which age = 22, there is only one such user in database.
User user = new User();
user.setName("tom");
user.setAge(22);
User user2 = (User) session.selectOne("UserMapper.queryUser",user);
In UserMapper.xml, I have a block.
<select id="queryUser" parameterType="User" statementType = "CALLABLE" >
{CALL QUERYUSER( #{age, mode=IN, jdbcType=INTEGER},
#{rs_name, mode=OUT, jdbcType=VARCHAR},
#{rs_age, mode=OUT, jdbcType=INTEGER})}
</select>
and in my Oracle database, I have a table USERINFO,
USERINFO(
NAME VARCHAR(30),
AGE NUMBER(5)
)
and I am writing my stored procedure like this:
In fact, I don't know how to write SP, I just wrote mine according to what I can get on the Internet.
So please forgive me if I wrote a terrible stored procedure.
CREATE OR REPLACE
PROCEDURE queryUser (OUT rs_name VARCHAR,
OUT rs_age INTEGER,
IN age INTEGER
) AS
BEGIN
SELECT name,age INTO rs_name,rs_age
FROM USERINFO
where age = #age;
END;
When I ran it, it will throw exception:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00905: object MYBATIS.QUERYUSER is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
### The error may involve UserMapper.queryUser-Inline
### The error occurred while setting parameters
### Cause: java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00905: object MYBATIS.QUERYUSER is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
I guess I am writing the incorrect stored procedure, or incorrect xml, or both.
I also tried using cursor, it didn't work neither, or maybe I wrote in a wrong way.
Anyway, I don't know how to write a stored procedure for MyBatis.
I previously used stored functions, and use jdbc to call them, it can work; but for MyBatis, it doesn't consider it as procedure. Is MyBatis only able to call procedure not function??
Can someone please help me?
Really appreciate that.
Thanks and Regards,
Xiaobin