//定义存储过程
create or replace procedure pag_add(p1 varchar2,p2 varchar2,p3 out varchar2)
as
begin
p3:=p1+p2;
end;
//定义存储函数
DELIMITER $$
CREATE FUNCTION fun_add(p1 INT ,p2 INT) RETURNS INT
BEGIN
RETURN p1+p2;
END $$
DELIMITER ;
定义接口
public interface CallMapper {
// public void queryEmp(Map map);
//查询存储过程的方法
public void callProcedure(Map map);
//查询函数的方法
public void callFunction(Map map);
}
mapper文件
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
测试类
public class TestMyBatis {
//封装session的返回
public static SqlSession getSession(){
String resource = "/cn/et/mybatis/lesson02/mybatis.xml";
InputStream inputStream =TestMyBatis.class.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//操作数据库的类 SqlSession
SqlSession session=sqlSessionFactory.openSession();
return session;
}
//调用存储过程
public static void main(String[] args) throws IOException {
SqlSession session=getSession();
CallMapper cm=session.getMapper(CallMapper.class);
Map map=new HashMap();
map.put("p1", 100);
map.put("p2", 200);
cm.callProcedure(map);
System.out.println(map.get("p3"));
}
@Test
//调用函数
public void funTest(){
SqlSession session=getSession();
CallMapper cm=session.getMapper(CallMapper.class);
Map map=new HashMap();
map.put("p1", 100);
map.put("p2", 400);
cm.callFunction(map);
System.out.println(map.get("p3"));
}
}
通用mapper使用:
service方法;
public void sum(){
Map map=new HashMap();
map.put("p1", 100);
map.put("p2", 200);
userMapper.add(map);
System.out.println(map.get("p3"));
}
mapper接口:
public void add(Map map);
mapper文件:
{call prco_insert_orgtree ()}
]]>