1.添加 Airline
类
static constraints 里面是字段顺序
class Airline {
static mapping = {
table 'some_other_table_name'
columns {
name column:'airline_name'
url column:'link'
frequentFlyer column:'ff_id'
}
}
static constraints = {
name(blank:false, maxSize:100) // 数据验证
url(url:true)
frequentFlyer(blank:true)
notes(maxSize:1500)
}
String name
String url
String frequentFlyer
String notes
String toString(){
return name
}
}
class Trip {
String name
String city
...
Airline airline
}
3. generate-all demo.Airline
generate-all demo.Trip
4.grails-app/conf/DataSource.groovy 更换为MySQL
dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
username = "root"
password = "root"
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql://localhost:3306/test?autoreconnect=true"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost:3306/test?autoreconnect=true"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost:3306/test?autoreconnect=true"
pooled = true
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
}
}
创建了数据库,将驱动程序 JAR 复制到 lib 目录
5. 如果不能跑,那么就改下
demo\grails-app\conf\BuildConfig.groovy
把第36行 // runtime 'mysql:mysql-connector-java:5.1.16' 注释去掉
让程序在run-app自动下载jar