数据集 (DataSet) groovy

简化数据操纵

任何想简化 JDBC 编程的 API 或工具最好有一些好的数据操纵特性,在这一节中,我要向您再介绍三个。

数据集 (DataSet)

构建于 GroovySql 简单性的基础之上,GroovySql 支持 DataSet类型的概念,这基本上是数据库表的对象表示。使用 DataSet,您可以在行中遍历,也可以添加新行。实际上,用数据集是方便地表示表格的公共数据集合的方式。

但是,目前 GroovySql DataSet类型的不足之处是它们没有代表关系;它们只是与数据库表的一对一映射。在清单 10 中,我创建了一个来自 word表的 DataSet


清单 10. 用 GroovySql 创建数据集

 import groovy.sql.Sql 
 class GroovyDatasetsExample1{ 
  static void main(args) { 
    sql = Sql.newInstance("jdbc:mysql://localhost:3306/words", "words", 
          "words", "org.gjt.mm.mysql.Driver") 
    words = sql.dataSet("word") 
    words.each{ word | 
     println word.word_id + " " + word.spelling 
    } 
    words.add(word_id:"9999", spelling:"clerisy", part_of_speech:"Noun") 
  } 
 } 
原文地址:https://www.cnblogs.com/lexus/p/2570345.html