gpt4 book ai didi

java - Spring jpa实体和Lombok

转载 作者:搜寻专家 更新时间:2023-11-01 03:34:19 24 4
gpt4 key购买 nike

我用 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')
}

应用程序属性:

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"
}
}
},
...

只有当我向实体添加 getter 时,我才能看到数据,但从 lombok 文档来看,@Data 注释必须为所有实体属性生成 getter 和 setter。

最佳答案

将 City.groovy 重命名为 City.java,现在运行良好。感谢@highstakes

关于java - Spring jpa实体和Lombok,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35989683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com