我尝试使用SpringBoot + JPA + HIBERNATE + GRADLE + MYSQL制作一个糟糕的应用程序。我的问题是,当我点击@PostMapping
端点来保存用户时,数据库中保存了一个空对象。只生成了id
,但其他列显示null
。另外,当我点击@GetMapping
时,它返回空对象列表。我使用的是PostMan以供请求。
邮寄申请图片
创建
全部
Mysql数据库
生成分级
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.1'
id 'io.spring.dependency-management' version '1.1.0'
}
group = 'db.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '19'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
应用程序.属性
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.datasource.url=jdbc:mysql://localhost:3306/auto?autoReconnect=true&useSSL=false&createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
主应用程序.java
package db.example.autodbconnect;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AutodbconnectApplication {
public static void main(String[] args) {
SpringApplication.run(AutodbconnectApplication.class, args);
}
}
用户实体
package db.example.autodbconnect.entities;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String name;
}
用户JPA存储库
package db.example.autodbconnect.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import db.example.autodbconnect.entities.User;
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}
用户服务
package db.example.autodbconnect.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import db.example.autodbconnect.entities.User;
import db.example.autodbconnect.repositories.UserRepository;
@Service
public class UserService {
@Autowired
private UserRepository userRepo;
public User create(User body) {
return userRepo.save(body);
}
public List<User> findAll(){
List<User> res=userRepo.findAll();
return res;
}
}
用户控制器
package db.example.autodbconnect.contoller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import db.example.autodbconnect.entities.User;
import db.example.autodbconnect.service.UserService;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/create")
public ResponseEntity<User> create(@RequestBody User body) {
return new ResponseEntity<User>(userService.create(body), HttpStatus.CREATED);
}
@GetMapping("/all")
public ResponseEntity<List<User>> findALL(){
return new ResponseEntity<List<User>>(userService.findAll(),HttpStatus.OK);
}
}
1条答案
按热度按时间nszi6y051#
您没有为每个字段添加getter setter方法,这就是它存储空值的原因解决方案- 1-为每个字段添加getter setter方法,或者2-您还可以添加lombook依赖项并使用@Data注解,它将为您添加getter setter方法
希望对你有帮助