Grails切换到mysql数据库需要做如下的配置:
1、将mysql-connector-java-5.1.22-bin.jar驱动复制到Grails应用的根目录lib下面。
2、修改conf/DataSource.groovy;Grails默认用的是H2数据库,我们需要将driverClassName = "org.h2.Driver" 改成 driverClassName = 'com.mysql.jdbc.Driver';url改成我们自己数据库的url
上面的改法会发生几个问题,
1、如果单纯光把驱动复制到lib目录,会发生异常:
java.lang.ClassNotFoundException: com/mysql/jdbc/Driver
找不到这个驱动,此时我们还需要在BuildConfig.groovy文件中加入下面的配置:
dependencies {
runtime 'mysql:mysql-connector-java:5.1.22'
}
2、尽管我们自己的应用用的是mysql,但是可能某些插件或者框架用的是H2数据库,这样如果我们之配置mysql的数据源,可能导致数据库连接驱动不匹配的异常。
因此我们需要保留原来的数据源配置,另外添加mysql的数据源。
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
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-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
dataSource_mysql {
dialect = org.hibernate.dialect.MySQLInnoDBDialect
driverClassName = 'com.mysql.jdbc.Driver'
username = 'root'
password = 'root'
url = 'jdbc:mysql://localhost/DojoGrails'
dbCreate = 'update'
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
pooled = true
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
}
}
上面是DataSource配置的例子,添加了第二个数据源 dataSource_mysql
在这个多数据源应用里,我们需要在使用dataSource_mysql数据源的domain类里加入如下配置:
static mapping = {
datasource 'mysql'
}
以表明这个domain用的是哪个数据源;
~~~~~~~~~~~~~~
官方对多数据源的说明:
Configuring Additional DataSources
The default DataSource configuration in grails-app/conf/DataSource.groovy looks something like this:
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
}
environments {
development {
dataSource {
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDb"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:h2:prodDb"
}
}
}
This configures a single DataSource with the Spring bean named dataSource. To configure extra DataSources, add another dataSource block (at the top level, in an environment block, or both, just like the standard DataSource definition) with a custom name, separated by an underscore. For example, this configuration adds a second DataSource, using MySQL in the development environment and Oracle in production:
environments {
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 = 'secret'
url = 'jdbc:mysql://localhost/lookup'
dbCreate = 'update'
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:h2:prodDb"
}
dataSource_lookup {
dialect = org.hibernate.dialect.Oracle10gDialect
driverClassName = 'oracle.jdbc.driver.OracleDriver'
username = 'lookup'
password = 'secret'
url = 'jdbc:oracle:thin:@localhost:1521:lookup'
dbCreate = 'update'
}
}
}
You can use the same or different databases as long as they're supported by Hibernate.
Configuring Domain Classes
If a domain class has no DataSource configuration, it defaults to the standard 'dataSource'. Set the datasource property in the mapping block to configure a non-default DataSource. For example, if you want to use the ZipCode domain to use the 'lookup' DataSource, configure it like this;
class ZipCode {
String code
static mapping = {
datasource 'lookup'
}
}
A domain class can also use two or more DataSources. Use the datasources property with a list of names to configure more than one, for example:
class ZipCode {
String code
static mapping = {
datasources(['lookup', 'auditing'])
}
}
If a domain class uses the default DataSource and one or more others, use the special name 'DEFAULT' to indicate the default DataSource:
class ZipCode {
String code
static mapping = {
datasources(['lookup', 'DEFAULT'])
}
}
If a domain class uses all configured DataSources use the special value 'ALL':
class ZipCode {
String code
static mapping = {
datasource 'ALL'
}
}
分享到:
相关推荐
详细讲解grails开发环境配置。 详细讲解grails连接mysql数据库,crud开发
In Grails: A Quick-Start Guide, you’ll see how to use Grails by iteratively building an unique, working application. By the time we’re done, you’ll have built and deployed a real, functioning ...
这个例子实现了用grails 连接mysql 数据库,并且把内容显示出来,可以增删除改,下载这个例子后大家要把jdk tomcat grails环境搭好,然后用grails run-app来执行,也可以用其它的开发工具,本人用的是netbeans
Java web development is notoriously tedious, but help is on the way: Grails. Using the principle of convention-over-configuration and the dynamic Groovy programming language, Grails takes the pain out...
grails 配置mongodb数据库,并实现了用户登录的系统,只要将grails的环境配置好,安装上mongodb,再建立一个数据库ggtest,然后运行即可自动生成相应的集合数据。
本文内容包括:ShortenUrl插件简介创建TinyUrl类测试TinyUrl类创建IsGd类创建ShortenUrl服务打包并部署插件结束语下载参考资料在这个“精通Grails”系列中,ScottDavis将向您展示如何创建您自己的Grails插件。...
在启动时,MongoDB插件将在您的计算机中查找“ mongo”配置属性Config.groovy文件,该文件定义您的MongoDB数据库和相关配置特性。 #Config.groovy mongo{ databases{ server1{ host = "localhost" port = 1234...
Grails3配置单独的数据库properties.pdf
昂首阔步 Grails Swagger实现(不使用JAX-RS)
NULL 博文链接:https://search.iteye.com/blog/386023
在线学习 grails 使用 Groovy on Rails (Grails) 构建的 Web 应用程序。 它使用Bootstrap为网页赋予样式,从而管理学生和课程的数据库,并以HTML呈现内容。 用户可以看到数据库中所有课程/用户的列表; 还有课程和...
构建一个简单的 grails 应用程序来管理项目信息。 描述 该应用程序应允许用户创建/编辑/删除/查看项目。 项目具有以下信息: - 项目名称(字母数字) - 项目代码(字母数字) - 技术负责人(人名) - 项目经理(人名...
OohLaLog Grails 插件 Grails OohLaLog 插件添加了一个 log4j appender 来将流量转发到 oohlalog api 服务。 这允许其用户跨多个服务器聚合日志、设置警报、自定义计数器并实时监控他们的应用程序。 Log4J 附加程序 ...
AngularJS Grails的Lazybones模板 入门 要求 您需要在计算机上安装以下组件: JDK 1.6以上 创建一个新项目 lazybones create angular-grails my-project 资源 运行你的项目 任何新项目都将使用。 您可以像这样启动...
销售点Grails 中的销售点。 与 Jasper Reports 和 Paypal 集成。 该项目的完整细节在“Proyecto Final”pdf文件中(西班牙语) 主要关注的是后端核心功能,而不是前端和 UI。 由 Richard Garcia 和 David De La Hoz ...
流浪圣杯用于在 VM 中运行 Grails 的 Vagrant VM 配置。 要求: vagrantvagrant安装所需的流浪插件 vagrant plugin install vagrant-vbguestvagrant plugin install vagrant-cachier启动所有安装的 VM vagrant up...
详细介绍grails框架的奥秘,英文版你值得拥有
NULL 博文链接:https://youngbrick.iteye.com/blog/2352298
playIT-grails 过时的项目,请参见: 对于新版本