首页 > 解决方案 > 如何在 intellij J 2021 的 grails 3.3.9 中连接 Sqlite 数据源

问题描述

我需要一些帮助:我尝试从 grails 3.3.9 中的文件连接 sqlite 数据源

这是我的 application.yml 文件:

hibernate:
   cache:
    queries: false
    use_second_level_cache: false
    use_query_cache: false
dataSource:
     pooled: true
     jmxExport: true
     driverClassName: org.h2.Driver
     username: sa
     password: ''
dataSources:
   IkebanaUser:
    dbCreate: "update"
    url: "jdbc:sqlite:/home/marcos/IkebanaUser"
    logSql: "true"
    dialect: "SQLiteDialect"
    driverClassName: "org.sqlite.JDBC"
environments:
   development:
    dataSource:
        dbCreate: create-drop
        url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE

   test:
        dataSource:
        dbCreate: update
        url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
production:
    dataSource:
        dbCreate: none
        url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
        properties:
            jmxEnabled: true
            initialSize: 5
            maxActive: 50
            minIdle: 5
            maxIdle: 25
            maxWait: 10000
            maxAge: 600000
            timeBetweenEvictionRunsMillis: 5000
            minEvictableIdleTimeMillis: 60000
            validationQuery: SELECT 1
            validationQueryTimeout: 3
            validationInterval: 15000
            testOnBorrow: true
            testWhileIdle: true
            testOnReturn: false
            jdbcInterceptors: ConnectionState
            defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED
    dataSources:
            IkebanaUser:
                dbCreate: "update"
                url: "jdbc:sqlite:/home/marcos/IkebanaUser"
                logSql: "true"
                dialect: "SQLiteDialect"
                driverClassName: "org.sqlite.JDBC"

这是我的 build.gradle 文件:

buildscript {
    repositories {
       mavenLocal()
       maven { url "https://repo.grails.org/grails/core" }
}
 dependencies {
    classpath "org.grails:grails-gradle-plugin:$grailsVersion"
    classpath "org.grails.plugins:hibernate5:${gormVersion-".RELEASE"}"
    classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.15.1"
}

}

version "0.1"
group "ikebana.gammaversion"

apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"asset-pipeline"
apply plugin:"org.grails.grails-gsp"

repositories {
   mavenLocal()
   maven { url "https://repo.grails.org/grails/core" }

}

dependencies {
   compile "org.springframework.boot:spring-boot-starter-logging"
   compile "org.springframework.boot:spring-boot-autoconfigure"
   compile "org.grails:grails-core"
   compile "org.springframework.boot:spring-boot-starter-actuator"
   compile "org.springframework.boot:spring-boot-starter-tomcat"
   compile "org.grails:grails-web-boot"
   compile "org.grails:grails-logging"
   compile "org.grails:grails-plugin-rest"
   compile "org.grails:grails-plugin-databinding"
   compile "org.grails:grails-plugin-i18n"
   compile "org.grails:grails-plugin-services"
   compile "org.grails:grails-plugin-url-mappings"
   compile "org.grails:grails-plugin-interceptors"
   compile "org.grails.plugins:cache"
   compile "org.grails.plugins:async"
   compile "org.grails.plugins:scaffolding"
   compile "org.grails.plugins:events"
   compile "org.grails.plugins:hibernate5"
   compile "org.hibernate:hibernate-core:5.1.16.Final"
   compile "org.grails.plugins:gsp"
   console "org.grails:grails-console"
   profile "org.grails.profiles:web"
   runtime "org.glassfish.web:el-impl:2.1.2-b03"
   runtime "com.h2database:h2"
   runtime "org.apache.tomcat:tomcat-jdbc"
   runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.15.1"

   runtime 'org.xerial:sqlite-jdbc:3.6.17'

  testCompile "org.grails:grails-gorm-testing-support"
  testCompile "org.grails.plugins:geb"
  testCompile "org.grails:grails-web-testing-support"
  testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
  testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"

}

bootRun {
   jvmArgs('-Dspring.output.ansi.enabled=always')
   addResources = true
   String springProfilesActive = 'spring.profiles.active'
   systemProperty springProfilesActive, System.getProperty(springProfilesActive)

}

assets {
     minifyJs = true
     minifyCss = true

}

这是我的域类类 Usuario {

  static constraints = {
 }
static mapping = {
    datasource 'IkebanaUser'
}
private def nombre
private def apellido

def getNombre() {
    return nombre
}

……

我的“控制器”是

class UsuarioController {
  static scaffold= Usuario
   def index() { }
}

我也看到这篇文章Grails and SQLite to create a SQlitedialect.java in a java directory(见图),mi SO是Ubuntu 18.04,你认为错误在我的aplciation.yml中。请帮助..非常感谢 输入图片说明在这里

错误是这样的

正在运行应用程序... 2021-07-02 22:59:01.614 错误 --- [main] osboot.SpringApplication:应用程序启动失败

org.springframework.beans.factory.UnsatisfiedDependencyException:在类路径资源[org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]中定义的名称为“methodValidationPostProcessor”的bean创建时出错:通过方法“methodValidationPostProcessor”参数0表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“hibernateDatastoreServiceRegistry”的 bean 时出错:设置构造函数参数时无法解析对 bean“hibernateDatastore”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“hibernateDatastore”的 bean 时出错:通过构造函数实例化 bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.grails.orm.hibernate.HibernateDatastore]:构造函数抛出异常;嵌套异常是 org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver. java:749) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1178) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467) .beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1072) 在 org.springframework.beans.factory.support。java:47) 在 org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) 在 org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136) 在 ikebana.gammaversion .Application.main(Application.groovy:8) 原因:org.springframework.beans.factory.BeanCreationException:创建名为“hibernateDatastoreServiceRegistry”的bean时出错:设置构造函数参数时无法解析对bean“hibernateDatastore”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“hibernateDatastore”的 bean 时出错:通过构造函数实例化 bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.grails.orm.hibernate.HibernateDatastore]:构造函数抛出异常;通过构造函数实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.grails.orm.hibernate.HibernateDatastore]:构造函数抛出异常;嵌套异常是 org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver. java:279) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1100) 在 org.springframework .beans.factory.support.AbstractAutowireCapableBeanFactory。无法实例化 [org.grails.orm.hibernate.HibernateDatastore]:构造函数抛出异常;嵌套异常是 org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:271) ...省略了49个常见框架作者:org.hibernate.service.spi.ServiceException:无法在 org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:264)创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] ) 在组织。

FAILURE:构建失败并出现异常。

进程 'command '/usr/lib/jvm/java-8-oracle/bin/java'' 以非零退出值 1 结束

进程以退出代码 1 结束

标签: sqliteintellij-ideagrailsgroovydatasource

解决方案


build.graddle 中需要:

         compile group: 'com.zsoltfabok', name: 'sqlite-dialect', version: '1.0'

也把这条线扔掉

runtime 'org.xerial:sqlite-jdbc:3.6.17'

和 .yml 看起来

数据源:

 pooled: true
 jmxExport: true
 driverClassName: org.h2.Driver
 username: sa
 password: ''

 dataSources:
     IkebanaUser:
        pooled: true
        driverClassName: "org.sqlite.JDBC"
        username: ''
        password: ''

environments:
    development:
       dataSource:
       dbCreate: create-drop
       url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE

       dataSources:
           IkebanaUser:
           url: jdbc:sqlite:/home/marcos/IkebanaUser
           dbCreate: update
  test:
      dataSource:
           dbCreate: update
           url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE

  production:
     dataSource:
        dbCreate: none
        url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000; DB_CLOSE_ON_EXIT=FALSE
        properties:
        jmxEnabled: true
        initialSize: 5
        maxActive: 50
        minIdle: 5
        maxIdle: 25
        maxWait: 10000
        maxAge: 600000
        timeBetweenEvictionRunsMillis: 5000
        minEvictableIdleTimeMillis: 60000
        validationQuery: SELECT 1
        validationQueryTimeout: 3
        validationInterval: 15000
        testOnBorrow: true
        testWhileIdle: true
        testOnReturn: false
        jdbcInterceptors: ConnectionState
        defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

推荐阅读