遵循“Groovy輕松入門系列”的一貫做法,下面還是以一個實例來說明GORM DSL到底是怎么回事,它會帶給我們多大的便利。
1,執行‘grails create-app GormDslDemo’,創建一個名為GormDslDemo的Grails項目
D:\_DEV\grails_apps>grails?create-app?GormDslDemo
Welcome?to?Grails?1.0.2?-?http://grails.org/
Licensed?under?Apache?Standard?License?2.0
Grails?home?is?set?to:?D:\D\MY_DEV\grails-1.0.2
Base?Directory:?D:\_DEV\grails_apps
Note:?No?plugin?scripts?found
Running?script?D:\D\MY_DEV\grails-1.0.2\scripts\CreateApp.groovy
Environment?set?to?development
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\src
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\src\java
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\src\groovy
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\controllers
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\services
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\domain
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\taglib
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\utils
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\views
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\views\layouts
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\i18n
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\conf
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\test
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\test\unit
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\test\integration
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\scripts
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\web-app
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\web-app\js
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\web-app\css
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\web-app\images
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\web-app\META-INF
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\lib
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\conf\spring
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\conf\hibernate
[propertyfile]?Creating?new?property?file:?D:\_DEV\grails_apps\GormDslDemo\application.properties
?????[copy]?Copying?2?files?to?D:\_DEV\grails_apps\GormDslDemo
?????[copy]?Copying?2?files?to?D:\_DEV\grails_apps\GormDslDemo\web-app\WEB-INF
?????[copy]?Copying?5?files?to?D:\_DEV\grails_apps\GormDslDemo\web-app\WEB-INF\tld
?????[copy]?Copying?87?files?to?D:\_DEV\grails_apps\GormDslDemo\web-app
?????[copy]?Copying?17?files?to?D:\_DEV\grails_apps\GormDslDemo\grails-app
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo
[propertyfile]?Updating?property?file:?D:\_DEV\grails_apps\GormDslDemo\application.properties
Created?Grails?Application?at?D:\_DEV\grails_apps/GormDslDemo
D:\_DEV\grails_apps>
Welcome?to?Grails?1.0.2?-?http://grails.org/
Licensed?under?Apache?Standard?License?2.0
Grails?home?is?set?to:?D:\D\MY_DEV\grails-1.0.2
Base?Directory:?D:\_DEV\grails_apps
Note:?No?plugin?scripts?found
Running?script?D:\D\MY_DEV\grails-1.0.2\scripts\CreateApp.groovy
Environment?set?to?development
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\src
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\src\java
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\src\groovy
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\controllers
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\services
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\domain
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\taglib
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\utils
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\views
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\views\layouts
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\i18n
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\conf
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\test
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\test\unit
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\test\integration
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\scripts
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\web-app
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\web-app\js
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\web-app\css
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\web-app\images
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\web-app\META-INF
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\lib
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\conf\spring
????[mkdir]?Created?dir:?D:\_DEV\grails_apps\GormDslDemo\grails-app\conf\hibernate
[propertyfile]?Creating?new?property?file:?D:\_DEV\grails_apps\GormDslDemo\application.properties
?????[copy]?Copying?2?files?to?D:\_DEV\grails_apps\GormDslDemo
?????[copy]?Copying?2?files?to?D:\_DEV\grails_apps\GormDslDemo\web-app\WEB-INF
?????[copy]?Copying?5?files?to?D:\_DEV\grails_apps\GormDslDemo\web-app\WEB-INF\tld
?????[copy]?Copying?87?files?to?D:\_DEV\grails_apps\GormDslDemo\web-app
?????[copy]?Copying?17?files?to?D:\_DEV\grails_apps\GormDslDemo\grails-app
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo
[propertyfile]?Updating?property?file:?D:\_DEV\grails_apps\GormDslDemo\application.properties
Created?Grails?Application?at?D:\_DEV\grails_apps/GormDslDemo
D:\_DEV\grails_apps>
2,將數據庫JDBC驅動jar文件放到lib目錄下,比如我使用的數據庫是MySQL,那么JDBC驅動jar文件就是mysql-connector-java-5.1.5-bin.jar
3,配置數據源,將grails-app\conf\DataSource.groovy修改為(注意:假設您也是用MySQL,記得將用戶名和密碼改為自己的)
dataSource?{
????pooled?=?false
????//driverClassName?=?"org.hsqldb.jdbcDriver"
????driverClassName?=?"com.mysql.jdbc.Driver"
????//username?=?"sa"
????username?=?"root"
????//password?=?""
????password?=?"123"
}
hibernate?{
????cache.use_second_level_cache=true
????cache.use_query_cache=true
????cache.provider_class='org.hibernate.cache.EhCacheProvider'
}
//?environment?specific?settings
environments?{
????development?{
????????dataSource?{
????????????dbCreate?=?"create-drop"?//?one?of?'create',?'create-drop','update'
????????????//url?=?"jdbc:hsqldb:mem:devDB"
????????????url?=?"jdbc:mysql://localhost:3306/gorm_dsl_demo?createDatabaseIfNotExist=true"
????????}
????}
????test?{
????????dataSource?{
????????????dbCreate?=?"update"
????????????url?=?"jdbc:hsqldb:mem:testDb"
????????}
????}
????production?{
????????dataSource?{
????????????dbCreate?=?"update"
????????????url?=?"jdbc:hsqldb:file:prodDb;shutdown=true"
????????}
????}
}
????pooled?=?false
????//driverClassName?=?"org.hsqldb.jdbcDriver"
????driverClassName?=?"com.mysql.jdbc.Driver"
????//username?=?"sa"
????username?=?"root"
????//password?=?""
????password?=?"123"
}
hibernate?{
????cache.use_second_level_cache=true
????cache.use_query_cache=true
????cache.provider_class='org.hibernate.cache.EhCacheProvider'
}
//?environment?specific?settings
environments?{
????development?{
????????dataSource?{
????????????dbCreate?=?"create-drop"?//?one?of?'create',?'create-drop','update'
????????????//url?=?"jdbc:hsqldb:mem:devDB"
????????????url?=?"jdbc:mysql://localhost:3306/gorm_dsl_demo?createDatabaseIfNotExist=true"
????????}
????}
????test?{
????????dataSource?{
????????????dbCreate?=?"update"
????????????url?=?"jdbc:hsqldb:mem:testDb"
????????}
????}
????production?{
????????dataSource?{
????????????dbCreate?=?"update"
????????????url?=?"jdbc:hsqldb:file:prodDb;shutdown=true"
????????}
????}
}
4,創建domain class,在本例中,我們創建兩個domain class,沿用經典的Author和Book
創建Author
D:\_DEV\grails_apps\GormDslDemo>grails?create-domain-class?Author
Welcome?to?Grails?1.0.2?-?http://grails.org/
Licensed?under?Apache?Standard?License?2.0
Grails?home?is?set?to:?D:\D\MY_DEV\grails-1.0.2
Base?Directory:?D:\_DEV\grails_apps\GormDslDemo
Note:?No?plugin?scripts?found
Running?script?D:\D\MY_DEV\grails-1.0.2\scripts\CreateDomainClass.groovy
Environment?set?to?development
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo\grails-app\domain
Created??for?Author
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo\test\integration
Created?Tests?for?Author
D:\_DEV\grails_apps\GormDslDemo>
創建BookWelcome?to?Grails?1.0.2?-?http://grails.org/
Licensed?under?Apache?Standard?License?2.0
Grails?home?is?set?to:?D:\D\MY_DEV\grails-1.0.2
Base?Directory:?D:\_DEV\grails_apps\GormDslDemo
Note:?No?plugin?scripts?found
Running?script?D:\D\MY_DEV\grails-1.0.2\scripts\CreateDomainClass.groovy
Environment?set?to?development
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo\grails-app\domain
Created??for?Author
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo\test\integration
Created?Tests?for?Author
D:\_DEV\grails_apps\GormDslDemo>
D:\_DEV\grails_apps\GormDslDemo>grails?create-domain-class?Book
Welcome?to?Grails?1.0.2?-?http://grails.org/
Licensed?under?Apache?Standard?License?2.0
Grails?home?is?set?to:?D:\D\MY_DEV\grails-1.0.2
Base?Directory:?D:\_DEV\grails_apps\GormDslDemo
Note:?No?plugin?scripts?found
Running?script?D:\D\MY_DEV\grails-1.0.2\scripts\CreateDomainClass.groovy
Environment?set?to?development
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo\grails-app\domain
Created??for?Book
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo\test\integration
Created?Tests?for?Book
D:\_DEV\grails_apps\GormDslDemo>
Welcome?to?Grails?1.0.2?-?http://grails.org/
Licensed?under?Apache?Standard?License?2.0
Grails?home?is?set?to:?D:\D\MY_DEV\grails-1.0.2
Base?Directory:?D:\_DEV\grails_apps\GormDslDemo
Note:?No?plugin?scripts?found
Running?script?D:\D\MY_DEV\grails-1.0.2\scripts\CreateDomainClass.groovy
Environment?set?to?development
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo\grails-app\domain
Created??for?Book
?????[copy]?Copying?1?file?to?D:\_DEV\grails_apps\GormDslDemo\test\integration
Created?Tests?for?Book
D:\_DEV\grails_apps\GormDslDemo>
5,修改grails-app\domain目錄下的Author.groovy和Book.groovy:
Author.groovy
class?Author?{
????String?name
????static?hasMany?=?[books:?Book]
????static?mapping?=?{
????????table?'AUTHOR'
????????columns?{
????????????name?column:'AUTHOR_NAME'
????????}
????????id?generator:'increment',?column:'AUTHOR_ID_PK'
??????? version false
????}
}
????String?name
????static?hasMany?=?[books:?Book]
????static?mapping?=?{
????????table?'AUTHOR'
????????columns?{
????????????name?column:'AUTHOR_NAME'
????????}
????????id?generator:'increment',?column:'AUTHOR_ID_PK'
??????? version false
????}
}
Book.groovy
class?Book?{
????String?name
????Float?price
????Author?author
????static?mapping?=?{
????????table?'BOOK'
????????
????????columns?{
????????????name?column:'BOOK_NAME'
????????????price?column:'BOOK_PRICE'
????????????author?column:'AUTHOR_ID_FK'
????????}
????????id?generator:'increment',?column:'BOOK_ID_PK'
??????? version false
????}
}
????String?name
????Float?price
????Author?author
????static?mapping?=?{
????????table?'BOOK'
????????
????????columns?{
????????????name?column:'BOOK_NAME'
????????????price?column:'BOOK_PRICE'
????????????author?column:'AUTHOR_ID_FK'
????????}
????????id?generator:'increment',?column:'BOOK_ID_PK'
??????? version false
????}
}
對上面的mapping這個closure稍微解釋一下,以Book中的mapping為例,
??? static mapping = {
??????? table 'BOOK'??? // 為Book類生成的表名為BOOK,您可以將它改為其他名字,比如BOOK_TABLE等
???????
??????? columns {
??????????? name column:'BOOK_NAME'?????? // 表中與name屬性相對應的字段名為BOOK_NAME
??????????? price column:'BOOK_PRICE'???? // 類似地,表中與price屬性相對應的字段名為BOOK_PRICE
??????????? author column:'AUTHOR_ID_FK'? // 表中與author屬性相對應的字段名為AUTHOR_ID_FK,該字段是一個外鍵
??????? }
??????? id generator:'increment', column:'BOOK_ID_PK' // 指定id生成的策略為increment以及其對應的字段名為BOOK_ID_PK
??????? version false? // 不讓Grails自動生成version字段,該字段用來支持樂觀鎖,一般不建議將其去除,這里僅僅是為了演示
??? }
6, 執行'grails generate-all Author'和'grails generate-all Book'命令生成與Author和Book相關的CRUD的所有代碼和頁面。
D:\_DEV\grails_apps\GormDslDemo>grails?generate-all?Author
Welcome?to?Grails?1.0.2?-?http://grails.org/
Licensed?under?Apache?Standard?License?2.0
Grails?home?is?set?to:?D:\D\MY_DEV\grails-1.0.2
Base?Directory:?D:\_DEV\grails_apps\GormDslDemo
Note:?No?plugin?scripts?found
Running?script?D:\D\MY_DEV\grails-1.0.2\scripts\GenerateAll.groovy
Environment?set?to?development
????[mkdir]?Created?dir:?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\GormDslDemo\classes
??[groovyc]?Compiling?8?source?files?to?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\Gorm
DslDemo\classes
????[mkdir]?Created?dir:?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\GormDslDemo\resourc
es\grails-app\i18n
[native2ascii]?Converting?10?files?from?D:\_DEV\grails_apps\GormDslDemo\grails-app\i18n?to?C:\Docume
nts?and?Settings\Daniel\.grails\1.0.2\projects\GormDslDemo\resources\grails-app\i18n
?????[copy]?Copying?1?file?to?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\GormDslDemo\cl
asses
?????[copy]?Copying?1?file?to?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\GormDslDemo\re
sources
?????[copy]?Copying?1?file?to?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\GormDslDemo
[0]?spring.GrailsWebApplicationContext?Refreshing?org.codehaus.groovy.grails.commons.spring.GrailsWe
bApplicationContext@f4a376:?display?name?[org.codehaus.groovy.grails.commons.spring.GrailsWebApplica
tionContext@f4a376];?startup?date?[Fri?Apr?04?00:00:55?CST?2008];?root?of?context?hierarchy
[0]?spring.GrailsWebApplicationContext?Bean?factory?for?application?context?[org.codehaus.groovy.gra
ils.commons.spring.GrailsWebApplicationContext@f4a376]:?org.springframework.beans.factory.support.De
faultListableBeanFactory@101ac1c
Generating?views?for?domain?class?Author?
Generating?controller?for?domain?class?Author?
Finished?generation?for?domain?class?Author
Welcome?to?Grails?1.0.2?-?http://grails.org/
Licensed?under?Apache?Standard?License?2.0
Grails?home?is?set?to:?D:\D\MY_DEV\grails-1.0.2
Base?Directory:?D:\_DEV\grails_apps\GormDslDemo
Note:?No?plugin?scripts?found
Running?script?D:\D\MY_DEV\grails-1.0.2\scripts\GenerateAll.groovy
Environment?set?to?development
????[mkdir]?Created?dir:?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\GormDslDemo\classes
??[groovyc]?Compiling?8?source?files?to?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\Gorm
DslDemo\classes
????[mkdir]?Created?dir:?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\GormDslDemo\resourc
es\grails-app\i18n
[native2ascii]?Converting?10?files?from?D:\_DEV\grails_apps\GormDslDemo\grails-app\i18n?to?C:\Docume
nts?and?Settings\Daniel\.grails\1.0.2\projects\GormDslDemo\resources\grails-app\i18n
?????[copy]?Copying?1?file?to?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\GormDslDemo\cl
asses
?????[copy]?Copying?1?file?to?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\GormDslDemo\re
sources
?????[copy]?Copying?1?file?to?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\GormDslDemo
[0]?spring.GrailsWebApplicationContext?Refreshing?org.codehaus.groovy.grails.commons.spring.GrailsWe
bApplicationContext@f4a376:?display?name?[org.codehaus.groovy.grails.commons.spring.GrailsWebApplica
tionContext@f4a376];?startup?date?[Fri?Apr?04?00:00:55?CST?2008];?root?of?context?hierarchy
[0]?spring.GrailsWebApplicationContext?Bean?factory?for?application?context?[org.codehaus.groovy.gra
ils.commons.spring.GrailsWebApplicationContext@f4a376]:?org.springframework.beans.factory.support.De
faultListableBeanFactory@101ac1c
Generating?views?for?domain?class?Author?

Generating?controller?for?domain?class?Author?

Finished?generation?for?domain?class?Author
D:\_DEV\grails_apps\GormDslDemo>grails?generate-all?Book
Welcome?to?Grails?1.0.2?-?http://grails.org/
Licensed?under?Apache?Standard?License?2.0
Grails?home?is?set?to:?D:\D\MY_DEV\grails-1.0.2
Base?Directory:?D:\_DEV\grails_apps\GormDslDemo
Note:?No?plugin?scripts?found
Running?script?D:\D\MY_DEV\grails-1.0.2\scripts\GenerateAll.groovy
Environment?set?to?development
??[groovyc]?Compiling?2?source?files?to?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\Gorm
DslDemo\classes
[0]?spring.GrailsWebApplicationContext?Refreshing?org.codehaus.groovy.grails.commons.spring.GrailsWe
bApplicationContext@15863e4:?display?name?[org.codehaus.groovy.grails.commons.spring.GrailsWebApplic
ationContext@15863e4];?startup?date?[Fri?Apr?04?00:03:29?CST?2008];?root?of?context?hierarchy
[15]?spring.GrailsWebApplicationContext?Bean?factory?for?application?context?[org.codehaus.groovy.gr
ails.commons.spring.GrailsWebApplicationContext@15863e4]:?org.springframework.beans.factory.support.
DefaultListableBeanFactory@f4ca49
Generating?views?for?domain?class?Book?
Generating?controller?for?domain?class?Book?
Finished?generation?for?domain?class?Book
D:\_DEV\grails_apps\GormDslDemo>
Welcome?to?Grails?1.0.2?-?http://grails.org/
Licensed?under?Apache?Standard?License?2.0
Grails?home?is?set?to:?D:\D\MY_DEV\grails-1.0.2
Base?Directory:?D:\_DEV\grails_apps\GormDslDemo
Note:?No?plugin?scripts?found
Running?script?D:\D\MY_DEV\grails-1.0.2\scripts\GenerateAll.groovy
Environment?set?to?development
??[groovyc]?Compiling?2?source?files?to?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\Gorm
DslDemo\classes
[0]?spring.GrailsWebApplicationContext?Refreshing?org.codehaus.groovy.grails.commons.spring.GrailsWe
bApplicationContext@15863e4:?display?name?[org.codehaus.groovy.grails.commons.spring.GrailsWebApplic
ationContext@15863e4];?startup?date?[Fri?Apr?04?00:03:29?CST?2008];?root?of?context?hierarchy
[15]?spring.GrailsWebApplicationContext?Bean?factory?for?application?context?[org.codehaus.groovy.gr
ails.commons.spring.GrailsWebApplicationContext@15863e4]:?org.springframework.beans.factory.support.
DefaultListableBeanFactory@f4ca49
Generating?views?for?domain?class?Book?

Generating?controller?for?domain?class?Book?

Finished?generation?for?domain?class?Book
D:\_DEV\grails_apps\GormDslDemo>
7,啟動完MySQL數據庫之后,執行'grails run-app'命令,啟動web服務器。
D:\_DEV\grails_apps\GormDslDemo>grails?run-app
Welcome?to?Grails?1.0.2?-?http://grails.org/
Licensed?under?Apache?Standard?License?2.0
Grails?home?is?set?to:?D:\D\MY_DEV\grails-1.0.2
Base?Directory:?D:\_DEV\grails_apps\GormDslDemo
Note:?No?plugin?scripts?found
Running?script?D:\D\MY_DEV\grails-1.0.2\scripts\RunApp.groovy
Environment?set?to?development
??[groovyc]?Compiling?2?source?files?to?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\Gorm
DslDemo\classes
Running?Grails?application..
2008-04-04?00:06:07.861::INFO:??Logging?to?STDERR?via?org.mortbay.log.StdErrLog
2008-04-04?00:06:07.095::INFO:??jetty-6.1.4
2008-04-04?00:06:08.454::INFO:??No?Transaction?manager?found?-?if?your?webapp?requires?one,?please?c
onfigure?one.
2008-04-04?00:06:08.329:/GormDslDemo:INFO:??Set?web?app?root?system?property:?'GormDslDemo'?=?[D:\_D
EV\grails_apps\GormDslDemo\web-app\]
2008-04-04?00:06:08.345:/GormDslDemo:INFO:??Initializing?Log4J?from?[file:C:\Documents?and?Settings\
Daniel/.grails/1.0.2/projects/GormDslDemo/resources/log4j.properties]
2008-04-04?00:06:08.376:/GormDslDemo:INFO:??Initializing?Spring?root?WebApplicationContext
[0]?spring.GrailsWebApplicationContext?Refreshing?org.codehaus.groovy.grails.commons.spring.GrailsWe
bApplicationContext@c393a1:?display?name?[org.codehaus.groovy.grails.commons.spring.GrailsWebApplica
tionContext@c393a1];?startup?date?[Fri?Apr?04?00:06:12?CST?2008];?parent:?org.springframework.web.co
ntext.support.XmlWebApplicationContext@bd3b2d
[0]?spring.GrailsWebApplicationContext?Bean?factory?for?application?context?[org.codehaus.groovy.gra
ils.commons.spring.GrailsWebApplicationContext@c393a1]:?org.springframework.beans.factory.support.De
faultListableBeanFactory@144569b
2008-04-04?00:06:19.620:/GormDslDemo:INFO:??Initializing?Spring?FrameworkServlet?'grails'
2008-04-04?00:06:20.151::INFO:??Started?SelectChannelConnector@0.0.0.0:8080
Server?running.?Browse?to?http://localhost:8080/GormDslDemo
Welcome?to?Grails?1.0.2?-?http://grails.org/
Licensed?under?Apache?Standard?License?2.0
Grails?home?is?set?to:?D:\D\MY_DEV\grails-1.0.2
Base?Directory:?D:\_DEV\grails_apps\GormDslDemo
Note:?No?plugin?scripts?found
Running?script?D:\D\MY_DEV\grails-1.0.2\scripts\RunApp.groovy
Environment?set?to?development
??[groovyc]?Compiling?2?source?files?to?C:\Documents?and?Settings\Daniel\.grails\1.0.2\projects\Gorm
DslDemo\classes
Running?Grails?application..
2008-04-04?00:06:07.861::INFO:??Logging?to?STDERR?via?org.mortbay.log.StdErrLog
2008-04-04?00:06:07.095::INFO:??jetty-6.1.4
2008-04-04?00:06:08.454::INFO:??No?Transaction?manager?found?-?if?your?webapp?requires?one,?please?c
onfigure?one.
2008-04-04?00:06:08.329:/GormDslDemo:INFO:??Set?web?app?root?system?property:?'GormDslDemo'?=?[D:\_D
EV\grails_apps\GormDslDemo\web-app\]
2008-04-04?00:06:08.345:/GormDslDemo:INFO:??Initializing?Log4J?from?[file:C:\Documents?and?Settings\
Daniel/.grails/1.0.2/projects/GormDslDemo/resources/log4j.properties]
2008-04-04?00:06:08.376:/GormDslDemo:INFO:??Initializing?Spring?root?WebApplicationContext
[0]?spring.GrailsWebApplicationContext?Refreshing?org.codehaus.groovy.grails.commons.spring.GrailsWe
bApplicationContext@c393a1:?display?name?[org.codehaus.groovy.grails.commons.spring.GrailsWebApplica
tionContext@c393a1];?startup?date?[Fri?Apr?04?00:06:12?CST?2008];?parent:?org.springframework.web.co
ntext.support.XmlWebApplicationContext@bd3b2d
[0]?spring.GrailsWebApplicationContext?Bean?factory?for?application?context?[org.codehaus.groovy.gra
ils.commons.spring.GrailsWebApplicationContext@c393a1]:?org.springframework.beans.factory.support.De
faultListableBeanFactory@144569b
2008-04-04?00:06:19.620:/GormDslDemo:INFO:??Initializing?Spring?FrameworkServlet?'grails'
2008-04-04?00:06:20.151::INFO:??Started?SelectChannelConnector@0.0.0.0:8080
Server?running.?Browse?to?http://localhost:8080/GormDslDemo
8,打開瀏覽器,訪問:http://localhost:8080/GormDslDemo ,看一下自己的勞動成果。
9,查看MySQL中,Grails自動幫您生成的表:
AUTHOR表
CREATE?TABLE?`author`?(
??`AUTHOR_ID_PK`?bigint(20)?NOT?NULL,
??`AUTHOR_NAME`?varchar(255)?NOT?NULL,
??PRIMARY?KEY??(`AUTHOR_ID_PK`)
)
??`AUTHOR_ID_PK`?bigint(20)?NOT?NULL,
??`AUTHOR_NAME`?varchar(255)?NOT?NULL,
??PRIMARY?KEY??(`AUTHOR_ID_PK`)
)
BOOK表
CREATE?TABLE?`book`?(
??`BOOK_ID_PK`?bigint(20)?NOT?NULL,
??`AUTHOR_ID_FK`?bigint(20)?NOT?NULL,
??`BOOK_NAME`?varchar(255)?NOT?NULL,
??`BOOK_PRICE`?float?NOT?NULL,
??PRIMARY?KEY??(`BOOK_ID_PK`),
??KEY?`FK1F32E99AB78860`?(`AUTHOR_ID_FK`),
??CONSTRAINT?`FK1F32E99AB78860`?FOREIGN?KEY?(`AUTHOR_ID_FK`)?REFERENCES?`author`?(`AUTHOR_ID_PK`)
)
??`BOOK_ID_PK`?bigint(20)?NOT?NULL,
??`AUTHOR_ID_FK`?bigint(20)?NOT?NULL,
??`BOOK_NAME`?varchar(255)?NOT?NULL,
??`BOOK_PRICE`?float?NOT?NULL,
??PRIMARY?KEY??(`BOOK_ID_PK`),
??KEY?`FK1F32E99AB78860`?(`AUTHOR_ID_FK`),
??CONSTRAINT?`FK1F32E99AB78860`?FOREIGN?KEY?(`AUTHOR_ID_FK`)?REFERENCES?`author`?(`AUTHOR_ID_PK`)
)
看過這篇文章后,或多或少給了您一些如何方便地處理遺留數據庫的啟發了吧 :)
附:朝花夕拾——Groovy & Grails