{"id":2239,"date":"2023-05-07T09:01:16","date_gmt":"2023-05-07T01:01:16","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2239"},"modified":"2023-05-07T09:01:25","modified_gmt":"2023-05-07T01:01:25","slug":"groovy-code-example-database-advanced-operation","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/05\/07\/groovy-code-example-database-advanced-operation\/","title":{"rendered":"Groovy\u4ee3\u7801\u793a\u4f8b &#8211; \u6570\u636e\u5e93\u9ad8\u7ea7\u64cd\u4f5c"},"content":{"rendered":"<h2>\u6570\u636e\u5e93\u64cd\u4f5c\u4e4b\u4e8b\u52a1\u5904\u7406<\/h2>\n<p>\u6700\u7b80\u5355\u7684\u5b9e\u73b0\u6570\u636e\u5e93\u4e8b\u52a1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528withTransaction\u95ed\u5305\u5c06\u6570\u636e\u5e93\u64cd\u4f5c\u5305\u542b\u5728\u8be5\u95ed\u5305\u91cc\u9762\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">assert sql.firstRow(&#039;SELECT COUNT(*) as num FROM Author&#039;).num == 0\n\/\/\u4e8b\u52a1\u95ed\u5305\nsql.withTransaction {\n  sql.execute &quot;INSERT INTO Author (firstname, lastname) VALUES (&#039;Dierk&#039;, &#039;Koenig&#039;)&quot;\n  sql.execute &quot;INSERT INTO Author (firstname, lastname) VALUES (&#039;Jon&#039;, &#039;Skeet&#039;)&quot;\n}\nassert sql.firstRow(&#039;SELECT COUNT(*) as num FROM Author&#039;).num == 2<\/code><\/pre>\n<p>\u8fd9\u91cc\u6570\u636e\u5e93\u8868\u5f00\u59cb\u65f6\u5019\u4e3a\u7a7a\uff0c\u5728\u6210\u529f\u63d2\u5165\u540e\u6709\u4e24\u6761\u6570\u636e\u3002\u4e8b\u52a1\u95ed\u5305\u4ee5\u5916\u7684\u533a\u57df\u4e0d\u4f1a\u770b\u5230\u4ec5\u63d2\u5165\u4e00\u6761\u6570\u636e\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u5982\u679c\u4e8b\u52a1\u5904\u7406\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u4e86\u9519\u8bef\uff0cwithTransaction\u95ed\u5305\u4e2d\u7684\u64cd\u4f5c\u5c06\u4f1a\u56de\u6eda\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u5e93\u7684\u5143\u6570\u636e\u83b7\u53d6firstname\u5b57\u6bb5\u7684\u6700\u5927\u957f\u5ea6\u5e76\u8bd5\u56fe\u63d2\u5165\u8d85\u8fc7\u8be5\u957f\u5ea6\u7684firstname\u4ece\u800c\u5bfc\u81f4\u63d2\u5165\u5931\u8d25\uff0c\u6700\u540e\u4e8b\u52a1\u56de\u6eda\u3002<\/p>\n<pre><code class=\"language-java\">def maxFirstnameLength\ndef metaClosure = { meta -&gt; maxFirstnameLength = meta.getPrecision(1) }\ndef rowClosure = {}\ndef rowCountBefore = sql.firstRow(&#039;SELECT COUNT(*) as num FROM Author&#039;).num\ntry {\n  sql.withTransaction {\n    sql.execute &quot;INSERT INTO Author (firstname) VALUES (&#039;Dierk&#039;)&quot;\n    \/\/\u83b7\u53d6\u5143\u6570\u636e\n    sql.eachRow &quot;SELECT firstname FROM Author WHERE firstname = &#039;Dierk&#039;&quot;, metaClosure, rowClosure\n    \/\/\u8bd5\u56fe\u63d2\u5165\u8d85\u8fc7\u6700\u5927\u957f\u5ea6\u7684\u6570\u636e\n    sql.execute &quot;INSERT INTO Author (firstname) VALUES (?)&quot;, &#039;X&#039; * (maxFirstnameLength + 1)\n  }\n} catch(ignore) { println ignore.message }\ndef rowCountAfter = sql.firstRow(&#039;SELECT COUNT(*) as num FROM Author&#039;).num\nassert rowCountBefore == rowCountAfter<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u5c3d\u7ba1\u7b2c\u4e00\u6761\u8bed\u53e5\u6267\u884c\u6210\u529f\uff0c\u4f46\u662f\u7531\u4e8e\u540e\u7eed\u7684\u64cd\u4f5c\u5931\u8d25\u5bfc\u81f4\u6574\u4e2a\u95ed\u5305\u7684\u64cd\u4f5c\u88ab\u56de\u6eda\uff0c\u56e0\u6b64\u884c\u6570\u636e\u6ca1\u6709\u53d8\u5316\u3002<\/p>\n<h2>\u6570\u636e\u5e93\u64cd\u4f5c\u4e4b\u6279\u91cf\u5904\u7406<\/h2>\n<p>\u5f53\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u5c24\u5176\u662f\u63d2\u5165\u6570\u636e\u7684\u65f6\u5019\uff0c\u5c06\u8fd9\u4e9b\u64cd\u4f5c\u7ec4\u5408\u6210\u6279\u91cf\u6765\u5904\u7406\u6548\u7387\u4f1a\u66f4\u9ad8\u3002groovy\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>withBatch<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u6279\u91cf\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-java\">sql.withBatch(3) { stmt -&gt;\n  stmt.addBatch &quot;INSERT INTO Author (firstname, lastname) VALUES (&#039;Dierk&#039;, &#039;Koenig&#039;)&quot;\n  stmt.addBatch &quot;INSERT INTO Author (firstname, lastname) VALUES (&#039;Paul&#039;, &#039;King&#039;)&quot;\n  stmt.addBatch &quot;INSERT INTO Author (firstname, lastname) VALUES (&#039;Guillaume&#039;, &#039;Laforge&#039;)&quot;\n  stmt.addBatch &quot;INSERT INTO Author (firstname, lastname) VALUES (&#039;Hamlet&#039;, &#039;D&#039;&#039;Arcy&#039;)&quot;\n  stmt.addBatch &quot;INSERT INTO Author (firstname, lastname) VALUES (&#039;Cedric&#039;, &#039;Champeau&#039;)&quot;\n  stmt.addBatch &quot;INSERT INTO Author (firstname, lastname) VALUES (&#039;Erik&#039;, &#039;Pragt&#039;)&quot;\n  stmt.addBatch &quot;INSERT INTO Author (firstname, lastname) VALUES (&#039;Jon&#039;, &#039;Skeet&#039;)&quot;\n}<\/code><\/pre>\n<p>\u8fd9\u4e9b\u4ee5\u4e0a\u4ee3\u7801\u540e\uff0c\u4f1a\u63d2\u51657\u6761\u6570\u636e\u5230\u6570\u636e\u5e93\u3002\u5b9e\u9645\u4e0a\uff0c\u6570\u636e\u5df2\u7ecf\u88ab\u52a0\u5165\u6279\u91cf\uff0c\u867d\u7136\u6211\u4eec\u4e0d\u77e5\u9053\u6279\u91cf\u662f\u5982\u4f55\u63d2\u5165\u7684\u3002\u5982\u679c\u4f60\u60f3\u77e5\u9053\u6279\u91cf\u63d2\u5165\u7684\u7ec6\u8282\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u8fd0\u884c\u65e5\u5fd7\u3002\u5728\u6267\u884c\u6279\u91cf\u4e4b\u524d\u52a0\u4e0a\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">import java.util.logging.*\n\n\/\/\u4e0b\u4e00\u884c\u5f00\u59cb\u8be6\u7ec6\u7684\u65e5\u5fd7\u8bb0\u5f55\nLogger.getLogger(&#039;groovy.sql&#039;).level = Level.FINE\n\/\/ \u8fd8\u9700\u8981\u8c03\u6574JRE_HOME\/lib\u76ee\u5f55\u4e0blogging.properties\u7684\u4e0b\u8ff0\u914d\u7f6e\n\/\/ java.util.logging.ConsoleHandler.level = FINE\n\u5f00\u542f\u4e86\u65e5\u5fd7\u4ee5\u53ca\u8c03\u6574\u4e86logging.properties\u914d\u7f6e\u4ee5\u540e\uff0c\u5c06\u4f1a\u770b\u5230\u4ee5\u4e0b\u65e5\u5fd7\u8f93\u51fa\uff1a\n\nFINE: Successfully executed batch with 3 command(s)\nApr 19, 2015 8:38:42 PM groovy.sql.BatchingStatementWrapper processResult\n\nFINE: Successfully executed batch with 3 command(s)\nApr 19, 2015 8:38:42 PM groovy.sql.BatchingStatementWrapper processResult\n\nFINE: Successfully executed batch with 1 command(s)\nApr 19, 2015 8:38:42 PM groovy.sql.Sql getStatement<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a<\/p>\n<p>1\uff09\u4efb\u4f55\u7684SQL\u8bed\u53e5\u7ec4\u5408\u90fd\u53ef\u4ee5\u52a0\u5230\u6279\u91cf\u4e2d\uff0c\u5e76\u4e0d\u9700\u8981\u662f\u63d2\u5165\u540c\u5f20\u8868\u3002<\/p>\n<p>2\uff09\u4e3a\u907f\u514dSQL\u6ce8\u5165\uff0c\u5f3a\u70c8\u5efa\u8bae\u4f7f\u7528\u9884\u7f16\u8bd1\u8bed\u53e5prepared statment\uff0c\u53ef\u4ee5\u901a\u8fc7<code>witchBatch<\/code>\u540c\u6001\u65b9\u6cd5\uff0c\u4f7f\u7528GString\u548c\u53c2\u6570\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">def qry = &#039;INSERT INTO Author (firstname, lastname) VALUES (?,?)&#039;\nsql.withBatch(3, qry) { ps -&gt;\n  ps.addBatch(&#039;Dierk&#039;, &#039;Koenig&#039;)\n  ps.addBatch(&#039;Paul&#039;, &#039;King&#039;)\n  ps.addBatch(&#039;Guillaume&#039;, &#039;Laforge&#039;)\n  ps.addBatch(&#039;Hamlet&#039;, &quot;D&#039;Arcy&quot;)\n  ps.addBatch(&#039;Cedric&#039;, &#039;Champeau&#039;)\n  ps.addBatch(&#039;Erik&#039;, &#039;Pragt&#039;)\n  ps.addBatch(&#039;Jon&#039;, &#039;Skeet&#039;)\n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u63d0\u4f9b\u4e86\u66f4\u52a0\u5b89\u5168\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u5c24\u5176\u662f\u6570\u636e\u6765\u81ea\u4e8e\u811a\u672c\u6216\u662f\u8868\u5355\u63d0\u4ea4\u7684\u65f6\u5019\u3002\u5f53\u7136\uff0c\u5982\u679c\u4f7f\u7528\u9884\u7f16\u8bd1\u6279\u91cf\u7684\u8bdd\uff0c\u5c31\u53ea\u80fd\u9650\u4e8e\u540c\u4e2aSQL\u7684\u4f5c\u7528\u4e8e\u5355\u4e2a\u8868\u3002<\/p>\n<h2>\u6570\u636e\u5e93\u64cd\u4f5c\u4e4b\u5206\u9875\u5904\u7406<\/h2>\n<p>\u5f53\u9700\u8981\u5411\u7528\u6237\u5c55\u73b0\u4e00\u4e2a\u6570\u636e\u91cf\u8f83\u5927\u7684\u5e93\u8868\u65f6\uff0c\u901a\u8fc7\u5206\u9875\u5c55\u793a\u662f\u6bd4\u8f83\u5408\u7406\u548c\u65b9\u4fbf\u7684\u3002groovy\u4e2d\u8bb8\u591a\u67e5\u8be2\u65b9\u6cd5\u90fd\u53ef\u4ee5\u6307\u5b9a\u5206\u9875\u53c2\u6570\u3002\u4ee5\u4e0b\u4ee3\u7801\u901a\u8fc7\u6307\u5b9a\u8d77\u59cb\u4f4d\u7f6e\u548c\u9875\u9762\u5927\u5c0f\u786e\u5b9arows\u65b9\u6cd5\u7684\u5206\u9875\u67e5\u8be2\u7ed3\u679c\u3002<\/p>\n<pre><code class=\"language-java\">def qry = &#039;SELECT * FROM Author&#039;\nassert sql.rows(qry, 1, 3)*.firstname == [&#039;Dierk&#039;, &#039;Paul&#039;, &#039;Guillaume&#039;]\nassert sql.rows(qry, 4, 3)*.firstname == [&#039;Hamlet&#039;, &#039;Cedric&#039;, &#039;Erik&#039;]\nassert sql.rows(qry, 7, 3)*.firstname == [&#039;Jon&#039;]<\/code><\/pre>\n<h2>\u6570\u636e\u5e93\u64cd\u4f5c\u4e4b\u83b7\u53d6\u5e93\u8868\u5143\u6570\u636e<\/h2>\n<p>JDBC\u5143\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u83b7\u53d6\u3002\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u53ef\u80fd\u662f\u901a\u8fc7\u884c\u6570\u636erow\u83b7\u53d6\u8868\u540d\u3001\u5217\u540d\u3001\u5217\u7c7b\u578b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-java\">sql.eachRow(&quot;SELECT * FROM Author WHERE firstname = &#039;Dierk&#039;&quot;) { row -&gt;\n  def md = row.getMetaData()\n  assert md.getTableName(1) == &#039;AUTHOR&#039;\n  assert (1..md.columnCount).collect{ md.getColumnName(it) } == [&#039;ID&#039;, &#039;FIRSTNAME&#039;, &#039;LASTNAME&#039;]\n  assert (1..md.columnCount).collect{ md.getColumnTypeName(it) } == [&#039;INTEGER&#039;, &#039;VARCHAR&#039;, &#039;VARCHAR&#039;]\n}<\/code><\/pre>\n<p>\u4e0e\u4e0a\u8ff0\u4ee3\u7801\u7565\u6709\u4e0d\u540c\uff0c\u4e0b\u9762\u67e5\u770b\u4e0b\u5217\u7684\u522b\u540d\u3002<\/p>\n<pre><code class=\"language-java\">sql.eachRow(&quot;SELECT firstname AS first FROM Author WHERE firstname = &#039;Dierk&#039;&quot;) { row -&gt;\n  def md = row.getMetaData()\n  assert md.getColumnName(1) == &#039;FIRSTNAME&#039;\n  \/\/\u83b7\u53d6\u522b\u540d\n  assert md.getColumnLabel(1) == &#039;FIRST&#039;\n}<\/code><\/pre>\n<p>\u83b7\u53d6\u5143\u6570\u636e\u662f\u7ecf\u5e38\u7528\u5230\u7684\uff0c\u56e0\u6b64groovy\u63d0\u4f9b\u4e86\u591a\u6001\u65b9\u6cd5\u6307\u5b9a\u5143\u6570\u636e\u95ed\u5305\uff0c\u8be5\u95ed\u5305\u53ea\u6267\u884c\u4e00\u6b21\u7528\u4e8e\u83b7\u53d6\u5143\u6570\u636e\uff0c\u540c\u65f6\u6307\u5b9a\u884c\u6570\u636e\u95ed\u5305\u7528\u4e8e\u6bcf\u884c\u6570\u636e\u7684\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e24\u4e2a\u95ed\u5305\u7684\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-java\">def metaClosure = { meta -&gt; assert meta.getColumnName(1) == &#039;FIRSTNAME&#039; }\ndef rowClosure = { row -&gt; assert row.FIRSTNAME == &#039;Dierk&#039; }\nsql.eachRow(&quot;SELECT firstname FROM Author WHERE firstname = &#039;Dierk&#039;&quot;, metaClosure, rowClosure)<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u83b7\u53d6\u5143\u6570\u636e\u53ea\u9700\u8981\u4e00\u884c\u6570\u636e\u5373\u53ef\uff0c\u56e0\u6b64\u4e0a\u9762\u7684\u4f8b\u5b50\u4f7f\u7528firstRow\u4e5f\u53ef\u4ee5\u3002<\/p>\n<p>\u6700\u540e\uff0cJDBC\u53ef\u4ee5\u4ece\u8fde\u63a5connection\u4e2d\u83b7\u53d6\u8fde\u63a5\u76f8\u5173\u5143\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u5f0f\u83b7\u53d6\u8fde\u63a5\u5143\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-java\">def md = sql.connection.metaData\nassert md.driverName == &#039;HSQL Database Engine Driver&#039;\nassert md.databaseProductVersion == &#039;2.3.2&#039;\nassert [&#039;JDBCMajorVersion&#039;, &#039;JDBCMinorVersion&#039;].collect{ md[it] } == [4, 0]\nassert md.stringFunctions.tokenize(&#039;,&#039;).contains(&#039;CONCAT&#039;)\ndef rs = md.getTables(null, null, &#039;AUTH%&#039;, null)\nassert rs.next()\nassert rs.getString(&#039;TABLE_NAME&#039;) == &#039;AUTHOR&#039;<\/code><\/pre>\n<p>\u5177\u4f53\u8fd8\u8bf7\u53c2\u8003javadoc\u770b\u770b\u80fd\u4ece\u5143\u6570\u636e\u4e2d\u80fd\u591f\u83b7\u53d6\u5230\u7684\u4fe1\u606f\u3002<\/p>\n<h2>\u6570\u636e\u5e93\u64cd\u4f5c\u4e4b\u547d\u540d\u53c2\u6570\u548c\u540d\u79f0\u5e8f\u53f7\u53c2\u6570<\/h2>\n<p>Groovy\u652f\u6301SQL\u5360\u4f4d\u7b26\u7684\u51e0\u79cd\u6269\u5c55\u65b9\u5f0f\u3002\u5f88\u591a\u65f6\u5019GString\u7684\u6269\u5c55\u5bf9\u6211\u4eec\u6765\u8bf4\u5df2\u7ecf\u8db3\u591f\uff0c\u4f46\u662f\u5f53\u9700\u8981\u4e0ejava\u96c6\u6210\u6216\u8005\u89c9\u5f97GString\u4e0d\u591f\u8f7b\u91cf\u7ea7\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u5360\u4f4d\u7b26\u6269\u5c55\u3002\u547d\u540d\u53c2\u6570\u8ddfSQL\u8bed\u53e5+\u53c2\u6570\u5217\u8868\u7684\u65b9\u5f0f\u7c7b\u4f3c\uff0c\u4e0d\u540c\u7684\u662f\uff0c\u5360\u4f4d\u7b26\u4e0d\u662f\u4e00\u4e32\u7684<code>?<\/code>\u5b57\u7b26\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>:propName<\/code>\u6216\u8005<code>?.propName<\/code>\u7684\u5f62\u5f0f\u5360\u4f4d\u7b26\uff0c\u4ee5\u53ca\u4f7f\u7528map\u5bf9\u8c61\u3001\u547d\u540d\u53c2\u6570\u3001\u666e\u901a\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002map\u5bf9\u8c61\u548c\u666e\u901a\u5bf9\u8c61\u5fc5\u987b\u6709propName\u5c5e\u6027\u4e0e\u5360\u4f4d\u7b26\u53c2\u6570\u540d\u79f0\u76f8\u5bf9\u5e94\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5192\u53f7\u5f62\u5f0f\u53c2\u6570\u5360\u4f4d\u7b26\u7684\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-java\">sql.execute &quot;INSERT INTO Author (firstname, lastname) VALUES (:first, :last)&quot;, first: &#039;Dierk&#039;, last: &#039;Koenig&#039;<\/code><\/pre>\n<p>\u95ee\u53f7\u5f00\u5934\u5f62\u5f0f\u7684\u53c2\u6570\u5360\u4f4d\u7b26\u7684\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-java\">sql.execute &quot;INSERT INTO Author (firstname, lastname) VALUES (?.first, ?.last)&quot;, first: &#039;Jon&#039;, last: &#039;Skeet&#039;<\/code><\/pre>\n<p>\u5982\u679c\u53c2\u6570\u4fe1\u606f\u8de8\u8d8a\u591a\u4e2amap\u5bf9\u8c61\u6216\u8005\u57df\u5bf9\u8c61(domain object)\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u7528<code>?<\/code>\u5f00\u5934\u5f62\u5f0f\u7684\u5360\u4f4d\u7b26\u914d\u5408\u53c2\u6570\u987a\u5e8f\u6570\uff0c\u5982\u4e0b\u4f8b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-java\">class Rockstar { String first, last }\ndef pogo = new Rockstar(first: &#039;Paul&#039;, last: &#039;McCartney&#039;)\ndef map = [lion: &#039;King&#039;]\nsql.execute &quot;INSERT INTO Author (firstname, lastname) VALUES (?1.first, ?2.lion)&quot;, pogo, map<\/code><\/pre>\n<h2>\u6570\u636e\u5e93\u64cd\u4f5c\u4e4b\u5b58\u50a8\u8fc7\u7a0b\u8c03\u7528<\/h2>\n<p>\u4e0d\u540c\u7684\u6570\u636e\u5e93\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\u6216\u51fd\u6570\u7684\u65b9\u5f0f\u7565\u6709\u4e0d\u540c\u3002\u5bf9\u4e8eHSQLDB\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u4e0b\u9762\u90a3\u6837\u521b\u5efa\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u5e93\u8868\u4e2d\u6240\u6709\u4f5c\u8005\u3002<\/p>\n<pre><code class=\"language-java\">sql.execute &quot;&quot;&quot;\n  CREATE FUNCTION SELECT_AUTHOR_INITIALS()\n  RETURNS TABLE (firstInitial VARCHAR(1), lastInitial VARCHAR(1))\n  READS SQL DATA\n  RETURN TABLE (\n    SELECT LEFT(Author.firstname, 1) as firstInitial, LEFT(Author.lastname, 1) as lastInitial\n    FROM Author\n  )\n&quot;&quot;&quot;<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528SQL\u7684CALL\u8bed\u53e5\u6267\u884c\u51fd\u6570\u83b7\u53d6\u6570\u636e<\/p>\n<pre><code class=\"language-java\">def result = []\nsql.eachRow(&#039;CALL SELECT_AUTHOR_INITIALS()&#039;) {\n  result &lt;&lt; &quot;$it.firstInitial$it.lastInitial&quot;\n}\nassert result == [&#039;DK&#039;, &#039;JS&#039;, &#039;GL&#039;]<\/code><\/pre>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u521b\u5efa\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2alastname\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<p>\u521b\u5efa\u5e26\u6709\u53c2\u6570\u7684\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-java\">sql.execute &quot;&quot;&quot;\n  CREATE FUNCTION FULL_NAME (p_lastname VARCHAR(64))\n  RETURNS VARCHAR(100)\n  READS SQL DATA\n  BEGIN ATOMIC\n    DECLARE ans VARCHAR(100);\n    SELECT CONCAT(firstname, &#039; &#039;, lastname) INTO ans\n    FROM Author WHERE lastname = p_lastname;\n    RETURN ans;\n  END\n&quot;&quot;&quot;<\/code><\/pre>\n<p>\u53ef\u4ee5\u4f7f\u7528\u5360\u4f4d\u7b26\u6307\u5b9a\u51fd\u6570\u7684\u53c2\u6570\u548c\u6307\u5b9a\u51fd\u6570\u7ed3\u679c\u7684\u5b58\u653e\u4f4d\u7f6e:<\/p>\n<p>\u4f7f\u7528\u5e26\u6709\u53c2\u6570\u7684\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u7b2c\u4e00\u4e2a\u95ee\u53f7\u6307\u5b9a\u7ed3\u679c\u7684\u4f4d\u7f6e\uff0c\u7b2c\u4e8c\u4e2a\u95ee\u53f7\u6307\u5b9a\u53c2\u6570\u7684\u4f4d\u7f6e\ndef result = sql.firstRow(&quot;{? = call FULL_NAME(?)}&quot;, [&#039;Koenig&#039;])\nassert result[0] == &#039;Dierk Koenig&#039;<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u8f93\u5165\u548c\u8f93\u51fa\u53c2\u6570\u7684\u5b58\u50a8\u8fc7\u7a0b<\/p>\n<pre><code class=\"language-java\">sql.execute &quot;&quot;&quot;\n  CREATE PROCEDURE CONCAT_NAME (OUT fullname VARCHAR(100),\n    IN first VARCHAR(50), IN last VARCHAR(50))\n  BEGIN ATOMIC\n    SET fullname = CONCAT(first, &#039; &#039;, last);\n  END\n&quot;&quot;&quot;<\/code><\/pre>\n<p>\u4f7f\u7528<code>CONCAT_NAME<\/code>\u5b58\u653e\u5b58\u50a8\u8fc7\u7a0b\u53c2\u6570\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6bd4\u8f83\u7279\u522b\u7684\u8c03\u7528\u65b9\u5f0f\u3002\u4efb\u4f55\u7684\u8f93\u5165\u53c2\u6570\u90fd\u88ab\u770b\u505a\u662f\u5b58\u50a8\u8fc7\u7a0b\u7684\u8c03\u7528\u53c2\u6570\u3002\u5bf9\u4e8e\u8f93\u51fa\u53c2\u6570\uff0c\u53ef\u4ee5\u50cf\u4e0b\u9762\u7684\u65b9\u5f0f\u8f93\u51fa\uff1a<\/p>\n<pre><code class=\"language-java\">sql.call(&quot;{call CONCAT_NAME(?, ?, ?)}&quot;, [Sql.VARCHAR, &#039;Dierk&#039;, &#039;Koenig&#039;]) {\n  fullname -&gt; assert fullname == &#039;Dierk Koenig&#039;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u5e93\u64cd\u4f5c\u4e4b\u4e8b\u52a1\u5904\u7406 \u6700\u7b80\u5355\u7684\u5b9e\u73b0\u6570\u636e\u5e93\u4e8b\u52a1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528withTransaction\u95ed\u5305\u5c06\u6570\u636e\u5e93\u64cd\u4f5c\u5305\u542b\u5728\u8be5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[568],"tags":[],"class_list":["post-2239","post","type-post","status-publish","format-standard","hentry","category-groovy"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2239"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2239\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}