Пружинная загрузка 2.5
Контроллер:
@org.springframework.stereotype.Controller
public class OrdersController {
@Value("${spring.application.name}")
private String appName;
@Autowired
private OrderRepository orderRepository;
@Autowired
private CategoryRepository categoryRepository;
private static Logger logger = LogManager.getLogger(OrdersController.class);
@GetMapping("/orders")
public String getAllOrders(Model model) {
model.addAttribute("ordersList", orderRepository.findAll());
model.addAttribute("appName", appName);
return "orders";
}
заказов. html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="${appName}">Order template title</title>
<link th:href="@{/public/style.css}" rel="stylesheet"/>
<meta charset="UTF-8"/>
</head>
<body>
<div class="container entity_list">
<h2>Orders</h2>
<br/>
<table>
<tr>
<td colspan="3" align="left" th:text="'Total count: ' + ${ordersList.size()}"/>
<td colspan="3" align="right"><a th:href="@{/order/add}">Add</a></td>
</tr>
<tr>
<th width="50">ID</th>
<th width="120">Name</th>
<th width="200">Created At</th>
<th width="200">Updated At</th>
<th width="200">Category</th>
<th width="60"></th>
<th width="60"></th>
</tr>
<th:block th:each="order : ${ordersList}">
<tr>
<td th:text="${{order.id}}"/>
<td><a th:href="@{/order/view/{id}(id=${order.id})}"/><span th:text="${order.name}"/></td>
<td th:text="${{order.created}}"/>
<td th:text="${{order.updated}}"/>
<td th:text="${{order.category}}"/>
<td><a th:href="@{/order/edit/{id}(id=${order.id})}">Edit</a></td>
<td><a th:href="@{/order/delete/{id}(id=${order.id})}">Delete</a></td>
</tr>
</th:block>
</table>
</div>
</body>
</html>
jpa:
import org.springframework.data.repository.CrudRepository;
import java.util.List;
public interface OrderRepository extends CrudRepository<Orders, Integer> {
// Spring Data - use JPQL -> generate SQL query on runtime
public List<Orders> findByName(String name);
public List<Orders> findByNameOrderById(String name);
}
Модели:
@Entity
public class Orders {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@NotNull
private String name;
private String description;
@NotNull
@DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
private Date created;
@DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
private Date updated;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private Category category;
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@NotNull
private String name;
private String description;
@NotNull
@DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
private Date created;
@DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
private Date updated;
@OneToOne(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "category")
private Orders orders;
И вот результат:
Как вы можете увидеть идентификатор категории печати. Но мне нужно напечатать название категории.