mysql - 在 Grails 2.0 中使用 MySQL - 查找外部连接器 .jar

标签 mysql grails hsqldb

我使用的是 Grails 2.0,但我无法让代码识别外部库。特别是 MySQL 驱动程序。

基本上,每当我尝试将数据源从 HSQLDB 更改为 MySql 时,问题就会发生。我下载了连接器 jar (5.0.8) 并将其放在 (project)/lib 目录中。我已验证 com.mysql.jdbc.Driver.class 文件位于 .jar 中。

每次我尝试运行该应用程序时,我都会收到错误消息:

由 ClassNotFoundException: com.mysql.jdbc.Driver 引起

非常感谢任何帮助。

dataSource {
    pooled = true
    //driverClassName = "org.h2.Driver"
    //username = "sa"
    //password = ""
    driverClassName = "com.mysql.jdbc.Driver"
    username = "bob"
    password = "password"
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = true
    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"
            dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
            url = "jdbc:mysql://localhost:3306/tekevent"
        }
    }
    test {
        dataSource {
            dbCreate = "create-drop"
            url = "jdbc:h2:mem:testDb;MVCC=TRUE"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:prodDb;MVCC=TRUE"
            pooled = true
            properties {
               maxActive = -1
               minEvictableIdleTimeMillis=1800000
               timeBetweenEvictionRunsMillis=1800000
               numTestsPerEvictionRun=3
               testOnBorrow=true
               testWhileIdle=true
               testOnReturn=true
               validationQuery="SELECT 1"
            }
        }
    }
}

最佳答案

删除 jar 并在 BuildConfig.groovy 中使用依赖声明。与其拥有许多 jar 的多个副本,不如每个都下载一次并将它们缓存在您的 Ivy 缓存中并从那里引用它们。

BuildConfig.groovy 已经有一个 MySQL 的例子;只需取消注释并在需要时更新版本。还要确保未注释 mavenCentral() 存储库:

repositories {
    ...
    mavenCentral()
}

dependencies {
   ...
   runtime 'mysql:mysql-connector-java:5.1.16'
}

关于mysql - 在 Grails 2.0 中使用 MySQL - 查找外部连接器 .jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8945755/

相关文章:

java - 定位Java/mySQL应用的瓶颈

Grails 3 run-app 在 bootRun 后不返回

grails - 使用grails添加数据时,请提供null异常帮助

java - 创建多个 HSQLDB 服务器数据库的最简单方法是什么?

java - 使用 hsql hibernate 数据源隔离 Junit 测试

java - 无法以新用户身份连接到 HyperSQL/HSQLDB 数据库

php - Amazon RDS 中的大量数据

mysql - 如何使用prometheus和grafana监控多个mysql?

tomcat - 在生产中将 war 部署到 tomcat 后如何运行 grails 命令?

php - Symfony 3多对多不将数据保存到ManyToMany表