Java Webベースの宿舎管理システム開発ガイド

宿舎管理システムは、Java Web技術を使用したWebアプリケーションです。3つの異なるロール(学生、管理者、宿舎担当者)を持つ多層的なログインシステムを実装しています。各ユーザーには異なる権限と機能が割り当てられています。

技術スタック概要

このプロジェクトでは以下の技術を使用しています:

  • JSPおよびServlet
  • MySQLデータベース
  • Tomcatサーブレットコンテナ

開発環境構成:

  • IDEAまたはEclipse統合開発環境
  • MySQLデータベース
  • JDK開発キット
  • Tomcatアプリケーションサーバー

機能モジュール詳細

システムは以下の主要機能を提供します:

ホーム画面機能

複数のロールによるログイン機能を提供します。学生、宿舎担当者、管理者それぞれに専用のログインインターフェースがあり、各ユーザーの役割に応じた異なる機能セットが利用可能です。

学生モジュール

学生ユーザーは以下の操作ができます:

  • 欠席記録の確認
  • パスワード変更機能
  • 個人用ダッシュボード表示

管理者モジュール

管理者ユーザーには以下の機能があります:

  • 学生情報の完全な管理(CRUD操作)
  • 宿舎棟の管理
  • 学生の欠席記録閲覧
  • アカウントパスワード更新

宿舎担当者モジュール

宿舎担当者は以下の機能を利用できます:

  • 登録学生情報の閲覧
  • 学生欠席記録の追加
  • アカウントセキュリティ設定変更

実装例:Spring Bootベースの宿舎管理システム

以下はSpring Bootフレームワークを使用した宿舎管理システムの実装サンプルです。バックエンドにはSpring Boot、フロントエンドテンプレートエンジンにはThymeleaf、データベースにはMySQLを使用しています。

システム機能

  • 学生情報の完全管理(作成、読取、更新、削除)
  • 宿舎情報の完全管理(作成、読取、更新、削除)
  • 宿舎割当機能
  • 宿舎割当状況照会

技術スタック構成

  • バックエンド:Spring Boot、Spring MVC、Spring Data JPA
  • フロントエンド:HTML、CSS、JavaScript(Bootstrapフレームワーク使用)
  • データベース:MySQL
  • ビルドツール:Maven

データベース設計

以下のSQL構文でデータベーステーブルを作成します:

CREATE DATABASE housing_management;

USE housing_management;

-- 生徒情報テーブル
CREATE TABLE resident (
    id INT AUTO_INCREMENT PRIMARY KEY,
    full_name VARCHAR(50) NOT NULL,
    sex VARCHAR(10),
    contact_number VARCHAR(20),
    accommodation_id INT
);

-- 宿舎情報テーブル
CREATE TABLE accommodation (
    id INT AUTO_INCREMENT PRIMARY KEY,
    room_code VARCHAR(20) NOT NULL,
    max_capacity INT NOT NULL,
    current_residents INT DEFAULT 0
);

プロジェクト構造

housing-management/
├── src/
│   ├── main/
│   │   ├── java/com/example/housing/
│   │   │   ├── controller/
│   │   │   │   ├── ResidentController.java
│   │   │   │   ├── AccommodationController.java
│   │   │   ├── entity/
│   │   │   │   ├── Resident.java
│   │   │   │   ├── Accommodation.java
│   │   │   ├── repository/
│   │   │   │   ├── ResidentRepository.java
│   │   │   │   ├── AccommodationRepository.java
│   │   │   ├── HousingManagementApplication.java
│   │   ├── resources/
│   │   │   ├── templates/
│   │   │   │   ├── resident.html
│   │   │   │   ├── accommodation.html
│   │   │   │   ├── home.html
│   │   │   ├── application.properties
│   │   │   ├── static/
│   │   │   │   ├── css/
│   │   │   │   │   ├── main.css
│   │   │   │   ├── js/
│   │   │   │   │   ├── main.js

主要コード実装

application.properties設定

spring.datasource.url=jdbc:mysql://localhost:3306/housing_management?useSSL=false&serverTimezone=Asia/Tokyo
spring.datasource.username=root
spring.datasource.password=your_database_password
spring.jpa.hibernate.ddl-auto=update
spring.thymeleaf.cache=false

エンティティクラス定義

Resident.java
package com.example.housing.entity;

import jakarta.persistence.*;

@Entity
public class Resident {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer identifier;

    private String fullName;
    private String sex;
    private String contactNumber;

    @ManyToOne
    @JoinColumn(name = "accommodation_id")
    private Accommodation assignedAccommodation;

    // GetterおよびSetterメソッド
}
Accommodation.java
package com.example.housing.entity;

import jakarta.persistence.*;
import java.util.List;

@Entity
public class Accommodation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer identifier;

    private String roomCode;
    private Integer maxCapacity;
    private Integer currentResidents;

    @OneToMany(mappedBy = "assignedAccommodation")
    private List<Resident> residents;

    // GetterおよびSetterメソッド
}

コントローラー実装

ResidentController.java
package com.example.housing.controller;

import com.example.housing.entity.Resident;
import com.example.housing.repository.ResidentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/residents")
public class ResidentController {

    @Autowired
    private ResidentRepository residentRepo;

    @GetMapping
    public String showResidents(Model model) {
        model.addAttribute("residents", residentRepo.findAll());
        return "resident";
    }

    @GetMapping("/create")
    public String createResidentForm(Model model) {
        model.addAttribute("resident", new Resident());
        return "add-resident";
    }

    @PostMapping("/store")
    public String storeResident(@ModelAttribute Resident resident) {
        residentRepo.save(resident);
        return "redirect:/residents";
    }

    @GetMapping("/remove/{id}")
    public String removeResident(@PathVariable Integer id) {
        residentRepo.deleteById(id);
        return "redirect:/residents";
    }
}
AccommodationController.java
package com.example.housing.controller;

import com.example.housing.entity.Accommodation;
import com.example.housing.repository.AccommodationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/accommodations")
public class AccommodationController {

    @Autowired
    private AccommodationRepository accommodationRepo;

    @GetMapping
    public String showAccommodations(Model model) {
        model.addAttribute("accommodations", accommodationRepo.findAll());
        return "accommodation";
    }

    @GetMapping("/create")
    public String createAccommodationForm(Model model) {
        model.addAttribute("accommodation", new Accommodation());
        return "add-accommodation";
    }

    @PostMapping("/store")
    public String storeAccommodation(@ModelAttribute Accommodation accommodation) {
        accommodationRepo.save(accommodation);
        return "redirect:/accommodations";
    }

    @GetMapping("/remove/{id}")
    public String removeAccommodation(@PathVariable Integer id) {
        accommodationRepo.deleteById(id);
        return "redirect:/accommodations";
    }
}

フロントエンドページ

resident.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>居住者管理</title>
    <link rel="stylesheet" th:href="@{/css/main.css}">
</head>
<body>
<h1>居住者一覧</h1>
<a href="/residents/create">居住者追加</a>
<table border="1">
    <tr>
        <th>ID</th>
        <th>氏名</th>
        <th>性別</th>
        <th>連絡先</th>
        <th>部屋番号</th>
        <th>操作</th>
    </tr>
    <tr th:each="resident : ${residents}">
        <td th:text="${resident.identifier}"></td>
        <td th:text="${resident.fullName}"></td>
        <td th:text="${resident.sex}"></td>
        <td th:text="${resident.contactNumber}"></td>
        <td th:text="${resident.assignedAccommodation.roomCode}"></td>
        <td>
            <a th:href="@{/residents/remove/{id}(id=${resident.identifier})}">削除</a>
        </td>
    </tr>
</table>
</body>
</html>
accommodation.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>宿舎管理</title>
    <link rel="stylesheet" th:href="@{/css/main.css}">
</head>
<body>
<h1>宿舎一覧</h1>
<a href="/accommodations/create">宿舎追加</a>
<table border="1">
    <tr>
        <th>ID</th>
        <th>部屋コード</th>
        <th>最大収容数</th>
        <th>現在の入居数</th>
        <th>操作</th>
    </tr>
    <tr th:each="accommodation : ${accommodations}">
        <td th:text="${accommodation.identifier}"></td>
        <td th:text="${accommodation.roomCode}"></td>
        <td th:text="${accommodation.maxCapacity}"></td>
        <td th:text="${accommodation.currentResidents}"></td>
        <td>
            <a th:href="@{/accommodations/remove/{id}(id=${accommodation.identifier})}">削除</a>
        </td>
    </tr>
</table>
</body>
</html>

実行手順

  1. MySQLデータベースを作成し、SQLスクリプトを実行してテーブル構造を生成します。
  2. application.propertiesファイル内のデータベース接続情報を適切に設定します。
  3. Mavenを使用してプロジェクトをビルド:mvn clean install
  4. Spring Bootアプリケーションを起動:mvn spring-boot:run
  5. ブラウザで http://localhost:8080/residents または http://localhost:8080/accommodations にアクセスします。

タグ: java-web spring-boot MySQL Thymeleaf dormitory-management

6月19日 21:03 投稿