???????????????Groovy???????CRUD?????????groovy.sql???????????????????H2??
????1.?????????????
????<span style="font-family:'????'?? SimSun;font-size:16px;">//?????????????<br>def db = [<br>    url:'jdbc:h2:mem:groovy'??<br>    user:'root'??<br>    password:'root'??<br>    driver:'org.h2.Driver'<br>];<br></span>
????2.??????????????????????Groovy??Sql??
????<span style="font-family:'????'?? SimSun;font-size:16px;">//?????????????<br>def sql = Sql.newInstance(db.url?? db.user?? db.password?? db.driver);<br></span>
????3.??????????
????<span style="font-size:14px;font-family:'????'?? SimSun;">//??????????<br>sql.execute('''<br>    CREATE TABLE account(<br>        id integer NOT NULL??<br>        name varchar(20)??<br>        url varchar(100)<br>    )<br>''');<br> </span>
?????????groovy.sql.Sql???execute??????????SQL???????????groovy?д??????account??
????4.??????????д????????????д???????
????<span style="font-family:'????'?? SimSun;font-size:16px;">//д??????<br>def datas=[<br>   [100?? 'Jack'?? 'http://www.jack.net']?? <br>   [101?? 'Groovy'?? 'http://groovy.com']?? <br>   [102?? 'Apache'?? 'http://apache.org']<br>];<br>datas.each { param-><br>    sql.execute('INSERT INTO account(id?? name?? url) values(???????)'?? param);<br>}<br>println('Insert After:');<br>sql.eachRow('SELECT id?? name?? url FROM account') { row-><br>    printf('|%d|%s|%s| '?? row.id?? row.name?? row.url);<br>}<br></span>
??????4?е???????????????account??д??3???????????????????????????????????????????????Groovy??????????б????????
??????????????????
????<span style="font-family:'????'?? SimSun;font-size:16px;">Insert After:<br>|100|Jack|http://www.jack.net|<br>|101|Groovy|http://groovy.com|<br>|102|Apache|http://apache.org|<br></span>
????5.???????
????<span style="font-family:'????'?? SimSun;font-size:16px;">//????????????<br>def rs=sql.firstRow('SELECT * FROM account');<br>println('Query First Row:');<br>println(rs);<br></span>
????Groovy??Sql???????????????????????μ?Groovy??API????????5?е?????????????????????????????GroovyRowReasult?????????Map?????????????????Groovy??Map???????????£?
????<span style="font-family:'????'?? SimSun;font-size:16px;">Query First Row:<br>[ID:100?? NAME:ZhangSan?? URL:http://aiilive.blog.51cto.com]<br></span>
????6.????????
????<span style="font-family:'????'?? SimSun;font-size:16px;">def name='ZhangSan';<br>def url='http://aiilive.blog.51cto.com';<br>sql.executeUpdate("UPDATE account SET name=$name?? url=$url where id=100");<br>println('Update After:');<br>sql.eachRow('SELECT id?? name?? url FROM account') { row-><br>    printf('|%d|%s|%s| '?? row.id?? row.name?? row.url);<br>}<br></span>
????7.???????
????<span style="font-family:'????'?? SimSun;font-size:16px;">//????????????????<br>name='Groovy';<br>sql.executeUpdate('DELETE FROM account WHERE name = ?'?? [name]);<br>name='Apache';<br>sql.execute('DELETE FROM account WHERE name=:name'?? ['name':name]);<br>println('Delete After:');<br>sql.eachRow('SELECT id?? name?? url FROM account') { row-><br>    printf('|%d|%s|%s| '?? row.id?? row.name?? row.url);<br>}<br></span>
????8.???DataSet??????????
????DataSet??Sql????????????DataSet????????????????в?????????????
????<span style="font-family:'????'?? SimSun;font-size:16px;">def account=sql.dataSet('account');<br><br>account.add([id:103?? name:'h2'?? url:'http://h2.org']);<br><br>name='51cto';<br>url='http://www.51cto.com';<br>account.add([id:104?? name:name?? url:url]);<br><br>println('DataSet Update After');<br>account.eachRow('SELECT id?? name?? url FROM account') { row-><br>    printf('|%d|%s|%s| '?? row.id?? row.name?? row.url);<br>};<br>def accountRows=account.rows();<br>accountRows.each { row-><br>    printf('|%d|%s|%s| '?? row.id?? row.name?? row.url);<br>}<br></span>
????????dataSet?????account?????????е??????account?????DataSet???????????add????????????????????????????rows???????????????е????м???????rows??????????????????????????
???????????groovy.sql??????API???????????????????????e??е???????????????????????????????????????????????????????????????Spring?????????????