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

时间:2017-07-17 21:16:41

标签: java hibernate spring-boot spring-data-jpa thymeleaf

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

在我的控制器类中,我将对象"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();    
}

控制器方法:

    @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>

2 个答案:

答案 0 :(得分:2)

如果您的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; 
    ... 
 }

然后,您的OrderRepository findAll()方法会提供包含Orders数据的Customer实体列表。您可以像在控制器中一样将该列表传递给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

}

答案 1 :(得分:0)

您将能够访问Order对象所具有的属性,因为您的服务类返回了一个Orders List,当循环浏览此列表时,您的viewmyorder将具有与Order相同的属性。

对于语法,引用:http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html#spring-model-attributes