gpt4 book ai didi

java - 将数据添加到数据库(相关表)Spring Boot

转载 作者:行者123 更新时间:2023-11-30 07:43:03 26 4
gpt4 key购买 nike

我的英语很差,但我正在尝试描述我的问题。我是 Spring 的新人。我在向数据库中添加数据时遇到了一些问题。我必须列出 Pc 和 Pc 特征。它们通过 Id 关联。在非关联表中添加数据很容易,但是如何在关联表中添加数据呢?我应该在 Controller 中写什么?下面有一些类。

电脑类:

@Entity
@Table(name = "pc")
public class Pc {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

private String name;
private int price;

public Pc(){}

public Pc(String name, int price) {
this.name = name;
this.price = price;
}

@OneToMany
@JoinColumn(name = "pc_id")
private List<PcChars> chars = new ArrayList<>();

public List<PcChars> getChars() {
return chars;
}

public void setChars(List<PcChars> chars) {
this.chars = chars;
}

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;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

PcChars 类:

@Entity
@Table(name = "pcChars")
public class PcChars {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;


private String name;
private String value;



public PcChars(){}

public PcChars(String name, String value) {
this.name = name;
this.value = value;
}

@ManyToOne
private Pc pc;



public Pc getPc() {
return pc;
}

public void setPc(Pc pc) {
this.pc = pc;
}

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;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

PcCharactsController:

@Controller
public class PcCharactsController {

final private PcRepo pcRepo;
final private PcCharRepo pcCharRepo;

public PcCharactsController(PcRepo pcRepo, PcCharRepo pcCharRepo) {
this.pcRepo = pcRepo;
this.pcCharRepo = pcCharRepo;
}

//Pc characteristics list
@GetMapping("pc/{id}/")
public String pcCharList(@PathVariable int id, Model model) throws Exception{

Pc pc = pcRepo.findById(id).orElseThrow(() -> new Exception("PostId " +
id + " not found"));
List<PcChars> pcChars = pc.getChars();
model.addAttribute("model", pc.getName());
model.addAttribute("pcChars", pcChars);
return "charList";
}

//add characteristic
@PostMapping("pc/{id}/")
public String addCharact(){

return "charList";
}

特性.ftl:

<html>
<head>
<title>Ho</title>
</head>
<body>
<div>
<form method="post" action="/pc/${id}/">
<input type="text" name="name">
<input type="text" value="value">
<input type="hidden" name="pc_id" value="${id}">
<button type="submit">Add</button>
</form>
</div>
</body>
</html>

最佳答案

由于您没有使用任何 modelAttribute 将输入值直接绑定(bind)到 POJO,您可以使用简单的 HttpServletRequest 获取输入属性,使用它们创建对象你想使用Hibernate存储存储

@PostMapping("pc/{id}/")
public String addCharact(HttpServletRequest req){
String name = req.getParameter("name");
String value = req.getParameter("value");
String id = req.getParameter("id");
PcChars pcchars = new PcChars(name,value,id); // create the corresponding constructor
SessionFactory sessionFactory;
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.getTransaction();
tx.begin();
session.save(pcchars);
tx.commit();
}
catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return "charList";
}

关于java - 将数据添加到数据库(相关表)Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53806396/

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