【问题标题】:Join two tables and show the retrieved information in thymeleaf table连接两个表,并在 thymeleaf 表中显示检索到的信息
【发布时间】:2021-09-06 12:32:33
【问题描述】:

我有两个表,第一个是 Filiere,第二个是 Cycle,通过一对多关系相关,Filiere 可以有一个 Cycle,Cycle 可以有多个 Filiere,我想显示所有 filieres 的信息,包括使用 thymeleaf 在表中循环名称

package org.pfe.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Cycle implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(unique=true,nullable = false)
    private String nom;
    
    
    @OneToMany(mappedBy="cyc")
    private Set<Filiere> filList=new HashSet<>();


    public Cycle() {
        super();
    }


    public Cycle(String nom, Set<Filiere> filList) {
        super();
        this.nom = nom;
        this.filList = filList;
    }


    public String getNom() {
        return nom;
    }


    public void setNom(String nom) {
        this.nom = nom;
    }


    public Set<Filiere> getFilList() {
        return filList;
    }


    public void setFilList(Set<Filiere> filList) {
        this.filList = filList;
    }


    public Long getId() {
        return id;
    }
    


    
    
    
}

package org.pfe.entities;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springframework.format.annotation.DateTimeFormat;

@Entity
public class Filiere {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @NotBlank(message = "ne doit pas etre null" )
    @NotNull
    private String nom;
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @NotNull(message = "la date ne doit pas etre null")
    private Date dateCreation;
    
    
    @ManyToOne
    @JoinColumn(name="dept_id")
    private Departement dept;
    
    
    @ManyToOne
    @JoinColumn(name="cycle_id")
    private Cycle cyc;
    
    
    

}
你能尽快帮助我吗

【问题讨论】:

    标签: spring-boot spring-mvc spring-data-jpa


    【解决方案1】:

    你把循环对象放到模型上。

    例如:

    model.addAttribute("cycleList", listOfCycle);

    百里香

     <tr th:each="cycle:${cycleList}">
                <td th:text="${cycle.id}"></td>
                <td th:text="${cycle.nom}"></td>
                <td>
                    <div th:each="filList:${cycle.filList}">
                        <p th:text="${filList.id}"></p>
                        <p th:text="${filList.nom}"></p>
                        <p th:text="${filList.dateCreation}"></p>
                    </div>
                </td>
            </tr>
    

    这就是您可以在百里香中获取数据的方式。

    【讨论】:

      猜你喜欢
      • 2018-05-18
      • 2014-04-11
      • 2023-04-07
      • 2016-09-20
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多