Grails(3)Guide Book Chapter 4 Configuration

本文介绍 Grails 框架中数据源的配置方法,包括使用不同数据库时的 JDBC 驱动配置、环境感知的数据源定义、JNDI 数据源引用、数据库迁移等内容,并详细讲解了如何配置及使用多个数据源。
Grails(3)Guide Book Chapter 4 Configuration
4.3 The DataSource
If we use a database other than H2 we need a JDBC driver. It's the best to use Ivy to resolve the jar.

The dependency for the MySQL driver like this:
grails.project.dependency.resolution = {
inherits("global")
log "warn"
repositories{
grailsPlugins()
grailsHome()
grailsCentral()
mavenCentral()
}
dependencies {
runtime 'mysql:mysql-connector-java:5.1.16'
}
}

If we can't use Ivy then just put the JAR in our project's lib directory.

Database configuration file is her:
grails-app/conf/DataSource.groovy

Take these configuration as instance
dataSource{
pooled = true
dbCeate = "update"
url = "jdbc:mysql://localhost/yourDB"
driverClassName = "com.mysql.jdbc.Driver"
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
username = "yourUser"
password = "yourPassword"
}

dbCreate ---- create, create-drop, update, validate
create - Drops the existing schema. Creates the schema on startup.
create-drop - Same as create, drops the tables when the application shuts down.
update - Creates missing tables and indexes.
validate - Make no changes to my database, just reports warning.

4.3.1 DataSources and Environments
Just note here, Grails' DataSource definition is 'environment aware'.

4.3.2 JNDI DataSources
Referring to a JNDI DataSource

dataSource {
jndiName = "java:comp/env/myDataSource"
}

4.3.3 Automatic Database Migration

4.3.4 Transaction-aware DataSource Proxy

4.3.5 Database Console
Config.groovy

environments {
production {
grails.serverURL = "http://www.sillycat.com"
grails.dbconsole.enabled = true
grails.dbconsole.urlRoot = '/admin/dbconsole'
}
development {
grails.serverURL = "http://localhost:8080/${appname}"
}
test {
grails.serverURL = "http://localhost:8080/${appname}"
}
}

4.3.6 Multiple Datasources
By default all domain classes share a single DataSource and a single database.
Configuring Additional DataSources
In grails-app/conf/DataSource.groovy

envionments {
development {
dataSource {
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDb"
}
dataSource_lookup {
dialect = org.hibernate.dialect.MySQLInnoDBDialect
driverClassName = 'com.mysql.jdbc.Driver'
username = 'lookup'
password = '111111'
url = 'jdbc:mysql://localhost/lookup'
dbCreate = 'update'
}
}
…snip…
production {
dataSource {
dbCreate = "update"
url = "jdbc:h2:prodDb"
}
dataSource_lookup {
dialect = org.hibernate.dialect.Oracle10gDialect
driverClassName = 'oracle.jdbc.driver.OracleDrvier'
username = 'lookup'
password = '111111'
url = 'jdbc:oracle:thin:@localhost:1521:lookup'
dbCreate = 'update'
}
}
}

Configuring Domain Classes
If a domain class has no DataSource configuration, it defaults to the standard 'dataSource'. If we want to change that, we need to set the datasource property in the mapping block to configure a non-default DataSource.

class ZipCode {
String code
static mapping = {
datasource 'lookup'
}
}

A domain class can also use 2 or more DataSources.
static mapping = {
datasources(['lookup', 'auditing'])
}

DEFAULT is the special name for the default Datasource
static mapping = {
datasources(['lookup', 'DEFAULT'])
}

Haha, a domain can also configured to use all the DataSources
static mapping = {
datasource 'ALL'
}

Namespaces and GORM Methods
If a domain class uses more than one DataSource then we can use the namespace implied by each DataSource name to make GORM calls for a particular DataSource.

def zipCode = ZipCode.auditing.get(42)
…snip…
zipCode.auditing.save()

Hibernate Mapped Domain Classes


Services
class DataService {
static datasource = 'lookup'
…snip...
}

4.4 Externalized Configuration
In this example, we are losing configuration files (both Java Properties files and ConfigSlurper configurations) from different places on the class path and files located in USER_HOME.
Setting in Config.groovy

grails.config.locations = [
"classpath:${appName}-config.properties",
"classpath:${appName}-config.groovy",
"file:${userHome}/.grails/${appName}-config.properties",
"file:${userHome}/.grails/${appName}-config.groovy"
]

4.5 Versioning
Versioning Basics
When we create the project, the version is 0.1.
>grails set-version 0.2

Detecting Versions at Runtime
def version = grailsApplication.metadata['app.version']
Within controllers there is an implicit grailsApplication variable that can be used.

def grailsVersion = grailsApplication.metadata['app.grails.version']

alternatively

import grails.util.GrailsUtil

def grailsVersion = GrailsUtil.gailsVersion

4.6 Project Documentation
>grails doc

4.7 Dependency Resolution
grails-app/conf/BuildConfig.groovy

grails.project.dependency.resolution = {

dependencies {
runtime 'mysql:mysql-connector-java:5.1.16'
}
plugins {
compile ":hibernate:$grailsVersion"
compile ":jquery:1.6.1.1"
compile ":resources:1.0"

build ":tomcat:$grailsVersion"
}
}

4.7.1 Configurations and Dependencies
Grails features five dependency resolution configurations( or rather say scopes)
build Dependencies for the build system only
compile Dependencies for the compile step
runtime Dependencies needed at runtime but not for compilation
test Dependencies needed for testing but not at runtime
provided Dependencies needed at development time, but not during WAR deployment

Disabling transitive dependency resolution
By default, Grails will not only get the JARs and plugins that you declare, but it will also get their transitive dependencies. We can disable transitive dependency resolution.

runtime('com.mysql:mysql-connector-java:5.1.16',
'net.sf.ehcache:ehcache:1.6.1') {
transitive = false
}

Alternatively

runtime group: 'com.mysql',
name: 'mysql-connector-java',
version: '5.1.16',
transitive:false

Excluding specific transitive dependencies
runtime('com.mysql:mysql-connector-java:5.1.16',
'net.sf.ehcache:ehcache:1.6.1') {
excludes "xml-apis", "commons-logging"
}

alternatively

runtime(…snip..) {
excludes([ group: 'xml-apis', name: 'xml-apis'],

[name:'commons-logging' ] )
}

Where are the JARs
The default place is user.home/.grails/ivy-cache, we can change in settings.groovy
grails.dependency.cache.dir = "${userHome}/.my-dependency-cache"


References:
http://grails.org/doc/latest/guide/conf.html
http://grails.org/doc/latest/guide/index.html
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值