- 浏览: 2552106 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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
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
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 478NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 423Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 248GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 451GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 319Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 288NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 261Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 573NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 266Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 368Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 370Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
《Grails 2 的终极指南》是一本深入探讨Grails框架精髓的专业书籍,该书以英文撰写,旨在为读者提供全面、深入的Grails框架学习资料。Grails框架基于Groovy语言,是一种高度动态、敏捷的Java应用开发框架,它简化了...
《The Definitive Guide to Grails 2》是Grails框架深入学习的重要参考资料,由业界专家撰写,旨在为开发者提供全面、详尽的Grails 2技术指导。这本书结合了理论与实践,不仅介绍了Grails的基本概念,还涵盖了高级...
标题中的“grails3 CAS链接接数据库”表明我们要讨论的是如何在Grails 3框架下集成CAS(Central Authentication Service)并实现与数据库的连接。Grails是一个基于Groovy语言的开源Web应用框架,而CAS是一种广泛使用...
grails1.0开发框架3 类似于ruby on rails的框架。
4. **命令行工具**:Grails 提供强大的命令行工具,支持创建项目、运行应用、生成代码等任务,大大提升了开发效率。 5. **构建工具**:Grails 使用Gradle作为其构建工具,允许自定义构建流程和依赖管理。 **Grails...
Grails 3版本作为该框架的更新迭代产物,同样支持邮件发送服务。 首先,要在Grails 3中开发邮件发送功能,你需要遵循以下步骤: 1. 新建一个Grails项目。在创建项目的过程中,你需要按照Grails框架的约定设置好...
3. **实践 Grails 项目**:通过构建实际的 Web 应用,加深对 Grails 各个特性的理解。 4. **参与社区**:加入 Grails 社区,与其他开发者交流心得,获取最新的技术动态。 《Grails 完全指南》不仅是一本学习资料,...
3. **GORM(Grails Object-Relational Mapping)**:Grails的内置ORM工具,允许开发者以声明式的方式操作数据库,支持SQL的CRUD操作,简化了数据持久化的过程。GORM支持多种数据库,如MySQL、PostgreSQL等。 4. **...
grails1.0开发框架4 类似于ruby on rails的框架。
Grails 3是其一个重要的版本更新,带来了许多改进和新特性。本篇将详细介绍如何在Grails 3中配置单独的数据库properties,以便在开发过程中管理多个数据源,满足复杂的企业级需求。 首先,我们需要理解Grails中的...
3. **GORM (Grails Object-Relational Mapping)**: GORM是Grails的内置ORM框架,允许开发者使用Groovy DSL来处理数据库操作,支持多种数据库,如MySQL、PostgreSQL等。 4. **Grails Command Line Interface (CLI)**...
【Grails项目搭建详解】 Grails是一个基于Groovy语言的开源Web应用框架,它简化了开发过程,尤其适合快速构建动态网站。在Eclipse中搭建Grails项目可能相对复杂,但通过以下步骤,即使是初学者也能顺利进行。 1. *...
Definitive Guide to Grails
3. Convention over Configuration(CoC):Grails的核心理念之一就是“约定优于配置”,这意味着开发者在很多情况下不需要写大量的配置文件,框架会自动根据约定进行工作。 二、Grails中文文档的价值 1. 学习入口...
Using the principle of convention-over-configuration and the dynamic Groovy programming language, Grails takes the pain out of web development and brings back the fun. This book will get you up and ...
《The Definitive Guide to Grails 2》是关于Grails框架的一本权威指南,它为读者提供了深入理解和使用Grails 2开发Web应用程序所需的知识。Grails是一种基于Groovy语言的开源全栈式Web应用框架,它借鉴了Ruby on ...