繁体   English   中英

Spring JPA实体和龙目岛

[英]Spring jpa entity and Lombok

我已经用Spring创建了项目。

dependencies {
    compile('org.springframework.boot:spring-boot-starter-cache')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-devtools')
    compile('org.projectlombok:lombok')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.codehaus.groovy:groovy')
    runtime('com.h2database:h2')
    runtime('mysql:mysql-connector-java')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
}

application.properties:

spring.data.rest.base-path=/api

spring.datasource.url=jdbc:mysql://localhost/secret_backend
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

和实体类:

package com.app.Entity

import lombok.Data

import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.Table

@Data
@Entity
@Table(name = "cities")
public class City {

    private @Id @GeneratedValue Long id;
    private String slug;
    private String title;
    private String titleShort;

    private City() {}

    /*public String getSlug(){
        return slug;
    }*/

    public City(String slug) {
        this.slug = slug;
    }
}

当我导航到localhost:8080 / api / cities时,看不到实际的数据表单数据库:

{
  "_embedded": {
    "cities": [
      {
        "_links": {
          "self": {
            "href": "http://localhost:8080/api/cities/7"
          },
          "city": {
            "href": "http://localhost:8080/api/cities/7"
          }
        }
      },
      {
        "_links": {
          "self": {
            "href": "http://localhost:8080/api/cities/8"
          },
          "city": {
            "href": "http://localhost:8080/api/cities/8"
          }
        }
      },
...

仅当我向实体中添加吸气剂时,我才能看到数据,但是从lombok文档中,@ Data注释必须为所有实体属性生成吸气剂和设置器。

将City.groovy重命名为City.java,现在工作正常。 感谢@highstakes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM