如何使用micronaut中的gorm为mysql配置application.yml

ffx8fchx  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(313)

我试图用micronaut和groovy制作一个crud应用程序,我使用hibernate和gorm对mysql数据库和@transactional进行验证、保存、删除等。
当我运行应用程序时,它运行良好,但是在mysql中没有初始化表。当发送post请求将数据保存到db中时,我得到以下错误
5:54:48.840[nioeventloopgroup-1-3]错误i.m.h.s.netty.routinginboundhandler-发生意外错误:无法准备语句org.hibernate.exception.sqlgrammareexception:无法准备语句
原因:org.h2.jdbc.jdbcsqlexception:未找到表“student”;sql语句:从student this中选择this.id作为y0,其中this.name=?限制[42102-196]

我的域类代码是:

@Entity
class Student {
   String name
   int age
   String gender
   String number

   static constraints = {
       name nullable:false, blank:false,unique:true
       age nullable:true,blank:true,size: 1..5
       gender nullable:true,blank:true
       number size: 11..13,blank:true,unique:true,nullable:true
   }
}

我的控制器代码如下:

@Transactional(readOnly = true)
@Controller("/")
class StudntController {

   @Post("/save")
   @Transactional
   def save(@Body Object JSON)
   {
     def result = [:]

     Student stu = new Student(name: 
      JSON?.name,gender:JSON?.gender)

     stu.save(flush: true,failOnError: true)

     result.put("Student",stu)
     result.put("Message", "Student created successfully")
   }
}

这是我的application.yml配置:

micronaut:
 application:
    name: mincronaut-crud

 server:
        port: 8080
 ---

 hibernate:
    cache:
       queries: false
       use_second_level_cache: false
       use_query_cache: false

 environments:
    development:
       dataSource:
           dbCreate: update
           pooled: true
           jmxExport: true
           driverClassName: com.mysql.jdbc.Driver
           username: admin
           password: qwerzxcv123
           dialect: org.hibernate.dialect.MySQL5InnoDBDialect
           url: jdbc:mysql://192.168.1.121:3306/student
    test:
        dataSource:
           dbCreate: update
           url: jdbc:mysql://192.168.1.121:3306/student
    production:
        dataSource:
           dbCreate: update
           url: jdbc:mysql://192.168.1.121:3306/student

下面是我的build.gradle配置:

plugins {
   id "io.spring.dependency-management" version "1.0.6.RELEASE"
   id "com.github.johnrengelman.shadow" version "4.0.2"
   }

   apply plugin:"application"
   apply plugin:"groovy"

   version "0.1"
   group "mincronaut.crud"

   repositories {
     mavenLocal()
     mavenCentral()
     maven { url "https://jcenter.bintray.com" }
   }

   ext {
      gormVersion = '6.1.9.RELEASE'
      h2Version = '1.4.196'
      tomcatJdbcVersion = '8.5.28'
   }

  dependencyManagement {
   imports {
      mavenBom 'io.micronaut:micronaut-bom:1.0.1'
     }
    }

   dependencies {
     compile "io.micronaut:micronaut-http-client"
     compile "io.micronaut:micronaut-http-server-netty"
     compile "io.micronaut:micronaut-runtime-groovy"
     compile "io.micronaut:micronaut-validation"
     compile "io.micronaut.configuration:micronaut-hibernate-gorm"
     compileOnly "io.micronaut:micronaut-inject-groovy"
     compile "org.grails:grails-datastore-gorm- 
     hibernate5:$gormVersion"
     runtime "org.apache.tomcat:tomcat-jdbc:$tomcatJdbcVersion"
     runtime 'mysql:mysql-connector-java:6.0.6'
     runtime "ch.qos.logback:logback-classic:1.2.3"
     runtime "com.h2database:h2:$h2Version"
     testCompile "io.micronaut:micronaut-inject-groovy"
     testCompile("org.spockframework:spock-core") {
       exclude group: "org.codehaus.groovy", module: "groovy-all"
      }
     testCompile "junit:junit:4.12"
     testCompile "io.micronaut:micronaut-inject-java"
     testCompile "org.hamcrest:hamcrest-all:1.3"
     }

     shadowJar {
         mergeServiceFiles()
     }

     run.jvmArgs('-noverify', '-XX:TieredStopAtLevel=1')

     mainClassName = "mincronaut.crud.Application"
     tasks.withType(GroovyCompile) {
     groovyOptions.forkOptions.jvmArgs.add('- 
       Dgroovy.parameters=true')
   }

为什么运行后的代码没有在数据库中创建任何表,为什么它选择h2数据库,即使我在应用程序中的url是为mysql配置的,而且我还在build.gradle中提供了mysql插件。请帮助我在micronaut中使用gorm和groovy正确配置mysql。

doinxwow

doinxwow1#

我刚刚用mysql制作了这个工作凝乳。你可以参考这篇文章:
micronaut mysql delete record org.hibernate.hibernateexception:找不到当前线程的会话

相关问题