標籤:
一、Mybatis調用預存程序
1 在資料庫中建立以下的預存程序
create or replace procedure pro_hello(p_user_name in varchar2,p_result out varchar2) is
begin
p_result := ‘hello,‘ || p_user_name;
end;
2 編寫SQL對應檔mapper.xml
statementType裡的CALLABLE是標註此sql為預存程序。
parameterType是標註要傳的參數,看了一些資料不寫parameterType的話預設傳map。還是加上比較清晰
<select id="proHello" parameterType="java.util.map" statementType="CALLABLE">
{call pro_hello(#{uname,mode=IN,jdbcType=VARCHAR},#{result,mode=OUT,jdbcType=VARCHAR})}
</select>
3 編寫JAVA代碼調用預存程序
public class ProcedureTest {
public static void main(String[] args) throws IOException {
String resource = "mybatis.cfg.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = ssf.openSession();
try {
Map<String, String> param = new HashMap<String, String>();
param.put("uname", "zhangsan");
param.put("result", "");
String returnValue = (String) session.selectOne("User.proHello", param);
System.out.println("message=" + param.get("uname"));
System.out.println("result=" + param.get("result"));
System.out.println("returnValue=" + returnValue);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
}
二、Mybatis調用function
function帶有傳回值,假設一個oracle函數增加學生後返回成功與否的字串
<select id="isMember" statementType="CALLABLE" parameterType="cn.StudentDto">
{#{result,mode=OUT,jdbvType=VARCHAR} = call
addStudent(#{num,mode=IN,jdbcType=DECIMAL},#{name,mode=IN,jdbcType=VARCHAR},#{rollInYear,mode=IN,jdbcType=Date},#{age,mode=OUT,jdbcType=INTEGER})}
</select>
StudentDTO除了上述出現的學生資訊欄位外還需要String類型的result欄位。
原帖地址:
http://chenjc-it.iteye.com/blog/1443432
http://shen84121062.iteye.com/blog/1213857
Mybatis調用Oracle中的預存程序和function