gpt4 book ai didi

java - CrudRepository 无法对 h2 db 执行任何查询

转载 作者:行者123 更新时间:2023-12-02 09:06:06 25 4
gpt4 key购买 nike

我是 Spring 数据新手。我的应用程序中有一个 h2 数据库以及一个休息 Controller 。本质上,我有一些端点,您可以调用它们来执行某些查询。端点将使用扩展 crudrepository 的 itnerface 并执行查询。问题是我调用端点,但 crudrepository 似乎在数据库中找不到任何内容。我访问了数据库控制台,可以看到我预填充的数据,我使用 data.sql 为数据库播种了这些数据。

这就是我所拥有的:

@Repository
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
}

这是我的实体:

@Entity

public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstname;

public Employee(){}

public Employee(String firstname, String lastname, String email) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

private String lastname;
private String email;
}

Controller :

@RestController 
public class EmployeeController {

@Autowired
EmployeeService employeeServices;

@GetMapping("/getAllEmployees")
public void getAllEmployees(){

//Add employee to db via spring data
employeeServices.getAllEmployees();
}

@GetMapping("/deleteEmployee/{id}")
public void delete(@PathVariable Long id){

//Delete employee to db via spring data
employeeServices.deleteEmployee(id);
System.out.printf("Deleted");
}

@PostMapping("/createEmployee")
public void create(@RequestBody Employee employee){
//Add employee to db via spring data

Employee save = employeeServices.createEmployee(employee);
System.out.println("created: " + save.toString());

}

}

我的服务:

@Service
public class EmployeeService
{
@Autowired
EmployeeRepository employeeRepository;

public void getAllEmployees(){
List<Employee> listOfEmployees = new ArrayList<>();
employeeRepository.findAll().forEach(employee -> listOfEmployees.add(employee));

System.out.println(listOfEmployees);
}

public void deleteEmployee(Long id){
employeeRepository.deleteById(id);
System.out.println("deleted.");
}

public Employee createEmployee(Employee employee){
employeeRepository.save(employee);
System.out.println("saved.");
return employee;
}
}

这是我的 application.properties:

#Server
server.port=9975
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:file:./fileOrDbName
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

还有我的 data.sql:

DROP TABLE IF EXISTS TBL_EMPLOYEES; CREATE TABLE TBL_EMPLOYEES (id INT AUTO_INCREMENT  PRIMARY KEY, first_name VARCHAR(250) NOT NULL, last_name VARCHAR(250) NOT NULL, email VARCHAR(250) DEFAULT NULL); INSERT INTO TBL_EMPLOYEES (first_name, last_name, email) VALUES ('Lokesh', 'Gupta', 'abc@gmail.com'), ('Caption', 'America', 'cap@marvel.com');

应用程序启动正常,我可以通过 localhost:9975/h2 访问数据库控制台,但 Controller crud 似乎无法访问数据库。有什么想法吗?

最佳答案

发现问题了。这是因为我没有使用 @Column(name="..") 注释实体类中的字段,也没有使用 Table(name="TBL_EMPLOYEES “)。还需要在 application.properties 文件中添加以下内容 spring.jpa.hibernate.ddl-auto=none
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

关于java - CrudRepository 无法对 h2 db 执行任何查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59803986/

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