首先,需要执行符DELIMITER ,建议用//,即在存储过程开始前定义delimiter //,在结束后加上//,最后加上DELIMITER ; 具体原因@参考文章1写的很清楚,不再赘述。

参考文章1中的示例:

delimiter //;     -- 改变 MySQL delimiter 为:“//”   

drop procedure if exists pr_stat_agent //   

-- call pr_stat_agent ('2008-07-17', '2008-07-18')   

create procedure pr_stat_agent
(
pi_date_from date
,pi_date_to date
)
begin
-- check input
if (pi_date_from is null) then
set pi_date_from = current_date();
end if; if (pi_date_to is null) then
set pi_date_to = pi_date_from;
end if; set pi_date_to = date_add(pi_date_from, interval 1 day); -- stat
select agent, count(*) as cnt
from apache_log
where request_time >= pi_date_from
and request_time < pi_date_to
group by agent
order by cnt desc;
end; // delimiter ; // -- 改回默认的 MySQL delimiter:“;”

删除存过程

drop procedure p_name;

完整示例:

mybatis调用:

<!-- 根据用户id获取姓名Zhangyn,以下两种方法等效 -->
<select id="getUserName" resultType="java.lang.String">
<!-- select name from user where id=#{0} -->
{call yanan.get_user_name(#{0,jdbcType=INTEGER,mode=IN})}
</select>

更新:

mysql> use zyt;
Database changed
mysql> delimiter //
mysql> create procedure updateordercountbyid(in cid int,in ordercount int,in personinfoid int)
-> begin
-> update t_cartgoods set orderCount=ordercount where cartGoodsId=cid and personInfoId=personinfoid;
-> end //
Query OK, 0 rows affected mysql>
<!--  更新购物车购买数量,以下两种方法等效 -->
<update id="updateOrderCountByCartId" >
<!-- update t_cartgoods set orderCount=#{1} where cartGoodsId=#{0} and personInfoId=#{2} -->
{call yanan.updateordercountbyid(#{0,jdbcType=INTEGER,mode=IN},#{1,jdbcType=INTEGER,mode=IN},#{2,jdbcType=INTEGER,mode=IN})}
</update>

条件:

mysql> use yanan;
Database changed
mysql> delimiter //
mysql> create procedure test(in i int) begin declare t int;if(i<=2) then set t=3;else set t=1;end if;select * from user where id=t;end //
Query OK, 0 rows affected
mysql> delimiter ;
mysql> call test(0);
+----+------+
| id | name |
+----+------+
| 3 | 1234 |
+----+------+
1 row in set Query OK, 0 rows affected mysql> call test(1);
+----+------+
| id | name |
+----+------+
| 3 | 1234 |
+----+------+
1 row in set Query OK, 0 rows affected mysql> call test(2);
+----+------+
| id | name |
+----+------+
| 3 | 1234 |
+----+------+
1 row in set Query OK, 0 rows affected mysql> call test(3);
+----+-------+
| id | name |
+----+-------+
| 1 | yanan |
+----+-------+
1 row in set Query OK, 0 rows affected mysql> call test(4);
+----+-------+
| id | name |
+----+-------+
| 1 | yanan |
+----+-------+
1 row in set Query OK, 0 rows affected mysql> select * from user;
+----+-------+
| id | name |
+----+-------+
| 1 | yanan |
| 2 | zhang |
| 3 | 1234 |
| 4 | 7890 |
+----+-------+
4 rows in set mysql>

循环

mysql> drop procedure test;
Query OK, 0 rows affected mysql> delimiter //
mysql> create procedure test(in i int) begin declare t int;
-> while i<4 do
-> set i=i+1;end while; set t=i;select * from user where id=t;end //
Query OK, 0 rows affected
mysql> delimiter ;
mysql> select * from user;
+----+-------+
| id | name |
+----+-------+
| 1 | yanan |
| 2 | zhang |
| 3 | 1234 |
| 4 | 7890 |
+----+-------+
4 rows in set mysql> call test(-1);
+----+------+
| id | name |
+----+------+
| 4 | 7890 |
+----+------+
1 row in set Query OK, 0 rows affected mysql> call test(4);
+----+------+
| id | name |
+----+------+
| 4 | 7890 |
+----+------+
1 row in set Query OK, 0 rows affected mysql> call test(5);
Empty set Query OK, 0 rows affected mysql>

mysql存储过程且mybatis调用的更多相关文章

  1. MySQL存储过程_创建-调用

    阅读目录:MySQL存储过程_创建-调用-参数 存储过程:SQL中的"脚本" 创建存储过程 调用存储过程 存储过程体 语句块标签 存储过程的参数 in:向过程里传参 out:过程向 ...

  2. taskctl实现自定义mysql存储过程作业类型调用

    TASKCTL支持任意作业类型的扩展,但目前TASKCTL 4.1.3版本中并没有内置mysql存储过程的作业插件.通过介绍使TASKCTL支持调度mysql存储过程作业类型的步骤,一方面解决一些朋友 ...

  3. sqlserver存储过程及mybatis调用——待续

    创建带输入参数存储过程 use yanantestgoif exists (select * from sys.objects where name='yanan')drop procedure ya ...

  4. mysql 存储过程--- 创建,调用,删除

    DELIMITER //CREATE PROCEDURE p_addscore(nums INT,OUT retrows INT)BEGINDECLARE i INT DEFAULT 0;add_lo ...

  5. MySQL存储过程的创建及调用

    阅读目录:MySQL存储过程_创建-调用-参数 存储过程:SQL中的“脚本” 1.创建存储过程 2.调用存储过程 3.存储过程体 4.语句块标签 存储过程的参数 1.in:向过程里传参 2.out:过 ...

  6. java, mybatis, 调用mysql存储过程

    Map<String, Object> bindinfo = new HashMap<String, Object>();            bindinfo.put(&q ...

  7. MySQL 存储过程实例 与 ibatis/mybatis/hibernate/jdbc 如何调用存储过程

    虽然MySQL的存储过程,一般情况下,是不会使用到的,但是在一些特殊场景中,还是有需求的.最近遇到一个sql server向mysql迁移的项目,有一些sql server的存储过程需要向mysql迁 ...

  8. mysql 存储过程,以及mybatis如何调用

    说道存储过程,很多人都知道,但是真正用的人其实很少,但是在某些必要的场景,是必须使用的,虽然可以使用java代码解决,但是效率性能远不及存储过程 曾经在sqlserver 以及pgadmin上用过,m ...

  9. spring mybatis 3.2调用mysql存储过程返回多结果集(完整、亲测、可用)

    最近,有个开发提了个需求,希望中间件支持调用mysql存储过程时支持多结果集返回,因为某些原因我们使用了不少的存储过程,很多复杂的逻辑目前来看交互非常的多,所以从当前的现状来说,这个需求还是蛮合理的. ...

随机推荐

  1. findhex

    FindPattern(hD3D, 0x128000, (PBYTE)"\xC7\x06\x00\x00\x00\x00\x89\x86\x00\x00\x00\x00\x89\x86&qu ...

  2. 1、ABPZero系列教程之拼多多卖家工具 前言

    此系列文章围绕着拼多多卖家工具来介绍ABPZero的使用,内容包括手机登录.手机注册.拼团提醒.微信公众号绑定帐号.有拼团发送消息到微信公众号(只要关注过微信公众号并已绑定系统帐号). 学习此系列必备 ...

  3. HTML知识点总结之表单元素

    网页不可能是纯静态的,没有任何的交互功能:绝大多数的网站都有表单元素的使用.表单提供了一个浏览者和网站交互的途径,比如用户注册登录,用户留言等功能. form form元素只是一个数据获取元素的容器, ...

  4. flush table with read lock的轻量级解决方案[原创]

    为什么要使用FTWRL   MySQL dba在日常工作中,数据备份绝对是工作频度最高的工作内容之一.当你使用逻辑方式进行备份(mydumper,mysqldump)或物理方式进行备份(percona ...

  5. 大白话说Java泛型(一):入门、原理、使用

    文章首发于[博客园-陈树义],点击跳转到原文<大白话说Java泛型(一):入门.原理.使用> 远在 JDK 1.4 版本的时候,那时候是没有泛型的概念的.当时 Java 程序员们写集合类的 ...

  6. dict-命令行下中英文翻译工具

    命令行下中英文翻译工具(Chinese and English translation tools in the command line) 安装(Install) ubuntu 安装 pip sud ...

  7. 修复Java使用POI合并Excel单元格后,边框不显示的问题

    使用Apache POI生成Excel文档时,当进行单元格合并操作后,被合并的单元格边框会消失,使用如下方式可以解决. 创建方法: public void setBorderStyle(int bor ...

  8. CSS基础之盒子模型及浮动布局

    盒模型 谈到盒模型,有经验的小伙伴一定滚瓜烂熟,无非就是 内容(content).填充(padding).边框(border).边界(margin): 这些属性我们可以把它转移到我们日常生活中的盒子( ...

  9. zoj 3228:Searching the String

    Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...

  10. 剪邮票dfs+bfs+组合+结构体

    #include<iostream>#include<queue>using namespace std;struct Point{ int x; int y; };queue ...