Wenn wir ein Backend für die Website erstellen. Wir werden natürlich Kategorien und Beiträge/Produkte erstellen. Zum Hinzufügen/Bearbeiten/Löschen von Kategorien erstellen wir natürlich eine tabellenähnliche Struktur. Beispiel: This is category listing as tableHolen Sie sich die Anzahl der Kinder mit Ruhezustand in springmvc
Kategorie würde eine untergeordnete Kategorie in einer Tabelle haben, die Anzahl der untergeordneten Kategorien könnte 1 - viele sein. Ich möchte die Nummer der Kinderkategorie zeigen, wie ich oben erwähnt habe.
Als CI Entwickler i, dass
in der Ansicht von mit Query zu tun verwendenHier ist das Beispiel
<?php
if(sizeof($results)>0)
{
$i=1;
foreach($results as $key => $list)
{
if($i%2==0)$cls="row1"; else $cls="row2";
?>
<tr id="<?php echo $list->id;?>" class="<?php echo $cls; ?>">
<td> <?php echo $list->title, anchor("admin/categories/update/".$list->id."/", '<i class="fa fa-pencil rtrt"></i>'); ?></td>
<td>
<?php
// it will return number of childs
echo anchor ('admin/categories/category/'.$list->id, $this->Common->select_child ('tbl_category', $list->id));
?>
</td>
Im Moment bin ich mit Spring-mvc, Hibernate, Jpa und Mysql DB. Ich bin bekannt, dass SQL-Abfrage von einem JSP eine schlechte Übung wäre.
Hier ist meine Jsp-Code für die Tabelle wie Struktur,
<table class="table table-striped">
<thead>
<tr>
<th><i class="fa fa-pencil"></i></th>
<th>Category Name</th>
<th>Child</th>
<th>Created</th>
<th>Updated</th>
<th><input type="checkbox" /></th>
</tr>
</thead>
<tbody>
<c:if test="${not empty category}">
<c:forEach var="cat" items="${category}">
<tr>
<td><a href="${pageContext.request.contextPath}/category/${cat.id}/edit"><i class="fa fa-pencil"></i></a></td>
<td>${cat.title}</td>
<td><a href="${pageContext.request.contextPath}/category/show/${cat.id}"><i class="fa fa-folder-open"></i></a></td>
<td>${cat.created}</td>
<td>${cat.updated}</td>
<td><input type="checkbox" /></td>
</tr>
</c:forEach>
</c:if>
Hier ist die Kategorie Pojo Klasse
@Entity
@Table(name = "categories")
public class Categories {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
private Long parent_id;
private String title;
private String url_title;
private String description;
private String created;
private String updated;
/*
* @ At the time of Creating new user
* Auto persist created date
* Auto persist updated for first time
*/
@PrePersist
protected void onCreate(){
// Date conversion to string
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd h:mm:s");
sdf.setLenient(false);
String now = sdf.format(date);
created = now;
updated = now;
}
/*
* Respective Getter and Setter Methods
*
*/
public Long getId() {
return id;
}public void setId(Long id) {
this.id = id;
}
public Long getParent_id() {
return parent_id;
}public void setParent_id(Long parent_id) {
this.parent_id = parent_id;
}
public String getTitle() {
return title;
}public void setTitle(String title) {
this.title = title;
}
public String getUrl_title() {
return url_title;
}public void setUrl_title(String url_title) {
this.url_title = url_title;
}
public String getCreated() {
return created;
}public void setCreated(String created) {
this.created = created;
}
public String getUpdated() {
return updated;
}public void setUpdated(String updated) {
this.updated = updated;
}
public String getDescription() {
return description;
}public void setDescription(String description) {
this.description = description;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Categories other = (Categories) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
Die Frage ist
Wie kann ich Kinder zählen ohne SQL-Abfrage von Jsp? Bitte helfen Sie mir, ich brauche einen Vorschlag.
Können Sie die Kategorie entity pleaz hinzufügen? –
Ich habe gerade die Kategorien Klasse hinzugefügt? kannst du mir bitte vorschlagen, wie ich das erreiche, was ich brauche. –
Ich habe eine Antwort geschrieben, aber ich weiß nicht, was Sie wirklich meinen: ** Kinder zählen ** –