Spring_Q1

This commit is contained in:
m-shimizu
2026-10-08 16:39:57 +09:00
parent e0cdad9825
commit 34d68e34b2
8 changed files with 79 additions and 75 deletions

View File

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

View File

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

View File

@@ -9,15 +9,21 @@ import com.example.demo.service.Q1Service;
@Controller @Controller
public class Q1Controller { public class Q1Controller {
@Autowired
private Q1Service q1Service;
@Autowired
private Q1Logic q1Logic;
public String execute (String mailAddress, String password) { @Autowired
Q1Entity entity =q1Service.processService(mailAddress, password); Q1Service service;
return q1Logic.formatUserDetail(entity); 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);
} }
} }

View File

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

View File

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

View File

@@ -4,12 +4,14 @@ import org.springframework.stereotype.Repository;
import com.example.demo.domain.Q1Entity; import com.example.demo.domain.Q1Entity;
@Repository @Repository //←「このクラスはDAOとして使います」
public class Q1Dao { public class Q1Dao {
private Q1Dao q1Dao; public Q1Entity findData(String mailAddress, String password) {
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,3 +1,4 @@
//メールアドレスとパスワードを入力する
package com.example.demo.presentation; package com.example.demo.presentation;
import java.util.Scanner; import java.util.Scanner;
@@ -6,21 +7,31 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.example.demo.application.Q1Controller; import com.example.demo.application.Q1Controller;
import com.example.demo.domain.Q1Entity;
@Component @Component
public class Q1View { public class Q1View {
@Autowired
private Q1Controller q1Controller;
public void display() { @Autowired
Scanner scanner = new Scanner(System.in); Q1Controller controller;
System.out.println("メールアドレス入力してください。");
String mailAdress = scanner.nextLine(); public void display() {
System.out.println("パスワード入力してください。");
String password = scanner.nextLine(); 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,10 +8,13 @@ import com.example.demo.persistence.Q1Dao;
@Service @Service
public class Q1Service { public class Q1Service {
@Autowired
private Q1Dao q1Dao;
public Q1Entity processService(String mailAdress, String password) {
return q1Dao.createEntity(mailAdress,password);
}
//Q1ServiceがQ1Daoを使いたい→Q1Daoを渡しておくね
@Autowired
Q1Dao dao;
public Q1Entity getUserData(String mailAddress, String password) {
return dao.findData(mailAddress, password);
}
} }