1

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

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

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

引起:org.h2.jdbc.JdbcSQLException:找不到表“STUDENT”;SQL 语句:select this_.id as y0_ from student this_ where 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。

4

1 回答 1

0

我刚刚用 MySQL 做了这个工作 CURD。你可以参考这篇文章:

Micronaut MySQL 删除记录 org.hibernate.HibernateException: No Session found for current thread

于 2018-12-29T01:51:42.753 回答