这是H2控制台我是一个想从youtube上学习春季靴的新手。我一直在遵循一步一步的过程,从一个通道连接一个数据库在春季启动。所以问题是我的User.java文件中的名称和Id部分即使在使用@Entity和@Id之后也没有在这里创建。即使重复同样的步骤,我也做不到。谁能解释一下我错过了什么?
这是我的Application.java文件
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DbApplication {
public static void main(String[] args) {
SpringApplication.run(DbApplication.class, args);
}
}
我的User.java文件
package com.example.demo;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
private int id;
private String name;
public String toString() {
return name+ " : " + id ;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
这是我的控制器文件
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
@Autowired
UserDAO userdao;
@RequestMapping("index")
public String user() {
return "index.jsp";
}
@RequestMapping("addUser")
public String addUser(User user) {
userdao.save(user);
return "index.jsp";
}
@RequestMapping("getUser")
public ModelAndView getUser(@RequestParam int id) {
ModelAndView mav = new ModelAndView("showUser.jsp");
User user = userdao.findById(id).orElse(null);
mav.addObject(user);
return mav;
}
}
这是我的application。properties
spring.h2.console.enabled= true
spring.datasource.platform= h2
spring.datasource.url=jdbc:h2:mem:testdb
我的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.manigandan</groupId>
<artifactId>DB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>DB</name>
<description>demo website</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>10.1.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
正如我理解你的问题,你想让Spring Boot根据实现的实体User创建一个数据库表。这个表应该命名为USER,并且应该有两列:ID和NAME。
当前应用程序。属性是
spring.h2.console.enabled= true
spring.datasource.platform= h2
spring.datasource.url=jdbc:h2:mem:testdb
请用这个代替
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.generate-ddl=true
spring.h2.console.enabled= true
spring.jpa.properties.hibernate.show_sql=true