简单地说,Groovy 是下一代的java语言,跟java一样,它也运行在 JVM 中。
作为跑在JVM中的另一种语言,groovy语法与 Java 语言的语法很相似。同时,Groovy 抛弃了java烦琐的文法。
同样的语句,使用groovy能在最大限度上减少你的击键次数――这确实是“懒惰程序员们”的福音。
how to run the groovy using eclipse IDE:
1.
download the groovy plug-in for eclipse and put then under the related directory.
2.
create a java pro
ject and right-click the pro
ject,
SELEct 'groovy' then choose 'add groovy nature'. this operation let this java pro
ject support the groovy;
3.
aiming to separate the java class
and the groovy class,we can create co
m.java,co
m.groovy for related classe
s.
create groovy class:
new-->others-->groovy-->groovy clas
s.
follow the above steps,we can create a groovy clas
s.
4.
run the groovy:
just like to run java clas
s.
(2)
examples:
public static void main(def args)
{
@R_
450_5179@ println "
Hello Word !"
@R_
450_5179@ def var="
Hello world"+"groovy";
@R_
450_5179@ println var;
@R_
450_5179@ println var.class;
@R_
450_5179@ var=100;
@R_
450_5179@ println var.class;
@R_
450_5179@
String nullValue
//?操作符时刻都非常有用,可以极大地减少条件语句。若不为null,则会执行
后面的
@R_
450_5179@ nullValue?.toLowerCase()
@R_
450_5179@ println "-----------------------------"
@R_
450_5179@
@R_
450_5179@ def hw=new
HelloWorld(
);
@R_
450_5179@ hw.show1("
Hello")
@R_
450_5179@ hw.show1("world",2)
@R_
450_5179@ hw.show2("test",2)
//@R_
450_5179@ hw.connectDB(6)
@R_
450_5179@
@R_
450_5179@ println "-----------闭包,常用于循环------------------"
@R_
450_5179@ def list=['a','b','c']
@R_
450_5179@ list.each
{println it}
@R_
450_5179@ def map=['name':'john','age'
:14,'sex':'boy']
@R_
450_5179@ map=map+['weight':2
5] //
添加john的体重
@R_
450_5179@ ma
p.put('length',1.27) //
添加john的身高
@R_
450_5179@ map.father='Keller'@R_
450_5179@ //
添加john的父亲
@R_
450_5179@
@R_
450_5179@ println map['name']
@R_
450_5179@ println map['length']
@R_
450_5179@ map.each
{key,value-> println "$key:$value"}
@R_
450_5179@ println "-----------------------------"
@R_
450_5179@
}
def show1(val,count=3)
{
@R_
450_5179@ for(i in 0..<count) //compare with the later method(show2)
@R_
450_5179@
{
@R_
450_5179@ println i
@R_
450_5179@ println "this is a $
{i} test: $
{val} !";
@R_
450_5179@ }
}
def show2(val,count)
{
@R_
450_5179@ for(i in 0..count)
@R_
450_5179@
{
@R_
450_5179@ println "this is a"+i+" test: "+val;
@R_
450_5179@ }
}
def connectDB(id)
{
@R_
450_5179@ def url = "jdbc:
Oracle:thin:@10.50.74.117
:1521:mssapp"
@R_
450_5179@ def driver = "
Oracle.jdbc.driver.
OracleDriver"
@R_
450_5179@ def user = "dev"
@R_
450_5179@ def
password = "dev"
@R_
450_5179@
@R_
450_5179@ def
sql =
sql.newInstance(url,user,
password,driver)
@R_
450_5179@
sql.execute("insert into
test_account values('5','account5')")@R_
450_5179@
@R_
450_5179@
sql.execute("insert into
test_account values('7','account7')")
@R_
450_5179@
sql.execute("insert into
test_account values($
{i
D},'account6')")
@R_
450_5179@
sql.execute("
update
test_account set ACCOUNTNAME=? WHERE AC_ID=?",["herry Test",id])
@R_
450_5179@
sql.execute("
delete from
test_account where AC_ID>4")
@R_
450_5179@ def row =
sql.firstRow("
SELECT
COUNT(*) AS totalRECORD FROM
test_ACCOUNT")
@R_
450_5179@ println row.@R_635_1
0586@LRECORD
@R_
450_5179@
@R_
450_5179@ //使用groovy的隐含变量 it(它恰好就是迭代器的实例)
@R_
450_5179@
sql.eachRow("
SELECT
* FROM tEST_ACCOUNT")
@R_
450_5179@
{
@R_
450_5179@ println it.AC_ID
@R_
450_5179@ println it.ACCOUNTNAME
@R_
450_5179@ }
@R_
450_5179@
}
}
-------------------------------------------------------------------------------------------
public class PersonInfo{
//对于javabean各
属性,
默认为private(不同于groovy平时的
默认设置:平时
默认为publi
C)且
自动生成setter,getter.
//对于单独的
方法,如toShow(),
默认仍为public
Integer persionId
String name
Double weight
public
String pas;
def addr;
String toShow()
{
@R_
450_5179@ return " persionId -- $
{persionI
D} \n name -- $
{name}"
}
}
-------------------------------------------------------------------------------------------
package co
m.java;
import co
m.test.groovy.
HelloWorld;
import co
m.test.groovy.PersonInfo;
public class TestGroovy
{
public static void main(
String[] args)
{
@R_
450_5179@
HelloWorld hw=new
HelloWorld(
);
@R_
450_5179@ hw.show1("tes
Ting in java"
);
@R_
450_5179@
@R_
450_5179@ PersonInfo pi=new PersonInfo(
);
@R_
450_5179@ pi.setPersionId(123
);
@R_
450_5179@ pi.setName("tom"
);
@R_
450_5179@ pi.setWeight(120.0
);
@R_
450_5179@ Sy
stem.out.println(pi.getName()
);
@R_
450_5179@ Sy
stem.out.println(pi.getWeight()
);
@R_
450_5179@ Sy
stem.out.println(pi.toShow()
);
}
publi
c int plus(int i,int y)
{
@R_
450_5179@ return i+y;
}
}
references:
基础:
与 java整合:
与 ssh等的结合.