我对 Spring 还很陌生,我正在关注的在线讲座有点过时了。我制作了一个非常简单的 html 表单,可以将数据添加到数据库中。我可以创建该表,但无法使用 data.sql 文件中的静态数据填充该表。不过,我可以使用连接的 html 表单将数据添加到表中。
实体
package com.me.practice.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name="location_entity")
public class Location {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String country, st;
public Location() {
}
public Location(String country, String st) {
this.country = country;
this.st = st;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getSt() {
return st;
}
public void setSt(String st) {
this.st = st;
}
}
HTML 表单
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Location</title>
</head>
<body>
<form action="/new-location/save" method="post" th:object="${loc}">
<p>Country<input type="text" th:field="${loc.country}"/></p>
<p>State<input type="text" th:field="${loc.st}"/></p>
<button type="submit">Submit</button>
</form>
</body>
</html>
存储库界面
package com.me.practice.dao;
import org.springframework.data.repository.CrudRepository;
import com.me.practice.entity.Location;
public interface LocRepo extends CrudRepository<Location, Long>{
}
控制器
package com.me.practice.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.me.practice.dao.LocRepo;
import com.me.practice.entity.Location;
@Controller
@RequestMapping("/new-location")
public class LocationController {
@Autowired
LocRepo locRepo;
@GetMapping("/new")
public String newLocation(Model mdl) {
Location location = new Location();
mdl.addAttribute("loc", location);
return "locationForm";
}
@PostMapping("/save")
public String saveLocation(Location location, Model model) {
locRepo.save(location);
return "redirect:/new-location/new";
}
}
主要方法
package com.me.practice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
@SpringBootApplication
@EntityScan("com.me.practice.entity")
public class PracticeApplication {
public static void main(String[] args) {
SpringApplication.run(PracticeApplication.class, args);
}
}
应用属性
spring.h2.console.enabled=true
spring.h2.console.path=/h2.console
spring.jpa.show-sql=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.session.jdbc.initialize-schema=always
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.defer-datasource-initialization=true
spring.sql.init.continue-on-error=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.sql.init.mode=always
spring.thymeleaf.cache=false
spring.datasource.username=root
spring.datasource.password=root
spring.sql.init.platform=h2
spring.sql.init.data-locations=classpath:db/data.sql
数据.sql
INSERT INTO LocationEntity (country, st) VALUES ('Brazil', 'Rio de Janeiro');
INSERT INTO LocationEntity (country, st) VALUES ('USA', 'Texas');
INSERT INTO LocationEntity (country, st) VALUES ('Japan', 'Hokkaido');
在
newLocation
函数中,您需要从存储库获取 Location
实体,并检查它是否具有要在网页上显示的数据,例如。
Location location = locRepo.getAll();
System.out.println("Country=" + location.getCountry);
查看此讨论以获取更多信息