我试图创建一个功能性的delete按钮,使用自定义查询从数据库的表中删除一行。
我所做的每件事都会让我犯错误或无法工作。
存储库:
public interface ConsultationRepository extends Repository<Consultation, Integer> {
@Modifying
@Query("DELETE FROM Consultation c WHERE c.id =:id")
@Transactional
public String deleteConsultation(@Param("id") String consultationId);
void save(Consultation consultation) throws DataAccessException;
List<Consultation> findByChildId(Integer childId);
}
控制器:
@Controller
class ConsultationController {
private final ConsultationRepository consultations;
private final ChildRepository childs;
public ConsultationController(ConsultationRepository consultations, ChildRepository childs) {
this.consultations = consultations;
this.childs = childs;
}
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}
@ModelAttribute("consultation")
public Consultation loadChildWithVisit(@PathVariable("childId") int childId, Map<String, Object> model) {
Child child = this.childs.findById(childId);
child.setConsultationsInternal(this.consultations.findByChildId(childId));
model.put("child", child);
Consultation consultation = new Consultation();
child.addConsultation(consultation);
return consultation;
}
@GetMapping("/parents/*/childs/{childId}/consultations/new")
public String initNewConsultationForm(@PathVariable("childId") int childId, Map<String, Object> model) {
return "childs/createOrUpdateConsultationForm";
}
@PostMapping("/parents/{parentId}/childs/{childId}/consultations/new")
public String processNewConsultationForm(@Valid Consultation consultation, BindingResult result) {
if (result.hasErrors()) {
return "childs/createOrUpdateConsultationForm";
}
else {
this.consultations.save(consultation);
return "redirect:/parents/{parentId}";
}
}
@RequestMapping(value = "/delete_consultation", method = RequestMethod.GET)
public String handleDeleteConsultation(@RequestParam(name="consultationId") String consultationId) {
consultations.deleteConsultation(consultationId);
return "redirect:/parents";
}
}
带thymeleaf的html::
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org"
th:replace="~{fragments/layout :: layout (~{::body},'parents')}">
<body>
<h2>Informatii parinte</h2>
<table class="table table-striped" th:object="${parent}">
<tr>
<th>Nume si prenume</th>
<td><b th:text="*{firstName + ' ' + lastName}"></b></td>
</tr>
<tr>
<th>Adresa</th>
<td th:text="*{address}" /></td>
</tr>
<tr>
<th>Orasul</th>
<td th:text="*{city}" /></td>
</tr>
<tr>
<th>Telefon</th>
<td th:text="*{telephone}" /></td>
</tr>
</table>
<a th:href="@{{id}/edit(id=${parent.id})}" class="btn btn-default">Actualizeaza informatii parinte</a>
<a th:href="@{{id}/childs/new(id=${parent.id})}" class="btn btn-default">Adauga copil</a>
<br />
<br />
<br />
<h2>Copii si consultatii</h2>
<table class="table table-striped">
<tr th:each="child : ${parent.childs}">
<td valign="top">
<dl class="dl-horizontal">
<dt>Nume si prenume</dt>
<dd th:text="${child.name}" /></dd>
<dt>Data nasterii</dt>
<dd
th:text="${#temporals.format(child.birthDate, 'yyyy-MM-dd')}" /></dd>
<dt>Genul</dt>
<dd th:text="${child.type}" /></dd>
</dl>
</td>
<td valign="top">
<table class="table-condensed">
<thead>
<tr>
<th>Data consultatiei</th>
<th>Descriere</th>
</tr>
</thead>
<tr th:each="consultation : ${child.consultations}">
<td th:text="${#temporals.format(consultation.date, 'yyyy-MM-dd')}"></td>
<td th:text="${consultation?.description}"></td>
<td><a
th:href="@{/delete_consultation(consultationId=${consultation.consultationId})}"
class="btn btn-danger">
Sterge consultatie</a></td>
</tr>
<tr>
<td><a
th:href="@{{parentId}/childs/{childId}/edit(parentId=${parent.id},childId=${child.id})}">
Actualizeaza informatii copil
</a></td>
<td><a
th:href="@{{parentId}/childs/{childId}/consultations/new(parentId=${parent.id},childId=${child.id})}">
Adauga consultatie</a></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
我的方法不好?还是有其他更好的方法让它开始工作?
暂无答案!
目前还没有任何答案,快来回答吧!