【问题标题】:How to configure a MySQL DataSource on Grails 3.1.x with web-api profile?如何使用 web-api 配置文件在 Grails 3.1.x 上配置 MySQL 数据源?
【发布时间】:2016-01-22 09:58:13
【问题描述】:

我从 v1.3.x 开始就已经在使用 Grails,现在我在 DataSource/GORM 配置方面没有成功...

我的步骤是:
1 - $ grails create-app myApi --profile=web-api.

2 - 配置 application.yml 文件:

dataSource:
    pooled: true
    jmxExport: true
    driverClassName: com.mysql.jdbc.Driver
    dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    username: root
    password:

environments:
    development:
        dataSource:
            dbCreate: update
            url: jdbc:mysql://localhost:3306/test-app

3 - 在 build.gradle 文件中,将运行时依赖项 "com.h2database:h2" 替换为 "mysql:mysql-connector-java:5.1 .37"

4 - 创建一个名为 test-app

的数据库

5 - 创建域:$grails create-domain-class MyDomain

6 - 在 MyDomain 类上添加一个名为 name
的字符串属性 示例:

package myapi

class MyDomain {
    String name
    static constraints = { }
}

7 - 运行应用程序:$grails run-app
输出:

正在运行应用程序...
objc[27851]:类 JavaLaunchHelper 在 /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/bin/java 和 /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home 中实现/jre/lib/libinstrument.dylib。将使用两者之一。哪一个是未定义的。
Grails 应用程序在环境中以http://localhost:8080 运行:开发

并且没有创建表...有人知道我在这里缺少什么步骤吗?

OBS.:即使我拼错了数据库名称,应用程序仍在运行且不会发生错误。

【问题讨论】:

  • 当我尝试保存 MyDomain 的实例时,出现 java.lang.reflect.InvocationTargetException 并出现以下消息:“在 Grails 应用程序外部使用了类 [myapi.MyDomain] 上的方法。如果正在运行在使用模拟 API 或引导 Grails 的测试上下文中正确。”

标签: mysql hibernate grails grails-orm grails-domain-class


【解决方案1】:

默认情况下,Hibernate 插件不包含在带有 web-api 配置文件的 Grails 3.1.0.M2 的正常依赖项中,因此为了解决此问题,您只需添加您的依赖项:

runtime "org.grails.plugins:hibernate:4.3.10.7"

更多信息请访问:https://github.com/grails/grails-core/issues/9366

【讨论】:

    猜你喜欢
    • 2016-01-12
    • 2011-09-11
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 2015-03-01
    • 2010-11-01
    • 2017-08-21
    相关资源
    最近更新 更多