ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 一、格式定义 映射文件方式调用存储过程 的定义格式如下: ~~~ <procedure id="proc_id"> { proc_name( #{ param1 }, #{ param2 } , ......, #{ paramN } ) } </procedure> ~~~ 映射文件方式调用自定义的函数格式如下: ~~~ <procedure id="func_id"> { #{return} = func_name( #{ param1 }, #{ param2 } , ......, #{ paramN } ) } </procedure> 或 <procedure id="func_id"> { ?= func_name( #{ param1 }, #{ param2 } , ......, #{ paramN } ) } </procedure> -- 默认的函数值返回名称为“return”. ~~~ ### 二、实例参考 存储过程定义 ~~~ DROP PROCEDURE IF EXISTS proc_test_selectall; CREATE PROCEDURE proc_test_selectall (IN p_name varchar(30),OUT p_sum int) begin insert into test_book (book_name) values (p_name); select count(*) into p_sum from test_book; select * from test_book; end; ~~~ mapper定义 ~~~ <procedure id="test_102"> <![CDATA[ { call proc_test_selectall ( ? , #{sum | OUT } )} ]]> </procedure> ~~~ java代码 ~~~ public class TestMapperProcedure { private MLinkMapper mlinkmapper = null; public TestMapperProcedure() { MLinkClient mlinkclient = new MLinkClient("http://localhost:8080"); this.mlinkmapper = new MLinkMapper(mlinkclient); } public void test102() throws DataAccessException { int random = (int)(1+Math.random()*(9999-1+1)); ResultMo rm = mlinkmapper.procedure("test_102", "name开发指南" + random); if (rm != null) { System.out.println(">>"+rm); System.out.println(">> sum = "+rm.getParameter("sum")); List<Object[]> list = rm.getList(); for (Object[] objs : list) { System.out.println(">> "+Arrays.toString(objs)); } } else { System.err.println(">> ResultMo rm 返回空值!"); } } public static void main(String[] args) { try { new TestMapperProcedure().test2(); } catch (DataAccessException e) { e.printStackTrace(); } } } ~~~ ### 三、实例参考集锦 默认输入模式IN { call proc_test_insert ( ? ) } //仅是输入值,需要用数组方式赋值 { call proc_test_insert ( #{name} ) } // 默认IN输入模式 { call proc_test_insert ( #{name | OUT | VARCHAR} ) } //参数类型也可以明确指出。当然,默认为Java_Object就足够了,一般情况下无需要指出。 { call proc_test_insert ( #{name | INOUT } ) } { call proc_test_insert ( 123, 456, #{name} ) } { call proc_test_insert ( 123, 456, #{name1}, #{name2}, #{name3 | OUT} ) } { ?= call func100 ( 123, 456 ) } // 默认返回值名称 return { #{myname} = call func100 ( 123, 456, ) } //返回值名称 myname import java.util.Arrays; import java.util.List; import cn.cantong.mlink.MLinkClient; import cn.cantong.mlink.MLinkMapper; import cn.cantong.mlink.exception.DataAccessException; import cn.cantong.mlink.mo.ResultMo; public class TestMapperProc { private MLinkMapper mlinkmapper = null; public TestMapperProc() { MLinkClient mlinkclient = new MLinkClient("http://localhost:8080"); this.mlinkmapper = new MLinkMapper(mlinkclient); } public void test101() throws DataAccessException { System.out.println("--------------------------------------------------"); System.out.println("插入 (name返回自增ID){ call proc_test_insert (#{name | INOUT } )}"); System.out.println("--------------------------------------------------"); int random = (int)(1+Math.random()*(9999-1+1)); ResultMo rm = mlinkmapper.procedure("test_101", "name", "Java开发指南" + random); if (rm != null) { System.out.println(">>"+rm); } else { System.err.println(">> ResultMo rm 返回空值!"); } } public void test102() throws DataAccessException { System.out.println("--------------------------------------------------"); System.out.println("插入 (返回sum和全部结果集){ call proc_test_selectall (? ,#{sum | OUT } )}"); System.out.println("--------------------------------------------------"); int random = (int)(1+Math.random()*(9999-1+1)); ResultMo rm = mlinkmapper.procedure("test_102", "name开发指南" + random); if (rm != null) { System.out.println(">>"+rm); System.out.println(">> sum = "+rm.getParameter("sum")); List<Object[]> list = rm.getList(); for (Object[] objs : list) { System.out.println(">> "+Arrays.toString(objs)); } } else { System.err.println(">> ResultMo rm 返回空值!"); } } public void test103() throws DataAccessException { System.out.println("--------------------------------------------------"); System.out.println("函数 (like查询 return 返回结果) { ? = call func_get_count ( ? )}"); System.out.println("--------------------------------------------------"); ResultMo rm = mlinkmapper.procedure("test_103", "开发指南"); if (rm != null) { System.out.println(">>"+rm); System.out.println(">> return = "+rm.getParameter("return")); } else { System.err.println(">> ResultMo rm 返回空值!"); } } public static void main(String[] args) { try { new TestMapperProc().test101(); new TestMapperProc().test102(); new TestMapperProc().test103(); } catch (DataAccessException e) { e.printStackTrace(); } } }