存储过程可以一次执行多条语句,处理复杂的业务逻辑,完成一些计算。
这篇博客总结一下mysql中存储过程基本的用法——mysql存储过程开荒。我们从怎么写存储过程和怎么调用两方面来探讨下:
一、mysql中存储过程的用法
注意下面的示例可以在mysql管理工具中(我用的navicat)直接运行,如果要在mysql客户端(dos窗口)需要加 delimiter$$ 分隔符。
首先来看第一个例子:
这个存储过程有两个int类型的输入参数,一个varchar类型的输出参数
在begin和end之前执行数据库操作或是计算,
用declare声明了一个int类型的变量,
后面是一个if 判断,注意后面需要有then 和end if,这才是完整的if判断
select语句进行输出,可以直接用select ‘*’输出,或是用as 添加一个列名
存储过程写好编译无误后,用call调用,这里需要一个输出参数,所以我们定义了一个@p_in变量
<code class="hljs sql">use etoak; drop procedure if exists t1; create procedure t1(in a int,in b int,out d varchar(30)) begin declare c int; if a is null then set a = 0; end if; if b is null then set b = 0; end if; set c = a + b; /* select c as sum;*/ select 's' into d; select d as '哈哈'; -- 输出一列 end; /*调用存储过程*/ set @p_in = 1; call t1(10,1,@p_in);</code>
上面我们使用if then条件判断,下面来看使用case when来完成更多的条件:
<code class="hljs sql"><code class="hljs sql">drop procedure if exists t1; create procedure t1(in a int,in b int,out c varchar(30)) begin declare d int; set d = a+1; case d when 1 then insert into student values(null,'dx',11,now()); when 2 then insert into student values(null,'aa',11,now()); else insert into student values(null,'bb',11,now()); end case; select * from student; end;</code></code>
<code class="hljs sql">再来看两个循环,一个是while do循环,一个是loop循环:
<code class="hljs sql"><code class="hljs sql"><code class="hljs vbnet"> /*使用while do循环*/ create procedure t1() begin declare i int DEFAULT 0; while i<5 DO insert into student(name) values(i); set i=i+1; end while; select * from student; end; /*使用loop循环*/ drop procedure if exists t1; create procedure t1() begin declare i int DEFAULT 0; loop_label:LOOP if i = 3 THEN set i = i + 1; ITERATE loop_label; -- iterate相当于java循环里的continue end if; insert into student values(null,i,i,now()); set i = i + 1; if i >= 5 THEN leave loop_label; end if; end loop; select * from student; end;</code></code></code>
<code class="hljs sql"><code class="hljs vbnet">还有比较常用的模糊查询:
<code class="hljs sql"><code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql">/*模糊查询*/ drop procedure if exists t1; create procedure t1(in a varchar(30),out c varchar(30)) begin declare d int; select * from student where name like concat('%',a,'%'); end;</code></code></code></code>
<code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql">这个例子中要注意的是使用了concat拼接字符串函数。
<code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql">二、在java代码中如何调用存储过程
<code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql">通过上面我们知道可以在mysql客户端里面通过call调用存储过程,那在java代码里面又是如何调用的呢<br>
我们来看下下面的例子,使用jdbc的方式调用带输入输出参数的存储过程:<br>
存储过程为如下,实现简单的加法:
<code class="hljs sql"><code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql"><code class="hljs oxygene">create procedure t1(in a int,in b int,out d int) begin declare c int; if a is null then set a = 0; end if; if b is null then set b = 0; end if; set c = a + b; select c into d; end;</code></code></code></code></code>
<code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql"><code class="hljs oxygene"><strong>java中通过jdbc调用:</strong>
<code class="hljs sql"><code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql"><code class="hljs oxygene"><code class="hljs java"> import java.sql.CallableStatement; import java.sql.Connection; import java.sql.SQLException; import java.sql.Types; public class TestProc { public static void main(String[] args) throws SQLException { TestProc tp = new TestProc(); int a = tp.testPro(5, 6); System.out.println(a); //打印输出值 } //获取数据库连接 private static DBConnection dbConnection=null; static { if (null == dbConnection) { dbConnection = new DBConnection(); } } //执行存储过程的方法 public int testPro(int a,int b) throws SQLException{ Connection conn = null; CallableStatement stmt = null; int out = 0; String sql=""; try { conn = dbConnection.getConnection(); stmt = conn.prepareCall("{call t1(?,?,?) }"); stmt.setInt(1, a); stmt.setInt(2, b); stmt.registerOutParameter(3, Types.INTEGER); stmt.execute(); out = stmt.getInt(3); //这里获取下输出参数 }finally { dbConnection.close(conn); dbConnection.close(stmt); } return out; } } </code></code></code></code></code></code>
<code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql"><code class="hljs oxygene"><code class="hljs java"><strong>mybatis中存储过程的调用:</strong>
<code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql"><code class="hljs oxygene"><code class="hljs java">声明接口:
<code class="hljs sql"><code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql"><code class="hljs oxygene"><code class="hljs java"><code class="hljs lasso">public Map proc(Map map);</code></code></code></code></code></code></code>
<code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql"><code class="hljs oxygene"><code class="hljs java"><code class="hljs lasso">xml:
<select id="proc" parameterType="map" statementType="CALLABLE"> {call t1( #{firstParam,jdbcType=INTEGER,mode=IN}, #{secondParam,jdbcType=INTEGER,mode=IN}, #{outParam,jdbcType=INTEGER,mode=OUT} )} </select>
<code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql"><code class="hljs oxygene"><code class="hljs java"><code class="hljs lasso"><code class="hljs cs">测试:
<code class="hljs sql"><code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql"><code class="hljs oxygene"><code class="hljs java"><code class="hljs lasso"><code class="hljs cs"><code class="hljs vhdl">Map map = new HashMap(); map.put("firstParam",1); map.put("second", 2); bi.proc(map); System.out.println(map.toString());</code></code></code></code></code></code></code></code></code>
<code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql"><code class="hljs oxygene"><code class="hljs java"><code class="hljs lasso"><code class="hljs cs"><code class="hljs vhdl"><strong>这里注意一下:</strong><br>
mybatis的入参map里面不需要put输出参数,执行完存储过程之后,会自动把输出参数放到map里面。所以我们的打印结果如下:
<code class="hljs sql"><code class="hljs vbnet"><code class="hljs sql"><code class="hljs oxygene"><code class="hljs java"><code class="hljs lasso"><code class="hljs cs"><code class="hljs vhdl">{second=2, firstParam=1, outParam=1}

存儲過程是MySQL中的預編譯SQL語句集合,用於提高性能和簡化複雜操作。 1.提高性能:首次編譯後,後續調用無需重新編譯。 2.提高安全性:通過權限控制限制數據表訪問。 3.簡化複雜操作:將多條SQL語句組合,簡化應用層邏輯。

MySQL查詢緩存的工作原理是通過存儲SELECT查詢的結果,當相同查詢再次執行時,直接返回緩存結果。 1)查詢緩存提高數據庫讀取性能,通過哈希值查找緩存結果。 2)配置簡單,在MySQL配置文件中設置query_cache_type和query_cache_size。 3)使用SQL_NO_CACHE關鍵字可以禁用特定查詢的緩存。 4)在高頻更新環境中,查詢緩存可能導致性能瓶頸,需通過監控和調整參數優化使用。

MySQL被廣泛應用於各種項目中的原因包括:1.高性能與可擴展性,支持多種存儲引擎;2.易於使用和維護,配置簡單且工具豐富;3.豐富的生態系統,吸引大量社區和第三方工具支持;4.跨平台支持,適用於多種操作系統。

MySQL數據庫升級的步驟包括:1.備份數據庫,2.停止當前MySQL服務,3.安裝新版本MySQL,4.啟動新版本MySQL服務,5.恢復數據庫。升級過程需注意兼容性問題,並可使用高級工具如PerconaToolkit進行測試和優化。

MySQL備份策略包括邏輯備份、物理備份、增量備份、基於復制的備份和雲備份。 1.邏輯備份使用mysqldump導出數據庫結構和數據,適合小型數據庫和版本遷移。 2.物理備份通過複製數據文件,速度快且全面,但需數據庫一致性。 3.增量備份利用二進制日誌記錄變化,適用於大型數據庫。 4.基於復制的備份通過從服務器備份,減少對生產系統的影響。 5.雲備份如AmazonRDS提供自動化解決方案,但成本和控制需考慮。選擇策略時應考慮數據庫大小、停機容忍度、恢復時間和恢復點目標。

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

在MySQL中優化數據庫模式設計可通過以下步驟提升性能:1.索引優化:在常用查詢列上創建索引,平衡查詢和插入更新的開銷。 2.表結構優化:通過規範化或反規範化減少數據冗餘,提高訪問效率。 3.數據類型選擇:使用合適的數據類型,如INT替代VARCHAR,減少存儲空間。 4.分區和分錶:對於大數據量,使用分區和分錶分散數據,提升查詢和維護效率。

tooptimizemysqlperformance,lofterTheSeSteps:1)inasemproperIndexingTospeedUpqueries,2)使用ExplaintplaintoAnalyzeandoptimizequeryPerformance,3)ActiveServerConfigurationStersLikeTlikeTlikeTlikeIkeLikeIkeIkeLikeIkeLikeIkeLikeIkeLikeNodb_buffer_pool_sizizeandmax_connections,4)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。