备注:本包包括了log4j2的日志功能
一、定义存储过程
CREATE DEFINER=`mybatis`@`localhost` PROCEDURE `test_procedure_01`(IN a int ,in b int ,out c int)
BEGIN
业务......
//and commit;
set c=a+b;
END
传入参数a,b,过程计算a+b的结果返回给C,
在mysql客户端 测试
SET @p_out=1;
CALL test_procedure_01(5, 6,@p_out);
SELECT @p_out;
测试结果:
二、 XML文件配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.elements.proc.dao.CallprocMapper">
<select id="callProc" parameterType="java.util.Map"
statementType="CALLABLE" >
{CALL test_procedure_01(
#{a,jdbcType=INTEGER,mode=IN},
#{b,jdbcType=INTEGER,mode=IN},
#{c,jdbcType=INTEGER,mode=INOUT})
}
</select>
</mapper>
三、接口定义类
package com.elements.proc.dao;
import java.util.Map;
public interface CallprocMapper {
void callProc(Map map); //注意这里没有返回值
}
四、JAVA测试类
package com.elements.proc;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.elements.proc.dao.CallprocMapper;
public class TestUserAddress {
@Test
public void TestSelectAll() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try {
CallprocMapper mx = (CallprocMapper) session
.getMapper(CallprocMapper.class);
int cc = 0;
Map map=new HashMap();
map.put("a", 1);
map.put("b", 2);
map.put("c", cc);
mx.callProc(map);
int bbb=(Integer) map.get("c");//这里才是我们可以取得返回值的地方
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaa:"+bbb);
} finally {
session.close();
}
}
}
项目代码地址: http://pan.baidu.com/s/1kV92pgF