首先,需要执行符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. Winform跨窗体操作控件(使用委托)

    Winform跨窗体操作控件是winform开发中很常见的形式,最常见且简单有效的方式便是使用委托的方式来进行操作,下面我将通过一个小实例来说明如何使用委托跨窗体实现控件操作. 实例介绍:两个窗体,F ...

  2. HtmlWebpackPlugin实现资源的自定义插入

    目前碰到的问题 我们用html-webpack-plugin的inject属性去自动插入打包后的js, css到页面中,但是如果想给script标签添加一个crossorigin属性呢, 例如: &l ...

  3. 日期插件-flatpickr

    github的仓库地址:https://github.com/chmln/flatpickr 手册地址:http://www.htmleaf.com/Demo/201608213895.html    ...

  4. React Native 网络层分析

    文:志俊(沪江Web前端) 本文原创,转载请注明作者及出处 在使用React Native开发中,我们熟练的采用JavaScript的方式发送请求的方式发送一个请求到服务端,但是处理这个请求的过程其实 ...

  5. CTF---Web入门第五题 貌似有点难

    貌似有点难分值:20 来源: 西普学院 难度:难 参与人数:7249人 Get Flag:2519人 答题人数:2690人 解题通过率:94% 不多说,去看题目吧. 解题链接: http://ctf5 ...

  6. [51nod1614]刷题计划

    大赛将至,摆在你面前的是n道题目,第 i(1 ≤ i ≤ n) 道题目能提升 ai 点智力值,代码量为 bi KB,无聊值为 ci ,求至少提升m点智力值的情况下,所做题目代码量之和*无聊值之和最小为 ...

  7. [51nod1407]与与与与

    有n个整数,问从他们中取出若干个数字相与之后结果是0的有多少组. 答案比较大,输出对于 1,000,000,007 (1e9+7)取模后的结果. Input 第一行输入一个整数n.(1<=n&l ...

  8. hdu_2087 剪花布条(kmp)

    剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. c++2(循环和递归)

    其实编程的朋友知道,不管学什么语言,循环和递归是两个必须学习的内容.当然,如果循环还好理解一点,那么递归却没有那么简单.我们曾经对递归讳莫如深,但是我想告诉大家的是,递归其实没有那么可怕.所谓的递归就 ...

  10. vi 方向键和Backspace键失效问题的解决方法

    安装的ubuntu默认的编辑器是vi,遇到了两个问题: ① insert模式下,按方向键将产生A.B.C.D等字符,解决方案: :set nocompatible ② insert模式下Backspa ...