gpt4 book ai didi

java - Thymeleaf:在同一页面上显示两个 JPA 实体表的数据

转载 作者:行者123 更新时间:2023-12-01 20:19:30 26 4
gpt4 key购买 nike

我正在使用 Spring Boot、Thymeleaf、Hibernate 和 JPA 来开发此应用程序。我已经映射了需要从中检索数据的两个实体之间的关系,并且想知道如何在客户查看其订单的页面上显示两个表中的数据。

在我的 Controller 类中,我将对象“viewmyorders”添加到 ModelAndView,其中将包含来自两个表/实体的 HQL 查询的结果。我应该在 Thymeleaf 模板上使用什么语法来在页面上显示数据?两个表的结果是否会先于 “${viewmyorder.}”“.” 之后是什么 - 数据库中相关列的名称?

感谢帮助!

订单类别:

@Entity
@Table(name = "orders")
public class Orders {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "orderno")
private int id;

@Column(name = "item_name")
private String itemName;

@Column(name = "description")
private String itemDescription;

@Column(name = "cost")
private String cost;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "orderId")
private Customer customerInfo;

//getters and setters
}

客户类别:

@Entity
@Table(name = "customer")
public class Customer {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "cust_id")
private int cust_id;

@Column(name = "fName")
private String firstName;

@Column(name = "surName")
private String surName;

@Column(name = "address")
private String address;

@Column(name = "phone")
private String phone;

@Column(name = "orderId")
private int orderId;

@Column(name = "active")
private int isactive;

@OneToOne(mappedBy="customerInfo")
private Orders orderInfo;

//getters and setters
}

存储库类:

public interface OrderRepository extends JpaRepository<Orders, Integer> {

//customerInfo is the object (of type orders) which maps the relationship between the two entities
String MYORDERQUERY = "SELECT ord, cust from Orders ord inner join ord.customerInfo cust";

@Query(MYSWAPSQUERY)
List<Orders> findMyOrders();
}

Controller 方法:

    @RequestMapping(value={"/viewmyorders"}, method = RequestMethod.GET)
public ModelAndView viewOrders(){
ModelAndView modelAndView = new ModelAndView();
//orderService calls method above from Repository class to return order info
modelAndView.addObject("viewmyorders", orderService.listAllOrders());
return modelAndView;
}

Thymeleaf 模板页面上的表格:

                    <table class="table table-striped">
<tr>
<th>Order Number</th>
<th>Product Description</th>
<th>Delivery Address</th>
<th>Contact Number</th>
</tr>
<tr th:each="viewmyorder : ${viewmyorders}">
<td th:text="${viewmyorder.orderno}"></td>
<td th:text="${viewmyorder.description}"></td>
<td th:text="${viewmyorder.address}"></td>
<td th:text="${viewmyorder.phone}"></td>
</tr>
</table>

最佳答案

如果您的 Orders 实体具有 Customer 字段,例如

 @Entity
class Orders {
...
@OneToOne
@JoinColumn(name = "customer_id")
private Customer customer;
...
}


@Entity
class Customer {
...
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "customer_id")
@NotNull
private Long id;
...
@OneToOne(mappedBy = "customer")
private Orders orders;
...
}

然后您的 OrderRepositoryfindAll() 方法会给出一个 Orders 实体列表,其中将包含 Customer数据也是如此。您可以像在 Controller 中一样将该列表传递到 Thymeleaf 模板,然后引用 Customer 数据,例如${viewmyorders.customer.address}

编辑:当然,不仅是 findAll(),任何返回所有 Orders 实体列的查询都将包含 Customer 数据,以及您的自定义查询的结果。

编辑#2:我想你想要这样的想法:数据库架构和实体:

CREATE TABLE orders (
order_id INT NOT NULL,
item_name VARCHAR(20),
description VARCHAR(50),
cost VARCHAR(20)
);


CREATE TABLE customer (
cust_id INT NOT NULL,
fName VARCHAR(20),
surName VARCHAR(20),
address VARCHAR(50),
phone VARCHAR(15),
active INT,
order_id INT,
FOREIGN KEY (order_id) REFERENCES orders (order_id)
);



@Entity
@Table(name = "orders")
public class Orders {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "order_id")
private int id;

@Column(name = "item_name")
private String itemName;

@Column(name = "description")
private String itemDescription;

@Column(name = "cost")
private String cost;

@OneToOne(mappedBy = "orderInfo", cascade = CascadeType.ALL)
private Customer customerInfo;

//getters and setters
}



@Entity
@Table(name = "customer")
public class Customer {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "cust_id")
private int cust_id;

@Column(name = "fName")
private String firstName;

@Column(name = "surName")
private String surName;

@Column(name = "address")
private String address;

@Column(name = "phone")
private String phone;

@Column(name = "active")
private int isactive;

@OneToOne
@JoinColumn(name = "order_id") // This will map the primary key of orders table to the column named order_id in customer table (see SQL above), you do not need to create a field for it in java entity
private Orders orderInfo;
// Later you can reference order_id through a Customer like customer.getOrderInfo().getId();

//getters and setters

}

关于java - Thymeleaf:在同一页面上显示两个 JPA 实体表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45153804/

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