天天看点

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

<select id="prohello" parametertype="hashmap" statementtype="callable">

<![cdata[

    {call pro_hello (#{p_user_name,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("p_user_name", "zhangsan");

                 string returnvalue = (string) session.selectone("user.prohello", param);

                 system.out.println("message=" + param.get("p_user_name"));

                 system.out.println("result=" + param.get("result"));

                 system.out.println("returnvalue=" + returnvalue);

            }catch (exception e) {

                e.printstacktrace();

            }finally {

              session.close();

            }

       }

}

4 执行java代码,控制台输出结果如下

2012-03-07 20:36:32,406 debug [java.sql.preparedstatement] -==>  executing: {call pro_hello(?,?)}

2012-03-07 20:36:32,406 debug [java.sql.preparedstatement] -==> parameters: zhangsan(string)

message=zhangsan

原帖地址:http://chenjc-it.iteye.com/blog/1443432