Spring boot SpringBoot将数据存储在h2数据库中,但它可以';t获取数据&;按id在浏览器上显示它
我已经用3个变量创建了alien.class,通过使用data.mysql文件,我将数据提取到h2数据库中&我想在web浏览器上访问我的数据。当我输入aid并单击Submit按钮时,我应该获得特定的alien id值,但我得到的是-->${alien} 1.外国人阶级Spring boot SpringBoot将数据存储在h2数据库中,但它可以';t获取数据&;按id在浏览器上显示它,spring-boot,h2,Spring Boot,H2,我已经用3个变量创建了alien.class,通过使用data.mysql文件,我将数据提取到h2数据库中&我想在web浏览器上访问我的数据。当我输入aid并单击Submit按钮时,我应该获得特定的alien id值,但我得到的是-->${alien} 1.外国人阶级 package com.telusko.demo.model; import javax.persistence.Entity; import javax.persistence.Id; import org.springfra
package com.telusko.demo.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.springframework.stereotype.Component;
@Entity
@Component
public class Alien
{
@Id
private int aid;
private String aname;
private String tech;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
public String getTech() {
return tech;
}
public void setTech(String tech) {
this.tech = tech;
}
@Override
public String toString() {
return "Alien [aid=" + aid + ", aname=" + aname + ", tech=" + tech + "]";
}
}
2.外星控制员
package com.telusko.demo.controller;
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.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.telusko.demo.dao.AlienRepo;
import com.telusko.demo.model.Alien;
@Controller
public class AlienController
{
@Autowired
AlienRepo repo;
@RequestMapping("/")
public String home()
{
return "home";
}
@RequestMapping("/addAlien")
public String addAlien(Alien alien)
{
repo.save(alien);
return "home";
}
@RequestMapping("/getAlien")
public ModelAndView getAlien(@RequestParam int aid )
{
ModelAndView mv = new ModelAndView("showAlien");
Alien alien = repo.findById(aid).orElse(new Alien());
mv.addObject(alien);
return mv;
}
}
3.home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="addAlien">
<input type="text" name="aid"><br>
<input type="text" name="aname"><br>
<input type="text" name="tech"><br>
<input type="submit"><br>
</form>
<form action="getAlien">
<input type="text" name="aid"><br>
<input type="submit"><br>
</form>
</body>
</html>
6.应用程序属性
server.port=8085
spring.profiles.active=@spring.profiles.active@
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:vikram
spring.jpa.open-in-view=false
server.error.whitelabel.enabled=false
spring.jpa.defer-datasource-initialization=true
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.jsp
spring.datasource.username=sa
spring.datasource.password=
7.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>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.telusko</groupId>
<artifactId>bootjpaa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>bootjpaa</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.45</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<packaging>war</packaging>
</project>
4.0.0
org.springframework.boot
spring启动程序父级
2.5.0
com.telusko
bootjpaa
0.0.1-快照
bootjpaa
SpringBoot的演示项目
1.8
org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
SpringBootStarterWeb
org.apache.tomcat
雄猫贾斯珀
9.0.45
com.h2数据库
氢
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
弹簧靴启动器jdbc
org.springframework.boot
弹簧启动装置
org.springframework.boot
springbootmaven插件
战争
server.port=8085
spring.profiles.active=@spring.profiles.active@
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:vikram
spring.jpa.open-in-view=false
server.error.whitelabel.enabled=false
spring.jpa.defer-datasource-initialization=true
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.jsp
spring.datasource.username=sa
spring.datasource.password=
<?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>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.telusko</groupId>
<artifactId>bootjpaa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>bootjpaa</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.45</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<packaging>war</packaging>
</project>