首先,需要执行符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. 代码审计之XiaoCms(后台任意文件上传至getshell,任意目录删除,会话固定漏洞)

    0x00 前言 这段时间就一直在搞代码审计了.针对自己的审计方法做一下总结,记录一下步骤. 审计没他,基础要牢,思路要清晰,姿势要多且正. 下面是自己审计的步骤,正在逐步调整,寻求效率最高. 0x01 ...

  2. 用 Identity Server 4 (JWKS 端点和 RS256 算法) 来保护 Python web api

    目前正在使用asp.net core 2.0 (主要是web api)做一个项目, 其中一部分功能需要使用js客户端调用python的pandas, 所以需要建立一个python 的 rest api ...

  3. 使用sysbench对mysql压力测试

    sysbench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况.关于这个项目的详细介绍请看:https://github.com/akopytov/sy ...

  4. js 移动端写搜索时怎么调用软键盘上面的搜索按钮

    这段时间一直在做移动端,所以遇到很多问题,现在很多网站在做移动端搜索的时候都不会在后面加一个搜索按钮,而是直接调用输入法上面的搜索搜索按钮进行搜索 input的一个新属性给我们提供非常方便的书写, 就 ...

  5. Python面试题解答

    1. 一个谜题 >>> t = (1, 2, [30, 40]) >>> t[2] += [50, 60] 到底会发生下面 4 种情况中的哪一种? a. t变成(1 ...

  6. 在visual studio的工程项目应用中打开console控制窗口

    在visual studio的工程项目应用中打开console控制窗口,这个可以方便我们在console中输出参数的值检查错误. 只需要在需要打开console的地方加入下面的代码即可. AllocC ...

  7. TSP(个人模版)

    O(n^2)TSP: #include<stdio.h> #include<string.h> #include<algorithm> #include<io ...

  8. Codeforces 791B Bear and Friendship Condition(DFS,有向图)

    B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...

  9. linux系统下,警告:warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration] 和 warning: the `gets' function is dangerous and should not be used. 的由来和解决方法。

    字符数组 的英文名字是 char [] gets()函数的基本用法为:char *gets(char *s); 该函数的参数是一个字符数组,该函数的返回值也是一个字符数组. linux下的代码如下: ...

  10. Equals()和GetHashCode()方法深入了解

    最近在看Jeffrey Richter的CLR Via C#,在看到GetHashCode()方法的时候,有一个地方不是特别明白,就是重写Equals()方法时为什么要把GetHashCode()方法 ...