1 Commits

Author SHA1 Message Date
1dbc3c6db0 Q2 (HTML) Done 2026-10-08 16:47:49 +09:00
23 changed files with 404 additions and 121 deletions

View File

@@ -1,42 +1,112 @@
package com.example.demo.controller;
import java.util.List;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import com.example.demo.entity.Employee;
import com.example.demo.form.EmployeeForm;
import com.example.demo.form.LoginForm;
import com.example.demo.service.EmployeeService;
@Controller
public class MainController {
@RequestMapping(value = "/",method = RequestMethod.GET)
public String loin() {
return"login";
}
@RequestMapping(value = "/doLogin",method = RequestMethod.POST)
public String doLoin() {
return"menu";
}
@RequestMapping(value = "/logout",method = RequestMethod.POST)
public String logout() {
return "login";
}
@RequestMapping(value = "/select",method = RequestMethod.POST)
public String select() {
return "select";
}
@RequestMapping(value = "/back",method = RequestMethod.POST)
public String back() {
return "menu";
}
@RequestMapping(value = "/insert",method = RequestMethod.POST)
public String insert() {
return "insert";
}
@RequestMapping(value = "/update",method = RequestMethod.POST)
public String update() {
return "update";
}
@RequestMapping(value = "/delete",method = RequestMethod.POST)
public String delete() {
return "delete";
}
}
@Autowired
private HttpSession session;
@Autowired
private EmployeeService employeeService;
@GetMapping("/")
public String login(Model model) {
model.addAttribute("loginForm", new LoginForm());
return "login";
}
@PostMapping("/doLogin")
public String doLogin(@ModelAttribute LoginForm form, Model model) {
var employeeOpt = employeeService.login(form.getMail(), form.getPass());
if (employeeOpt.isPresent()) {
session.setAttribute("login_emp", employeeOpt.get());
return "menu";
}
model.addAttribute("error", "メールアドレスまたはパスワードが違います");
return "login";
}
@PostMapping("/logout")
public String logout() {
session.invalidate(); // Session ကို ဖျက်ပစ်ခြင်း
return "redirect:/";
}
@PostMapping("/back")
public String back() {
return "menu";
}
// --- ရှာဖွေခြင်း (select) ---
@PostMapping("/select")
public String select(Model model) {
List<Employee> list = employeeService.getAllEmployee();
model.addAttribute("employeeList",list);
return "select";
}
// --- သစ်ထည့်ခြင်း (insert) ---
@PostMapping("/insert")
public String insert(Model model) {
model.addAttribute("employeeForm", new EmployeeForm());
return "insert";
}
@PostMapping("/insertConfirm")
public String insertConfirm(@ModelAttribute EmployeeForm form, Model model) {
model.addAttribute("employeeForm", form);
return "insertConfirm";
}
@PostMapping("/insertResult")
public String insertResult(@ModelAttribute EmployeeForm form) {
// Form -> Entity သို့ ပြောင်းပြီး Save လုပျရပါမညျ
return "insertResult";
}
// --- ပြင်ဆင်ခြင်း (update) ---
@PostMapping("/update")
public String update(Model model) {
model.addAttribute("employeeForm", new EmployeeForm());
return "update";
}
@PostMapping("/updateConfirm")
public String updateConfirm(@ModelAttribute EmployeeForm form, Model model) {
model.addAttribute("employeeForm", form);
return "updateConfirm";
}
@PostMapping("/updateResult")
public String updateResult(@ModelAttribute EmployeeForm form) {
return "updateResult";
}
// --- ဖျက်ဆီးခြင်း (delete) ---
@PostMapping("/delete")
public String delete() {
return "delete";
}
@PostMapping("/deleteResult")
public String deleteResult(Integer id) {
// employeeService.deleteById(id);
return "deleteResult";
}
}

View File

@@ -0,0 +1,29 @@
package com.example.demo.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;
@Data
@Entity
@Table(name="employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="id_employee", nullable=false)
private Integer idEmployee;
@Column(name ="nm_employee", nullable=false)
private String nmEmployee;
@Column(name ="kn_employee", nullable=false)
private String knEmployee;
@Column(name ="mail_address", nullable=false)
private String mailAddress;
@Column(name ="password", nullable=false)
private String password;
@Column(name ="id_department", nullable=false)
private Integer idDepartment;
}

View File

@@ -0,0 +1,14 @@
package com.example.demo.form;
import lombok.Data;
@Data
public class EmployeeForm {
private String nmEmp;
private String knEmp;
}

View File

@@ -0,0 +1,15 @@
package com.example.demo.form;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginForm {
private String mail;
private String pass;
}

View File

@@ -0,0 +1,12 @@
package com.example.demo.repsitory;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.entity.Employee;
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
Optional<Employee> findByMailAddressAndPassword(String mail, String pass);
}

View File

@@ -0,0 +1,36 @@
package com.example.demo.service;
import java.util.List;
import java.util.Optional;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.entity.Employee;
import com.example.demo.repsitory.EmployeeRepository;
@Service
@Transactional
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
// Login စစ်ဆေးခြင်း
public Optional<Employee> login(String mail, String pass) {
return employeeRepository.findByMailAddressAndPassword(mail, pass);
}
// အချက်အလက်အားလုံး ရှာဖွေခြင်း
public List<Employee> getAllEmployee() {
return employeeRepository.findAll();
}
// အချက်အလက် သိမ်းဆည်းခြင်း / ပြင်ဆင်ခြင်း
public Employee save(Employee employee) {
return employeeRepository.save(employee);
}
// အချက်အလက် ဖျက်ဆီးခြင်း
public void deleteById(Integer id) {
employeeRepository.deleteById(id);
}
}

View File

@@ -1,20 +1,25 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>削除ページ</title>
</head>
<body>
<h3> 削除</h3>
<h3>削除</h3>
<form action="logout" method="post">
<input type="submit" value="ログアウト">
</form>
<!-- ID တစ်ခုတည်း ရိုက်ထည့်ရန် Form -->
<form action="deleteResult" method="post">
<div><label>id: <input type="number" name="idEmployee"></label></div>
<br>
<input type="submit" value="削除">
</form>
<br>
<form action="back" method="post">
<input type="submit" value="戻る">
</form>
</body>
</html>

View File

@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>削除確認ページ</title>
</head>
<body>
<h3> 削除完了しました。</h3>
<form action="logout" method="post">
<input type="submit" value="ログアウト">
</form>
<form action="back" method="post">
<input type="submit" value="戻る">
</form>
</body>
</html>

View File

@@ -11,6 +11,17 @@
<form action="logout" method="post">
<input type="submit" value="ログアウト">
</form>
<!-- Input Form များ -->
<form action="insertConfirm" method="post">
<div><label>name: <input type="text" name="nmEmp"></label></div>
<div><label>kana: <input type="text" name="knEmp"></label></div>
<div><label>mail: <input type="email" name="mail"></label></div>
<div><label>pass: <input type="password" name="pass"></label></div>
<div><label>id_dep: <input type="number" name="idDepartment"></label></div>
<br>
<input type="submit" value="登録">
</form>
<br>
<form action="back" method="post">
<input type="submit" value="戻る">
</form>

View File

@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>insert Result</title>
</head>
<body>
<h3>登録完了しました。</h3>
<form action="logout" method="post">
<input type="submit" value="ログアウト">
</form>
<form action="back" method="post">
<input type="submit" value="戻る">
</form>
</body>
</html>

View File

@@ -8,6 +8,8 @@
<body>
<form action="doLogin" method="post">
<input type="email"name="mail"placeholder="メールアドレス">
<input type="password"name="pass"placeholder="パスワード">
<input type="submit" value="ログイン">
</form>

View File

@@ -21,7 +21,13 @@
</form>
<form action="delete" method="post">
<input type="submit" value="削除">
</form>
<!-- ログイン者情報の表示(キー名を login_emp に修正) -->
<div th:if="${session.login_emp != null}">
<span th:text="|ログイン者:${session.login_emp.nmEmployee}|"></span>
</div>
</body>

View File

@@ -6,15 +6,32 @@
<title>検索ページ</title>
</head>
<body>
<h3> 検索</h3>
<brbody>
<h3> 検索</h3>
<form action="logout" method="post">
<input type="submit" value="ログアウト">
</form>
<!-- ဇယားဖြင့် အချက်အလက်များ ပြသခြင်း -->
<table border="1">
<tr>
<th>ID</th>
<th>名前</th>
<th>カナ</th>
<th>メールアドレス</th>
</tr>
<tr th:each="emp : ${employeeList}">
<td th:text="${emp.idEmployee}"></td>
<td th:text="${emp.nmEmployee}"></td>
<td th:text="${emp.knEmployee}"></td>
<td th:text="${emp.mailAddress}"></td>
</tr>
</table>
<br>
<form action="back" method="post">
<input type="submit" value="戻る">
</form>
</body>
</body>
</html>

View File

@@ -11,6 +11,18 @@
<form action="logout" method="post">
<input type="submit" value="ログアウト">
</form>
<!-- Input Form များ -->
<form action="updateConfirm" method="post">
<div><label>id: <input type="number" name="idEmployee"></label></div>
<div><label>name: <input type="text" name="nmEmp"></label></div>
<div><label>kana: <input type="text" name="knEmp"></label></div>
<div><label>mail: <input type="email" name="mail"></label></div>
<div><label>pass: <input type="password" name="pass"></label></div>
<div><label>id_dep: <input type="number" name="idDepartment"></label></div>
<br>
<input type="submit" value="更新">
</form>
<br>
<form action="back" method="post">
<input type="submit" value="戻る">
</form>

View File

@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>更新ページ</title>
</head>
<body>
<h3> 更新完了しました。</h3>
<form action="logout" method="post">
<input type="submit" value="ログアウト">
</form>
<br>
<form action="back" method="post">
<input type="submit" value="戻る">
</form>
<body>
</body>
</html>

View File

@@ -9,10 +9,10 @@
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Spring-lesson</artifactId>
<artifactId>spring-lesson</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Spring-lesson</name>
<name>spring-lesson</name>
<description/>
<url/>
<licenses>
@@ -31,7 +31,10 @@
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
@@ -43,20 +46,38 @@
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>Spring-lesson-Q2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>

View File

@@ -2,7 +2,7 @@ package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import com.example.demo.presentation.Q1View;
@@ -10,9 +10,9 @@ import com.example.demo.presentation.Q1View;
public class SpringLessonApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringLessonApplication.class, args);
ApplicationContext context = SpringApplication.run(SpringLessonApplication.class, args);
// Q1View Bean ကို ရယူပြီး Execute လုပ်ခြင်း
Q1View view = context.getBean(Q1View.class);
view.display();
}

View File

@@ -9,21 +9,15 @@ import com.example.demo.service.Q1Service;
@Controller
public class Q1Controller {
@Autowired
Q1Service service;
Q1Logic logic = new Q1Logic();
public Q1Entity show(String mailAddress, String password) {
String result = logic.check(mailAddress, password);
if (!result.equals("OK")) {
System.out.println(result);
return null;
}
return service.getUserData(mailAddress, password);
private Q1Service q1Service;
@Autowired
private Q1Logic q1Logic;
public String execute (String mailAddress, String password) {
Q1Entity entity =q1Service.processService(mailAddress, password);
return q1Logic.formatUserDetail(entity);
}
}

View File

@@ -1,20 +1,24 @@
package com.example.demo.domain;
public class Q1Entity {
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class Q1Entity {
@Id
private String mailAddress;
private String password;
//↓これなんでいるんやろう
public Q1Entity() {
}
// Constructor
public Q1Entity(String mailAddress, String password) {
this.mailAddress = mailAddress;
this.password = password;
}
//getter・setter
// Getter & Setter
public String getMailAddress() {
return mailAddress;
}
@@ -30,5 +34,4 @@ public class Q1Entity {
public void setPassword(String password) {
this.password = password;
}
}

View File

@@ -1,19 +1,13 @@
package com.example.demo.logic;
import org.springframework.stereotype.Component;
import com.example.demo.domain.Q1Entity;
@Component
public class Q1Logic {
public String check(String mailAddress, String password) {
//メールアドレスが256文字より大きい
if (mailAddress.length() > 256) {
return "メールアドレスもしくはパスワードが不正です";
}
//パスワードが8文字より小さい
if (password.length() < 8) {
return "メールアドレスもしくはパスワードが不正です";
}
return "OK";
public String formatUserDetail(Q1Entity entity) {
return"メールアドレス:"+entity.getMailAddress()+" パスワード:"+entity.getPassword();
}
}

View File

@@ -4,14 +4,12 @@ import org.springframework.stereotype.Repository;
import com.example.demo.domain.Q1Entity;
@Repository //←「このクラスはDAOとして使います」
@Repository
public class Q1Dao {
public Q1Entity findData(String mailAddress, String password) {
private Q1Dao q1Dao;
public Q1Entity createEntity (String mailAddress, String password) {
return new Q1Entity(mailAddress, password);
Q1Entity entity = new Q1Entity();
entity.setMailAddress(mailAddress);
entity.setPassword(password);
return entity;
}
}

View File

@@ -1,4 +1,3 @@
//メールアドレスとパスワードを入力する
package com.example.demo.presentation;
import java.util.Scanner;
@@ -7,31 +6,21 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.example.demo.application.Q1Controller;
import com.example.demo.domain.Q1Entity;
@Component
public class Q1View {
@Autowired
private Q1Controller q1Controller;
@Autowired
Q1Controller controller;
public void display() {
Scanner scanner = new Scanner(System.in);
System.out.println("メールアドレス入力してください。");
String mailAdress = scanner.nextLine();
System.out.println("パスワード入力してください。");
String password = scanner.nextLine();
public void display() {
Scanner scanner = new Scanner(System.in);
System.out.println("メールアドレス:");
String mailAddress = scanner.nextLine();
System.out.println("パスワード:");
String password = scanner.nextLine();
Q1Entity entity = controller.show(mailAddress, password);
if (entity != null) {
System.out.println("メールアドレス:" + entity.getMailAddress());
System.out.println("パスワード:" + entity.getPassword());
}
}
}
String result = q1Controller.execute(mailAdress, password);
System.out.println("======結果=====");
System.out.println(result);
}
}

View File

@@ -8,13 +8,10 @@ import com.example.demo.persistence.Q1Dao;
@Service
public class Q1Service {
//Q1ServiceがQ1Daoを使いたい→Q1Daoを渡しておくね
@Autowired
Q1Dao dao;
public Q1Entity getUserData(String mailAddress, String password) {
return dao.findData(mailAddress, password);
private Q1Dao q1Dao;
public Q1Entity processService(String mailAdress, String password) {
return q1Dao.createEntity(mailAdress,password);
}
}