Groovy简明学习

Groovy简明学习

本文同步发表在

http://www.simonme.org/?p=212

介绍

写起来很快,与java无缝兼容,网上对其性能担忧。

目前Gradle是基于他的。web框架Grails也是基于他的。成熟项目,目前github上有linkedin的glu https://github.com/linkedin/glu

变量的定义

def parser = new SSQHistoryTxtParser();

方法的定义类似变量

def methodName(arg1)

对文件的处理

new File(filePath).eachLine

        {

            line ->

            def result = processEveryLine(line);

 

            // 使用sql语句插入数据库

            insert2DB(result);

        };

 注意line ->

是指将groovy内置迭代后的文件行变量用line名取代

sql相关操作数据库查看

Database features

http://groovy.codehaus.org/Database+features

注意要自行关系连接,不同于groovy中的文件句柄

groovy要自行关闭数据库连接

http://stackoverflow.com/questions/1895374/with-groovy-sql-newinstance-in-grails-who-closes-the-connection-and-when

groovy中类的特性

自动生成支持构造函数

POJO类自动对属性生成get set方法

具体细节可以参见官方文档或参见 Groovy中的类

其他

Java的语法在其中都能使用

集合与映射

集合:方括号括起来并用逗号将元素分隔的

形如:def coll = ["Groovy", "Java", "Ruby"]

映射就是键值对的集合,键值对用分号分开

形如:def hash = [name:"Andy", "VPN-#":45]

http://www.ibm.com/developerworks/cn/education/java/j-groovy/section11.html

参考文章

官网

http://groovy.codehaus.org/

精通 Groovy   

http://www.ibm.com/developerworks/cn/education/java/j-groovy/index.html

Input Output

http://groovy.codehaus.org/Input+Output

Database features

http://groovy.codehaus.org/Database+features

groovy API

http://groovy.codehaus.org/api/index.html

groovy中对String的扩展方法

http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/StringGroovyMethods.html

与Java的对比

http://groovy.codehaus.org/Differences+from+Java

Groovy示例

http://groovy.codehaus.org/Cookbook+Examples

groovy要自行关闭数据库连接

http://stackoverflow.com/questions/1895374/with-groovy-sql-newinstance-in-grails-who-closes-the-connection-and-when

原文地址:https://www.cnblogs.com/simoncook/p/2937911.html