You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+Thymeleaf全栈:多对一关联下展示指定酒店房间

实现指定酒店房间展示功能方案

1. 新增RoomRepository查询方法

在RoomRepository中添加根据酒店ID查询房间的自定义方法,JPA会自动解析方法名生成SQL:

public interface RoomRepository extends JpaRepository<Room, Long> {
    // 根据酒店ID查询所属房间列表
    List<Room> findByHotelId(Long hotelId);
}

2. 修改RoomController,添加酒店房间查询接口

新增接口接收酒店ID参数,查询对应房间并传递到页面:

@Controller
@RequestMapping("/rooms")
public class RoomController {

    @Autowired
    private RoomRepository roomRepository;
    @Autowired
    private HotelRepository hotelRepository;

    // 其他现有方法...

    // 根据酒店ID展示房间列表
    @GetMapping("/hotel/{hotelId}")
    public String getRoomsByHotel(@PathVariable Long hotelId, Model model) {
        // 查询目标酒店
        Hotel targetHotel = hotelRepository.findById(hotelId)
                .orElseThrow(() -> new IllegalArgumentException("无效的酒店ID: " + hotelId));
        // 查询该酒店下所有房间
        List<Room> hotelRooms = roomRepository.findByHotelId(hotelId);
        
        model.addAttribute("hotel", targetHotel);
        model.addAttribute("rooms", hotelRooms);
        return "rooms/hotel-rooms"; // 对应Thymeleaf页面
    }
}

3. 修改「View Rooms」链接指向新接口

在酒店列表页或详情页中,将「View Rooms」的跳转链接修改为携带酒店ID的地址:

<!-- 酒店列表页示例 -->
<a th:href="@{/rooms/hotel/{hotelId}(hotelId=${hotel.id})}" class="btn btn-info">View Rooms</a>

<!-- 酒店详情页示例(如果是在详情页) -->
<a th:href="@{/rooms/hotel/{hotelId}(hotelId=${hotel.id})}" class="btn btn-info">查看该酒店房间</a>

4. 创建/修改酒店房间列表页面(hotel-rooms.html)

编写Thymeleaf页面展示指定酒店的房间信息:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${hotel.name} + ' - 房间列表'">酒店房间列表</title>
</head>
<body>
    <div class="container">
        <h1 th:text="${hotel.name}">酒店名称</h1>
        <p th:text="${hotel.address + ', ' + hotel.city + ', ' + hotel.country}">酒店地址</p>
        
        <h3>房间列表</h3>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>房间号</th>
                    <th>房型</th>
                    <th>价格</th>
                    <th>状态</th>
                </tr>
            </thead>
            <tbody>
                <tr th:each="room : ${rooms}">
                    <td th:text="${room.roomNumber}">101</td>
                    <td th:text="${room.type}">单人间</td>
                    <td th:text="${room.price}">¥200</td>
                    <td th:text="${room.available ? '空闲' : '已占用'}">空闲</td>
                </tr>
            </tbody>
        </table>
        
        <a th:href="@{/hotels}" class="btn btn-secondary">返回酒店列表</a>
    </div>
</body>
</html>

5. 验证实体关联配置

确保Room与Hotel的多对一关联配置正确,避免查询异常:

// Room实体类
@Entity
public class Room {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String roomNumber;
    private String type;
    private BigDecimal price;
    private boolean available;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "hotel_id") // 数据库外键字段名
    private Hotel hotel;

    // Getter、Setter方法
}

// Hotel实体类
@Entity
public class Hotel {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String address;
    private String city;
    private String country;

    @OneToMany(mappedBy = "hotel", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Room> rooms = new ArrayList<>();

    // Getter、Setter方法
}

内容的提问来源于stack exchange,提问作者nikolaPantelic98

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 21:35:20