加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

groovy basic knowledge

发布时间:2020-12-14 17:05:58 所属栏目:大数据 来源:网络整理
导读:简单地说,Groovy 是下一代的java语言,跟java一样,它也运行在 JVM 中。 作为跑在JVM中的另一种语言,groovy语法与 Java 语言的语法很相似。同时,Groovy 抛弃了java烦琐的文法。 同样的语句,使用groovy能在最大限度上减少你的击键次数 ――这确实是“懒惰
简单地说,Groovy 是下一代的java语言,跟java一样,它也运行在 JVM 中。
作为跑在JVM中的另一种语言,groovy语法与 Java 语言的语法很相似。同时,Groovy 抛弃了java烦琐的文法。 同样的语句,使用groovy能在最大限度上减少你的击键次数――这确实是“懒惰程序员们”的福音。
?
(1)
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 project and right-click the project,select 'groovy' then choose 'add groovy nature'. this operation let this java project support the groovy;
3.
aiming to separate the java class and the groovy class,we can create com.java,com.groovy for related classes.
create groovy class:
new-->others-->groovy-->groovy class.
follow the above steps,we can create a groovy class.
4.
run the groovy:
just like to run java class.
?
(2)
examples:
?
package com.test.groovy
import groovy.sql.Sql;
public class HelloWorld{
??? public static void main(def args){
??????? println "Hello Word !"
??????? def var="Hello world"+"groovy";
??????? println var;
??????? println var.class;
??????? var=100;
??????? println var.class;
??????? String nullValue
//?操作符时刻都非常有用,可以极大地减少条件语句。若不为null,则会执行后面的
??????? nullValue?.toLowerCase()
??????? println "-----------------------------"
???????
??????? def hw=new HelloWorld();
??????? hw.show1("hello")
??????? hw.show1("world",2)
??????? hw.show2("test",2)
//??????? hw.connectDB(6)
???????
??????? println "-----------闭包,常用于循环------------------"
??????? def list=['a','b','c']
??????? list.each{println it}
??????? def map=['name':'john','age':14,'sex':'boy']
??????? map=map+['weight':25]?????? //添加john的体重
??????? map.put('length',1.27)????? //添加john的身高
??????? map.father='Keller'???????? //添加john的父亲
???????
??????? println map['name']
??????? println map['length']
??????? map.each{key,value-> println "$key:$value"}
??????? println "-----------------------------"
???????
??? }
???
??? def show1(val,count=3)
??? {
??????? for(i in 0..<count) //compare with the later method(show2)
??????? {
??????????? println i
??????????? println "this is a ${i} test: ${val} !";
??????? }
??? }
???
??? def show2(val,count)
??? {
??????? for(i in 0..count)
??????? {
??????????? println "this is a"+i+" test: "+val;
??????? }
??? }
???
??? def connectDB(id)
??? {
??????? def url = "jdbc:oracle:thin:@10.50.74.117:1521:mssapp"
??????? def driver = "oracle.jdbc.driver.OracleDriver"??
??????? def user = "dev"??
??????? def password = "dev"
???????
??????? def sql = Sql.newInstance(url,user,password,driver)
??????? sql.execute("insert into test_account values('5','account5')")????????????
??????? sql.execute("insert into test_account values('7','account7')")???
??????? sql.execute("insert into test_account values(${id},'account6')")???
??????? sql.execute("update test_account set ACCOUNTNAME=? WHERE AC_ID=?",["herry Test",id])?
??????? sql.execute("delete from test_account where AC_ID>4")
??????? def row = sql.firstRow("SELECT COUNT(*) AS TOTALRECORD FROM TEST_ACCOUNT")
??????? println row.TOTALRECORD
???????
??????? //使用groovy的隐含变量 it(它恰好就是迭代器的实例)
??????? sql.eachRow("SELECT * FROM TEST_ACCOUNT")
??????? {
??????????? println it.AC_ID
??????????? println it.ACCOUNTNAME
??????? }
???????
??? }
}
need lib:? classes12.jar
-------------------------------------------------------------------------------------------
package com.test.groovy
public class PersonInfo{
??? //对于javabean各属性,默认为private(不同于groovy平时的默认设置:平时默认为public)且自动生成setter,getter.
??? //对于单独的方法,如toShow(),默认仍为public
??? Integer persionId
??? String name
??? Double weight
??? public String pas;
??? def addr;
???
??? String toShow()
??? {
??????? return " persionId -- ${persionId} n name -- ${name}"
??? }
}
-------------------------------------------------------------------------------------------
package com.java;
import com.test.groovy.HelloWorld;
import com.test.groovy.PersonInfo;
public class TestGroovy {
??? public static void main(String[] args) {
??????? HelloWorld hw=new HelloWorld();
??????? hw.show1("testing in java");
???????
??????? PersonInfo pi=new PersonInfo();
??????? pi.setPersionId(123);
??????? pi.setName("tom");
??????? pi.setWeight(120.0);
??????? System.out.println(pi.getName());
??????? System.out.println(pi.getWeight());
??????? System.out.println(pi.toShow());
??? }
???
??? public int plus(int i,int y)
??? {
??????? return i+y;
??? }
}
?
references:
基础:
http://blog.csdn.net/kmyhy/archive/2009/05/19/4200563.aspx
与 java整合:
http://sheng8407-sina-com.javaeye.com/blog/368079
实战 Groovy: 用 Groovy 进行 JDBC 编程
http://www.ibm.com/developerworks/cn/java/j-pg01115.html
用 Groovy 减少代码冗余
http://www.ibm.com/developerworks/cn/java/j-pg09196.html
与 ssh等的结合.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读