如何在micronaut中使用Gorm为MySQL配置application.yml

时间:2018-12-28 11:18:06

标签: mysql hibernate gorm micronaut

我正在尝试在micronaut和groovy中创建一个CRUD应用程序,我正在使用休眠和GORM进行验证,保存,删除等操作,并使用mySql数据库和@Transactional。

当我运行应用程序时,它运行良好,但是在mysql中没有初始化表。在发送发布请求以将数据保存到数据库时,出现以下错误

  

5:54:48.840 [nioEventLoopGroup-1-3]错误i.m.h.s.netty.RoutingInBoundHandler-发生意外错误:无法准备语句   org.hibernate.exception.SQLGrammarException:无法准备语句

     

由以下原因引起:org.h2.jdbc.JdbcSQLException:找不到表“ STUDENT”; SQL语句:   从学生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')
   }

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

1 个答案:

答案 0 :(得分:0)

我刚刚使用MySQL进行了此工作。您可以参考这篇文章:

Micronaut MySQL delete record org.hibernate.HibernateException: No Session found for current thread