Groovy笔记(9)_GroovySql

<p><span style="font-size: medium;"><strong>GroovySql概述</strong></span></p>
<p></p>
<p></p>
<p>1、GSql结合利用闭包和迭代器,把资源管理的负担转移到Groovy框架本身,从而简化JDBC编程</p>
<p>2、不用关闭Conne,也不用关闭ResultSet</p>
<p>3、Groovy把迭代器编程隐含的,使用起来更简单。</p>
<p> def coll = ["groovy","grails","java"]</p>
<p>coll.each{tiem -&gt; println item}</p>
<p></p>
<p></p>
<p></p>
<p><span style="font-size: medium;"><strong>建库建表</strong></span></p>
<p></p>
<p>1、创建MySQL数据库groovytoysstore</p>
<p> show databases;</p>
<p> create database groovysstore</p>
<p> character set utf8</p>
<p></p>
<p>2、建表toys,包含toyName,unitPrice 两列</p>
<p> use groovytoysstore</p>
<p> create table toys(toyName varchar(50) not null,</p>
<p>unitPrice varchar(20))</p>
<p></p>
<p></p>
<p><span style="font-size: medium;"><strong>建立数据库连接</strong></span></p>
<p></p>
<p>1、import groovy.sql.*</p>
<p> def db = 'jdbc:mysql: //主机/数据库</p>
<p> ?useUnicode=true&amp;characterEncoding=UTF-8'</p>
<p>def user = 'root'</p>
<p> def password = 'clat'</p>
<p> def driver = 'com.mysql.jdbc.Driver'</p>
<p> </p>
<p> def sql = Sql.newInstahnce(db,user,password,driver)</p>
<p> // 需要把mysql的驱动jar包拷贝到项目lib目录下</p>
<p></p>
<p></p>
<p></p>
<p><span style="font-size: medium;"><strong>数据库CRUD</strong></span></p>
<p></p>
<p>1、sql.<span style="color: #0000ff;">execute</span>("delete from toys")</p>
<p> sql.execute("insert into toys values('toy1','100')")</p>
<p> sql.<span style="color: #0000ff;">eachRow</span>("select * from toys"){toy -&gt;</p>
<p> println '/t/t'+toy.toyName + '/t' +toy.unitPrice}</p>
<p></p>
<p>2、def searchToyName = "toy1"</p>
<p> sql.eachRow("select * from toys where toyName =<span style="color: #ff0000;">?</span>",[<span style="color: #0000ff;">searchToyName</span>]){toy -&gt; </p>
<p>println '/t/t' + toy.toyName +'/t' +toy.unitPrice}</p>
<p></p>
<p>3、def updateToyName = 'toy1'</p>
<p> def updateUnitPrice = '1000'</p>
<p> sql.execute("update toys set unitPrice= ? where toyName =<span style="color: #ff0000;">?</span> ",[<span style="color: #0000ff;">updateUnitPrice</span>,<span style="color: #0000ff;">updateToyName</span>])</p>
<p></p>
<p></p>
<p><span style="font-size: medium;"><strong>操作DataSet(简化操作)</strong></span></p>
<p></p>
<p>1、<span style="color: #ff0000;">DataSet</span>类是Sql类的子类(可以用Jad查看)</p>
<p>2、def toys = sql.<span style="color: #ff0000;">dataSet</span>('toys')</p>
<p> //dataSet()参数是表名</p>
<p> public DataSet dataSet(String tableName){</p>
<p> return new DataSet(this, talbe);</p>
<p> } </p>
<p>3、toys.add(toyName:'toy8',unitPrice:'800')</p>
<p> //public void add(Map values) throws SQLException</p>
<p>4、toys.each{toy -&gt;</p>
<p> println toy.toyName + ':' + toy.unitPrice}</p>
<p></p>
<p>5、例子</p>
<p> def sql = Sql.newInstance(db,user,password,driver)</p>
<p> def toys = sql.<span style="color: #ff0000;">dataSet</span>('toys')</p>
<p> def list = toys.<span style="color: #0000ff;">rows</span>() //返回列表</p>
<p> println list.class</p>
<p> list.each{ //遍历列表</p>
<p> println it</p>
<p> }</p>
<p> def t = toys.<span style="color: #0000ff;">firstRow</span>() //获取第一行,lastRow()最后一行</p>
<p> println t</p>
<p></p>
<p></p>
<p></p>
<p><span style="font-size: medium;"><strong>简单OR Mapping</strong></span></p>
<p><span style="font-size: medium;"><strong></strong></span></p>
<p></p>
<p>1、SqlQuery抽象类(使用模板方法模式)</p>
<p> abstract class SqlQuery{</p>
<p> def sql ; def query</p>
<p> def SqlQuery(sql,query){</p>
<p> this.sql = sql; this.query = query</p>
<p> }</p>
<p><span style="color: #ff0000;"> def abstract mapRow(row)</span> //映射方法先定义,由子类实现</p>
<p>def execute(){</p>
<p>def rowsList = sql.rows(query)</p>
<p>def results = []</p>
<p> def size = rowsList.size()</p>
<p> 0.upto(size - 1){ index -&gt;</p>
<p> results &lt;&lt; this.<span style="color: #ff0000;">mapRow</span>(rowsList[index])</p>
<p> }</p>
<p>return results</p>
<p> }</p>
<p> }</p>
<p>2、class ToyQuery extends SqlQuery{ //子类实现mapRow()方法</p>
<p>def ToyQuery(sql){</p>
<p> super(sql,'select * from toys')</p>
<p> }</p>
<p>def mapRow(row){</p>
<p> def acc = new Toy(toyName: row.getProperty("toyName"),</p>
<p>unitPrice: row.getProperty("unitPrice"))</p>
<p> return acc</p>
<p> }</p>
<p> }</p>
<p></p>
<p>3、使用上面的类 完成简单ORM, 数据库 -》 对象</p>
<p></p>
<p> def db = 'jdbc:mysql://localhost/groovytoysstore?useUnicode=true&amp;characterEncoding=UTF-8'</p>
<p>def user= 'root'</p>
<p>def password = 'clat'</p>
<p>def driver = 'com.myql.jdbc.Driver'</p>
<p>def sql = Sql.newInstance(db,user,password,driver)</p>
<p> </p>
<p>def toyQuery = new ToyQuery(sql)</p>
<p> def toys= toyQuery.execute()</p>
<p>toys.each{ toy -&gt; println toy.toyName}</p>
<p></p>
<p></p>
<p></p>

你可能感兴趣的:(groovy)