我正在嘗試創建一個簡單的 Spring Boot 應用程式,該應用程式插入并顯示有關部門的一些詳細資訊。我遵循了創建應用程式所需的每一個步驟。
Eclipse 控制臺顯示它已正確創建了資料庫和表,但是在插入資料時它根本不起作用,即使在通過 mysql 作業臺手動將一些資料插入資料庫后,get one 和 get all 方法作業正常。
我的application.properties檔案:
server.port=9001
spring.datasource.url = jdbc:mysql://localhost:3306/department?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.dbcp2.test-while-idle=true
spring.datasource.dbcp2.validation-query= SELECT 1
# logging.level.org.springframework=DEBUG
# Show or not log for each sql query
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = create
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
我的Department.java物體類:
package com.micro2.departementservice.department.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long departmentId;
@Column(name="name")
private String deparmentName;
@Column(name = "address")
private String deparmentAddress;
@Column(name = "code")
private String departmentCode;
public Department() {
super();
}
public Department(long departmentId, String deparmentName, String deparmentAddress, String departmentCode) {
super();
this.departmentId = departmentId;
this.deparmentName = deparmentName;
this.deparmentAddress = deparmentAddress;
this.departmentCode = departmentCode;
}
public long getDepartmentId() {
return departmentId;
}
public void setDepartmentId(long departmentId) {
this.departmentId = departmentId;
}
public String getDeparmentName() {
return deparmentName;
}
public void setDeparmentName(String deparmentName) {
this.deparmentName = deparmentName;
}
public String getDeparmentAddress() {
return deparmentAddress;
}
public void setDeparmentAddress(String deparmentAddress) {
this.deparmentAddress = deparmentAddress;
}
public String getDepartmentCode() {
return departmentCode;
}
public void setDepartmentCode(String departmentCode) {
this.departmentCode = departmentCode;
}
}
該repository檔案:
package com.micro2.departementservice.department.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.micro2.departementservice.department.entity.Department;
@Repository
public interface DepartmentRepository extends CrudRepository<Department, Long>{
}
控制器:
package com.micro2.departementservice.department.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 com.micro2.departementservice.department.entity.Department;
import com.micro2.departementservice.department.repository.DepartmentRepository;
import lombok.extern.slf4j.Slf4j;
@RestController
@RequestMapping("/departments")
@Slf4j
public class DepartmentController {
@Autowired
private DepartmentRepository repository;
@PostMapping("/")
public Department saveDepartment(@RequestBody Department department) {
return repository.save(department);
}
@GetMapping("/{departmentId}")
public Department findDepartmentbyId(@PathVariable Long departmentId) {
return repository.findById(departmentId).get();
}
@GetMapping("/")
public List<Department> getAllDepartments() {
return (List<Department>) repository.findAll();
}
}
當我嘗試插入一些資料時,Eclipse 控制臺會顯示這一點:
Hibernate:
insert
into
department
(address, name, code)
values
(?, ?, ?)
我的郵遞員要求:
http://localhost:9001/departments/
{
"name":"AS",
"address":"123 Street",
"code":"AS-006"
}
它回傳:
{
"departmentId": 2,
"deparmentName": null,
"deparmentAddress": null,
"departmentCode": null
}
uj5u.com熱心網友回復:
您Department直接在休息控制器中使用物體類。
此類具有與成員名稱不一致的列名稱(例如,列名為 ,name但成員名為departmentName.
當您的原始 json 負載反序列化為 a 時Department,由于欄位不匹配,您將丟失要插入的資訊。
將您的有效載荷更改為
{
"departmentName":"AS",
"departmentAddress":"123 Street",
"departmentCode":"AS-006"
}
更好DTO的方法是在您的休息請求中按照您希望的方式創建一個帶有欄位的物件,然后將其映射到您的物體上。通常對于休息服務,這是首選方法。
例如
public class DepartmentDTO {
private String name;
private String address;
private String code;
// getters and setters
}
public class Department {
// your entity as it already is
}
然后在某處創建一個映射器類
@Component
public DepartmentMapper {
public Department toEntity(DepartmentDTO dto) {
Department entity = new Department();
entity.setDepartmentName(dto.getName());
// etc
return entity;
}
}
你的控制器然后變成
@RestController
@RequestMapping("/departments")
@Slf4j
public class DepartmentController {
@Autowired
private DepartmentRepository repository;
@Autowired
private DepartmentMapper departmentMapper;
@PostMapping("/")
public Department saveDepartment(@RequestBody DepartmentDTO dto) {
return repository.save(departmentMapper.toEntity(dto));
}
// etc
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/363268.html
上一篇:SpringDataJpaRepository在使用getById()方法時拋出LazyInitializationException而在使用findById()時不會拋出
