diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md deleted file mode 100644 index 6ee48cb..0000000 --- a/.github/pull_request_template.md +++ /dev/null @@ -1,28 +0,0 @@ -## 이번 주에 어떤 작업을 했는지 설명해주세요. - - -
- -## 특히 어떤 부분을 리뷰받고 싶나요? - - -
- -## 이번 주는 어떻게 학습했나요? 아래 질문에 짧게 답변주세요! - -### 이번 주에 학습에 투자한 시간 - - - - -### 학습 하면서 좋았던 점과 아쉬웠던 점 - - - - - - -### 어려움을 겪는 부분 - - - - -### 스터디 개선되었으면 하는 점 - - - diff --git a/.gitignore b/.gitignore deleted file mode 100644 index c2065bc..0000000 --- a/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ diff --git a/src/main/java/com/dku/springstudy/SpringStudyApplication.java b/CarrotMarket/HelloSpringApplication.java similarity index 60% rename from src/main/java/com/dku/springstudy/SpringStudyApplication.java rename to CarrotMarket/HelloSpringApplication.java index ef164c9..f0f0fd4 100644 --- a/src/main/java/com/dku/springstudy/SpringStudyApplication.java +++ b/CarrotMarket/HelloSpringApplication.java @@ -1,13 +1,13 @@ -package com.dku.springstudy; +package hello.hellospring; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class SpringStudyApplication { +public class HelloSpringApplication { public static void main(String[] args) { - SpringApplication.run(SpringStudyApplication.class, args); + SpringApplication.run(HelloSpringApplication.class, args); } } diff --git a/src/main/resources/application.properties b/CarrotMarket/README.md similarity index 100% rename from src/main/resources/application.properties rename to CarrotMarket/README.md diff --git a/CarrotMarket/SpringConfig.java b/CarrotMarket/SpringConfig.java new file mode 100644 index 0000000..2fb5b5f --- /dev/null +++ b/CarrotMarket/SpringConfig.java @@ -0,0 +1,29 @@ +package hello.hellospring; +import hello.hellospring.domain.Customer; +import hello.hellospring.repository.*; +import hello.hellospring.service.*; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + +@Configuration +public class SpringConfig { + + private final ProductRepository productRepository; + private final CustomerRepository customerRepository; + public SpringConfig(ProductRepository productRepository, CustomerRepository customerRepository) { + this.customerRepository = customerRepository; + this.productRepository = productRepository; + } + //인터페이스만 만들면 스프링이 구현체를 만들어 bean에 + + @Bean + public ProductService productService() { + return new ProductService(productRepository); + } + + @Bean + public CustomerService customerService() { + return new CustomerService(customerRepository); + } +} diff --git a/CarrotMarket/controller/CustomerController.java b/CarrotMarket/controller/CustomerController.java new file mode 100644 index 0000000..324bf98 --- /dev/null +++ b/CarrotMarket/controller/CustomerController.java @@ -0,0 +1,33 @@ +package hello.hellospring.controller; + +import hello.hellospring.domain.Customer; +import hello.hellospring.service.CustomerService; +import org.apache.tomcat.util.json.JSONParser; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class CustomerController { + + private final CustomerService customerService; + @Autowired + public CustomerController(CustomerService customerService) { + this.customerService = customerService; + } + + + + @GetMapping("new") + void join(@RequestParam("email") String em, @RequestParam("password") String ps ) { + Customer cus = new Customer(); + cus.setemail(em); + cus.setpassword(ps); + customerService.join(cus); + + } + + + +} \ No newline at end of file diff --git a/CarrotMarket/controller/ProductController.java b/CarrotMarket/controller/ProductController.java new file mode 100644 index 0000000..17e5496 --- /dev/null +++ b/CarrotMarket/controller/ProductController.java @@ -0,0 +1,39 @@ +package hello.hellospring.controller; + +import hello.hellospring.domain.Product; +import hello.hellospring.service.ProductService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +public class ProductController { + + private final ProductService productService; + @Autowired + public ProductController(ProductService productService) { + this.productService = productService; + } + + @GetMapping("products") + public List list() { + List products = productService.findProducts(); + return products; + } + + @GetMapping("heartclick") + void heartclick(@RequestParam("pid") Long pid) { + productService.HeartClick(pid); + } + + @GetMapping("heartunclick") + void heartunclick(@RequestParam("pid") Long pid) { + productService.HeartUnClick(pid); + } + + +} diff --git a/CarrotMarket/controller/ReactController.java b/CarrotMarket/controller/ReactController.java new file mode 100644 index 0000000..c0390c1 --- /dev/null +++ b/CarrotMarket/controller/ReactController.java @@ -0,0 +1,16 @@ +package hello.hellospring.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +@RestController +public class ReactController { + + @GetMapping("hello") + public List hello() { + return Arrays.asList("강동원","여진구","송중기"); + } +} \ No newline at end of file diff --git a/CarrotMarket/domain/Customer.java b/CarrotMarket/domain/Customer.java new file mode 100644 index 0000000..1671bac --- /dev/null +++ b/CarrotMarket/domain/Customer.java @@ -0,0 +1,33 @@ + +package hello.hellospring.domain; +import javax.persistence.*; + +@Entity +public class Customer{ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long cid; + private String cname; + private String email; + private String password; + private String phone; + private String nickname; + + + public Long getcid() { return cid; } + public void setcid(Long cid) { this.cid = cid; } + public String getcname() { + return cname; + } + public void setcname(String cname) { + this.cname = cname; + } + public String getemail() {return email;} + public void setemail(String email) { this.email = email;} + public String getpassword() { return password;} + public void setpassword(String password) { this.password = password;} + public String getphone() { return phone;} + public void setphone(String phone) {this.phone = phone;} + public String getnickname() {return nickname;} + public void setnickname(String nickname) {this.nickname = nickname;} +} diff --git a/CarrotMarket/domain/Product.java b/CarrotMarket/domain/Product.java new file mode 100644 index 0000000..fddc752 --- /dev/null +++ b/CarrotMarket/domain/Product.java @@ -0,0 +1,31 @@ +package hello.hellospring.domain; + + +import javax.persistence.*; + +@Entity +public class Product{ + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long pid; + private String pname; + private Long price; + private Long heart; + private String seller; + + public Long getpid() { + return pid; + } + public void setpid(Long pid) { this.pid = pid; } + public String getpname() { + return pname; + } + public void setpname(String pname) { + this.pname = pname; + } + public String getSeller() {return seller;} + public void setSeller(String seller) { this.seller = seller;} + public Long getPrice() { return price; } + public void setPrice(Long price) { this.price = price; } + public Long getHeart() { return heart; } + public void setHeart(Long heart) { this.heart = heart; } +} \ No newline at end of file diff --git a/CarrotMarket/repository/CustomerRepository.java b/CarrotMarket/repository/CustomerRepository.java new file mode 100644 index 0000000..b2ff841 --- /dev/null +++ b/CarrotMarket/repository/CustomerRepository.java @@ -0,0 +1,14 @@ +package hello.hellospring.repository; + +import hello.hellospring.domain.Customer; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; +import java.util.Optional; + +public interface CustomerRepository extends JpaRepository { + Customer save(Customer product); + Optional findByCname(String name); + Customer findByCid(Long pid); + List findAll(); +} diff --git a/CarrotMarket/repository/ProductRepository.java b/CarrotMarket/repository/ProductRepository.java new file mode 100644 index 0000000..b48b850 --- /dev/null +++ b/CarrotMarket/repository/ProductRepository.java @@ -0,0 +1,14 @@ +package hello.hellospring.repository; + +import hello.hellospring.domain.Product; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; +import java.util.Optional; + +public interface ProductRepository extends JpaRepository { + Product save(Product product); + Optional findByPname(String name); + Product findByPid(Long pid); + List findAll(); +} diff --git a/CarrotMarket/service/CustomerService.java b/CarrotMarket/service/CustomerService.java new file mode 100644 index 0000000..96097e3 --- /dev/null +++ b/CarrotMarket/service/CustomerService.java @@ -0,0 +1,33 @@ +package hello.hellospring.service; + +import hello.hellospring.domain.Customer; +import hello.hellospring.repository.CustomerRepository; + + +import java.util.List; +import java.util.Optional; + +//@service +public class CustomerService { + private final CustomerRepository customerRepository; + //@Autowired + public CustomerService(CustomerRepository customerRepository) { //의존관계 추가 + this.customerRepository = customerRepository; + } + + public List findCustomers() { //전체 회원 조회 + return customerRepository.findAll(); + } + + public Long join(Customer customer) { // 회원가입 + validateDuplicateMember(customer); //중복 회원 체크 + customerRepository.save(customer); + return customer.getcid(); + } + private void validateDuplicateMember(Customer customer) { + customerRepository.findByCname(customer.getcname()) + .ifPresent(m -> { //optinal 값이 null이 아니면 + throw new IllegalStateException("이미 존재하는 회원입니다."); + }); + } +} \ No newline at end of file diff --git a/CarrotMarket/service/ProductService.java b/CarrotMarket/service/ProductService.java new file mode 100644 index 0000000..19eb774 --- /dev/null +++ b/CarrotMarket/service/ProductService.java @@ -0,0 +1,33 @@ +package hello.hellospring.service; + +import hello.hellospring.domain.Product; +import hello.hellospring.repository.ProductRepository; + + +import java.util.List; +import java.util.Optional; + +//@service +public class ProductService { + private final ProductRepository productRepository; + //@Autowired + public ProductService(ProductRepository productRepository) { //의존관계 추가 + this.productRepository = productRepository; + } + + public List findProducts() { //전체 회원 조회 + return productRepository.findAll(); + } + + public void HeartClick(Long pid) { + Product pd = productRepository.findByPid(pid); + pd.setHeart(pd.getHeart()+1); + productRepository.save(pd); + } + + public void HeartUnClick(Long pid) { + Product pd = productRepository.findByPid(pid); + pd.setHeart(pd.getHeart()-1); + productRepository.save(pd); + } +} \ No newline at end of file diff --git a/JPA/Member.java b/JPA/Member.java new file mode 100644 index 0000000..ae3f088 --- /dev/null +++ b/JPA/Member.java @@ -0,0 +1,26 @@ +package hello.hellospring.domain; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Member { + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + //@Column(name = "username") + private String name; + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } +} + diff --git a/JPA/README.md b/JPA/README.md new file mode 100644 index 0000000..8cfa8d4 --- /dev/null +++ b/JPA/README.md @@ -0,0 +1 @@ +https://khs20010327.tistory.com/136 diff --git a/JPA/SpringConfig.java b/JPA/SpringConfig.java new file mode 100644 index 0000000..e437080 --- /dev/null +++ b/JPA/SpringConfig.java @@ -0,0 +1,21 @@ +package hello.hellospring; +import hello.hellospring.repository.*; +import hello.hellospring.service.MemberService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + +@Configuration +public class SpringConfig { + + private final MemberRepository memberRepository; + public SpringConfig(MemberRepository memberRepository) { + this.memberRepository = memberRepository; + } + //인터페이스만 만들면 스프링이 구현체를 만들어 bean에 등록 + + @Bean + public MemberService memberService() { + return new MemberService(memberRepository); + } +} diff --git a/JPA/SpringDataJpaMemberRepository.java b/JPA/SpringDataJpaMemberRepository.java new file mode 100644 index 0000000..1bd9d92 --- /dev/null +++ b/JPA/SpringDataJpaMemberRepository.java @@ -0,0 +1,8 @@ +package hello.hellospring.repository; + +import hello.hellospring.domain.Member; +import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; +public interface SpringDataJpaMemberRepository extends JpaRepository, MemberRepository { + Optional findByName(String name); +} \ No newline at end of file diff --git a/JPA/application.properties b/JPA/application.properties new file mode 100644 index 0000000..8e34fa9 --- /dev/null +++ b/JPA/application.properties @@ -0,0 +1,5 @@ +spring.datasource.url=jdbc:h2:tcp://localhost/~/test +spring.datasource.driver-class-name=org.h2.Driver +spring.datasource.username=sa +spring.jpa.show-sql=true +spring.jpa.hibernate.ddl-auto=none \ No newline at end of file diff --git a/JPA/build.gradle b/JPA/build.gradle new file mode 100644 index 0000000..8049504 --- /dev/null +++ b/JPA/build.gradle @@ -0,0 +1,25 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '2.7.7' + id 'io.spring.dependency-management' version '1.0.15.RELEASE' +} + +group = 'hello' +version = '0.0.1-SNAPSHOT' +sourceCompatibility = '11' + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + runtimeOnly 'com.h2database:h2' + testImplementation 'org.springframework.boot:spring-boot-starter-test' +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/build.gradle b/build.gradle deleted file mode 100644 index 15b77ef..0000000 --- a/build.gradle +++ /dev/null @@ -1,34 +0,0 @@ -plugins { - id 'java' - id 'org.springframework.boot' version '3.0.1' - id 'io.spring.dependency-management' version '1.1.0' -} - -group = 'com.dku' -version = '0.0.1-SNAPSHOT' -sourceCompatibility = '17' - -configurations { - compileOnly { - extendsFrom annotationProcessor - } -} - -repositories { - mavenCentral() -} - -dependencies { - implementation 'org.springframework.boot:spring-boot-starter-data-jpa' - implementation 'org.springframework.boot:spring-boot-starter-security' - implementation 'org.springframework.boot:spring-boot-starter-web' - compileOnly 'org.projectlombok:lombok' - runtimeOnly 'com.mysql:mysql-connector-j' - annotationProcessor 'org.projectlombok:lombok' - testImplementation 'org.springframework.boot:spring-boot-starter-test' - testImplementation 'org.springframework.security:spring-security-test' -} - -tasks.named('test') { - useJUnitPlatform() -} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar deleted file mode 100644 index 249e583..0000000 Binary files a/gradle/wrapper/gradle-wrapper.jar and /dev/null differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties deleted file mode 100644 index 070cb70..0000000 --- a/gradle/wrapper/gradle-wrapper.properties +++ /dev/null @@ -1,5 +0,0 @@ -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew deleted file mode 100755 index a69d9cb..0000000 --- a/gradlew +++ /dev/null @@ -1,240 +0,0 @@ -#!/bin/sh - -# -# Copyright © 2015-2021 the original authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -############################################################################## -# -# Gradle start up script for POSIX generated by Gradle. -# -# Important for running: -# -# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is -# noncompliant, but you have some other compliant shell such as ksh or -# bash, then to run this script, type that shell name before the whole -# command line, like: -# -# ksh Gradle -# -# Busybox and similar reduced shells will NOT work, because this script -# requires all of these POSIX shell features: -# * functions; -# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», -# «${var#prefix}», «${var%suffix}», and «$( cmd )»; -# * compound commands having a testable exit status, especially «case»; -# * various built-in commands including «command», «set», and «ulimit». -# -# Important for patching: -# -# (2) This script targets any POSIX shell, so it avoids extensions provided -# by Bash, Ksh, etc; in particular arrays are avoided. -# -# The "traditional" practice of packing multiple parameters into a -# space-separated string is a well documented source of bugs and security -# problems, so this is (mostly) avoided, by progressively accumulating -# options in "$@", and eventually passing that to Java. -# -# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, -# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; -# see the in-line comments for details. -# -# There are tweaks for specific operating systems such as AIX, CygWin, -# Darwin, MinGW, and NonStop. -# -# (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt -# within the Gradle project. -# -# You can find Gradle at https://github.com/gradle/gradle/. -# -############################################################################## - -# Attempt to set APP_HOME - -# Resolve links: $0 may be a link -app_path=$0 - -# Need this for daisy-chained symlinks. -while - APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path - [ -h "$app_path" ] -do - ls=$( ls -ld "$app_path" ) - link=${ls#*' -> '} - case $link in #( - /*) app_path=$link ;; #( - *) app_path=$APP_HOME$link ;; - esac -done - -APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit - -APP_NAME="Gradle" -APP_BASE_NAME=${0##*/} - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' - -# Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD=maximum - -warn () { - echo "$*" -} >&2 - -die () { - echo - echo "$*" - echo - exit 1 -} >&2 - -# OS specific support (must be 'true' or 'false'). -cygwin=false -msys=false -darwin=false -nonstop=false -case "$( uname )" in #( - CYGWIN* ) cygwin=true ;; #( - Darwin* ) darwin=true ;; #( - MSYS* | MINGW* ) msys=true ;; #( - NONSTOP* ) nonstop=true ;; -esac - -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar - - -# Determine the Java command to use to start the JVM. -if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD=$JAVA_HOME/jre/sh/java - else - JAVACMD=$JAVA_HOME/bin/java - fi - if [ ! -x "$JAVACMD" ] ; then - die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." - fi -else - JAVACMD=java - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." -fi - -# Increase the maximum file descriptors if we can. -if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then - case $MAX_FD in #( - max*) - MAX_FD=$( ulimit -H -n ) || - warn "Could not query maximum file descriptor limit" - esac - case $MAX_FD in #( - '' | soft) :;; #( - *) - ulimit -n "$MAX_FD" || - warn "Could not set maximum file descriptor limit to $MAX_FD" - esac -fi - -# Collect all arguments for the java command, stacking in reverse order: -# * args from the command line -# * the main class name -# * -classpath -# * -D...appname settings -# * --module-path (only if needed) -# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. - -# For Cygwin or MSYS, switch paths to Windows format before running java -if "$cygwin" || "$msys" ; then - APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) - CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) - - JAVACMD=$( cygpath --unix "$JAVACMD" ) - - # Now convert the arguments - kludge to limit ourselves to /bin/sh - for arg do - if - case $arg in #( - -*) false ;; # don't mess with options #( - /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath - [ -e "$t" ] ;; #( - *) false ;; - esac - then - arg=$( cygpath --path --ignore --mixed "$arg" ) - fi - # Roll the args list around exactly as many times as the number of - # args, so each arg winds up back in the position where it started, but - # possibly modified. - # - # NB: a `for` loop captures its iteration list before it begins, so - # changing the positional parameters here affects neither the number of - # iterations, nor the values presented in `arg`. - shift # remove old arg - set -- "$@" "$arg" # push replacement arg - done -fi - -# Collect all arguments for the java command; -# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of -# shell script including quotes and variable substitutions, so put them in -# double quotes to make sure that they get re-expanded; and -# * put everything else in single quotes, so that it's not re-expanded. - -set -- \ - "-Dorg.gradle.appname=$APP_BASE_NAME" \ - -classpath "$CLASSPATH" \ - org.gradle.wrapper.GradleWrapperMain \ - "$@" - -# Stop when "xargs" is not available. -if ! command -v xargs >/dev/null 2>&1 -then - die "xargs is not available" -fi - -# Use "xargs" to parse quoted args. -# -# With -n1 it outputs one arg per line, with the quotes and backslashes removed. -# -# In Bash we could simply go: -# -# readarray ARGS < <( xargs -n1 <<<"$var" ) && -# set -- "${ARGS[@]}" "$@" -# -# but POSIX shell has neither arrays nor command substitution, so instead we -# post-process each arg (as a line of input to sed) to backslash-escape any -# character that might be a shell metacharacter, then use eval to reverse -# that process (while maintaining the separation between arguments), and wrap -# the whole thing up as a single "set" statement. -# -# This will of course break if any of these variables contains a newline or -# an unmatched quote. -# - -eval "set -- $( - printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | - xargs -n1 | - sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | - tr '\n' ' ' - )" '"$@"' - -exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat deleted file mode 100644 index 53a6b23..0000000 --- a/gradlew.bat +++ /dev/null @@ -1,91 +0,0 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%"=="" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%"=="" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if %ERRORLEVEL% equ 0 goto execute - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* - -:end -@rem End local scope for the variables with windows NT shell -if %ERRORLEVEL% equ 0 goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -set EXIT_CODE=%ERRORLEVEL% -if %EXIT_CODE% equ 0 set EXIT_CODE=1 -if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% -exit /b %EXIT_CODE% - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega diff --git a/memory/README.md b/memory/README.md new file mode 100644 index 0000000..938a793 --- /dev/null +++ b/memory/README.md @@ -0,0 +1,3 @@ +![image](https://user-images.githubusercontent.com/101636590/215353139-3a29a907-0178-4abe-bce2-86fb3af8c065.png) + +정리 https://khs20010327.tistory.com/131 diff --git a/memory/main/Member.java b/memory/main/Member.java new file mode 100644 index 0000000..e4eec13 --- /dev/null +++ b/memory/main/Member.java @@ -0,0 +1,19 @@ +package hello.hellospring.domain; + +public class Member { + private Long id; + private String name; + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } +} + diff --git a/memory/main/MemberController.java b/memory/main/MemberController.java new file mode 100644 index 0000000..60709f6 --- /dev/null +++ b/memory/main/MemberController.java @@ -0,0 +1,53 @@ +package hello.hellospring.controller; +import hello.hellospring.domain.Member; +import hello.hellospring.service.MemberService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.List; + +@Controller +public class MemberController { + + @GetMapping("/") + public String home() { + return "home"; + } + + + private final MemberService memberService; + @Autowired + public MemberController(MemberService memberService) { + this.memberService = memberService; + } + + @GetMapping(value = "/members/new") + public String createForm() { + return "newMember"; + } + public class MemberForm { + private String name; + public String getName() { return name; } + public void setName(String name) { this.name = name; } + } + + @PostMapping(value = "/members/new") + public String create(MemberForm form) { + Member member = new Member(); + member.setName(form.getName()); + memberService.join(member); + return "redirect:/"; + } + + @GetMapping(value = "/members") + public String list(Model model) { + List members = memberService.findMembers(); + model.addAttribute("members", members); + return "memberList"; + } +} + + diff --git a/memory/main/MemberRepository.java b/memory/main/MemberRepository.java new file mode 100644 index 0000000..5a6a6f3 --- /dev/null +++ b/memory/main/MemberRepository.java @@ -0,0 +1,13 @@ +package hello.hellospring.repository; + +import hello.hellospring.domain.Member; +import java.util.List; +import java.util.Optional; + +public interface MemberRepository { + Member save(Member member); + Optional findById(Long id); + Optional findByName(String name); + //optional = Null일 수 있는 변수를 감싸는 Wrapper 클래스 + List findAll(); +} \ No newline at end of file diff --git a/memory/main/MemberService.java b/memory/main/MemberService.java new file mode 100644 index 0000000..bb80888 --- /dev/null +++ b/memory/main/MemberService.java @@ -0,0 +1,36 @@ +package hello.hellospring.service; + +import hello.hellospring.domain.Member; +import hello.hellospring.repository.MemberRepository; +import hello.hellospring.repository.MemoryRepository; + +import java.util.List; +import java.util.Optional; + +//@service +public class MemberService { + //private final MemberRepository memberRepository = new MemoryRepository(); + private final MemberRepository memberRepository; + //@Autowired + public MemberService(MemberRepository memberRepository) { //의존관계 추가 + this.memberRepository = memberRepository; + } + public Long join(Member member) { // 회원가입 + validateDuplicateMember(member); //중복 회원 체크 + memberRepository.save(member); + return member.getId(); + } + private void validateDuplicateMember(Member member) { + memberRepository.findByName(member.getName()) + .ifPresent(m -> { //optinal 값이 null이 아니면 + throw new IllegalStateException("이미 존재하는 회원입니다."); + }); + } + + public List findMembers() { //전체 회원 조회 + return memberRepository.findAll(); + } + public Optional findOne(Long memberId) { + return memberRepository.findById(memberId); + } +} \ No newline at end of file diff --git a/memory/main/MemoryRepository.java b/memory/main/MemoryRepository.java new file mode 100644 index 0000000..e27594f --- /dev/null +++ b/memory/main/MemoryRepository.java @@ -0,0 +1,39 @@ +package hello.hellospring.repository; +import hello.hellospring.domain.Member; +import java.util.*; + +//@Repository +public class MemoryRepository implements MemberRepository { + private static Map store = new HashMap<>(); + //동시성 문제가 고려되어 있지 않음, 실무에서는 ConcurrentHashMap, AtomicLong 사용 고려 + private static long sequence = 0L; + + @Override + public Member save(Member member) { + member.setId(++sequence); //auto increment + store.put(member.getId(), member); + return member; + } + @Override + public Optional findById(Long id) { + return Optional.ofNullable(store.get(id)); + //optional = Null일 수 있는 변수를 감싸는 Wrapper 클래스 + //Null이면 Optinal.empty()가 리턴됨 + } + @Override + public List findAll() { + return new ArrayList<>(store.values()); + //map을 list로 반환 + } + @Override + public Optional findByName(String name) { + return store.values().stream() //store의 value로 반복문을 돌림 + .filter(member -> member.getName().equals(name)) + //map에 저장된 member의 이름중 인자로 받은 name과 동일한 값이 있는지 체크 + .findAny(); + //하나라도 찾으면 종료 + } + public void clearStore() { + store.clear(); + } +} \ No newline at end of file diff --git a/memory/main/SpringConfig.java b/memory/main/SpringConfig.java new file mode 100644 index 0000000..85a1f76 --- /dev/null +++ b/memory/main/SpringConfig.java @@ -0,0 +1,18 @@ +package hello.hellospring; +import hello.hellospring.repository.MemberRepository; +import hello.hellospring.repository.MemoryRepository; +import hello.hellospring.service.MemberService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +@Configuration +public class SpringConfig { + + @Bean + public MemberService memberService() { + return new MemberService(memberRepository()); + } + @Bean + public MemberRepository memberRepository() { + return new MemoryRepository(); + } +} \ No newline at end of file diff --git a/memory/templates/home.html b/memory/templates/home.html new file mode 100644 index 0000000..f425b7a --- /dev/null +++ b/memory/templates/home.html @@ -0,0 +1,15 @@ + + + +
+
+

Hello Spring

+

회원 기능

+

+ 회원 가입 + 회원 목록 +

+
+
+ + \ No newline at end of file diff --git a/memory/templates/memberList.html b/memory/templates/memberList.html new file mode 100644 index 0000000..ca39975 --- /dev/null +++ b/memory/templates/memberList.html @@ -0,0 +1,23 @@ + + + +
+
+ + + + + + + + + + + + + +
#이름
+
+
+ + \ No newline at end of file diff --git a/memory/templates/newMember.html b/memory/templates/newMember.html new file mode 100644 index 0000000..5d91074 --- /dev/null +++ b/memory/templates/newMember.html @@ -0,0 +1,15 @@ + + + +
+
+
+ + +
+ +
+
+ + \ No newline at end of file diff --git a/memory/test/MemberServiceTest.java b/memory/test/MemberServiceTest.java new file mode 100644 index 0000000..6de7593 --- /dev/null +++ b/memory/test/MemberServiceTest.java @@ -0,0 +1,51 @@ +package hello.hellospring.service; +import hello.hellospring.domain.Member; +import hello.hellospring.repository.MemoryRepository; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.*; + +class MemberServiceTest { // ctrl + shift + T + MemberService memberService; + MemoryRepository memberRepository; + + @BeforeEach + public void beforeEach() { + memberRepository = new MemoryRepository(); + memberService = new MemberService(memberRepository); + } + @AfterEach + public void afterEach() { + memberRepository.clearStore(); + } + @Test + public void 회원가입() throws Exception { +//Given + Member member = new Member(); + member.setName("hello"); + //멤버 생성 +//When + Long saveId = memberService.join(member); + //멤버 삽입 +//Then + Member findMember = memberRepository.findById(saveId).get(); + assertEquals(member.getName(), findMember.getName()); + //생성한 멤버 name과 데이터베이스에 삽입 후 findById로 찾은 name이 같은지 체크 + } + @Test + public void 중복_회원_예외() throws Exception { +//Given + Member member1 = new Member(); + member1.setName("spring"); + Member member2 = new Member(); + member2.setName("spring"); + //When + memberService.join(member1); + IllegalStateException e = assertThrows(IllegalStateException.class, + () -> memberService.join(member2));//예외가 발생해야 한다. + assertThat(e.getMessage()).isEqualTo("이미 존재하는 회원입니다."); + //try catch 간략화 + } +} \ No newline at end of file diff --git a/memory/test/MemoryRepositoryTest.java b/memory/test/MemoryRepositoryTest.java new file mode 100644 index 0000000..28b915f --- /dev/null +++ b/memory/test/MemoryRepositoryTest.java @@ -0,0 +1,58 @@ +package hello.hellospring.repository; +import hello.hellospring.domain.Member; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import java.util.List; +import java.util.Optional; +import static org.assertj.core.api.Assertions.*; +//Assertions를 전역변수로 + +class MemoryRepositoryTest { + MemoryRepository repository = new MemoryRepository(); + @AfterEach + //각 테스트가 끝날떄마다 수행됨 + public void afterEach() { + repository.clearStore(); + } + + @Test + public void save() { +//given + Member member = new Member(); + member.setName("spring"); +//when + repository.save(member); +//then + Member result = repository.findById(member.getId()).get(); + assertThat(result).isEqualTo(member); + //Assertions.assertThat() + } + @Test + public void findByName() { +//given + Member member1 = new Member(); + member1.setName("spring1"); + repository.save(member1); + Member member2 = new Member(); + member2.setName("spring2"); + repository.save(member2); +//when + Member result = repository.findByName("spring1").get(); +//then + assertThat(result).isEqualTo(member1); + } + @Test + public void findAll() { +//given + Member member1 = new Member(); + member1.setName("spring1"); + repository.save(member1); + Member member2 = new Member(); + member2.setName("spring2"); + repository.save(member2); +//when + List result = repository.findAll(); +//then + assertThat(result.size()).isEqualTo(2); + } +} \ No newline at end of file diff --git a/mvc & api/HelloController.java b/mvc & api/HelloController.java new file mode 100644 index 0000000..debd0bc --- /dev/null +++ b/mvc & api/HelloController.java @@ -0,0 +1,49 @@ +package hello.hellospring.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class HelloController { + + @GetMapping("hello") + public String hello(Model model) { + model.addAttribute("data", "hello!!"); + return "hello"; + } + + @GetMapping("hello-mvc") + public String helloMvc(@RequestParam("name") String str, Model model) { + //@RequestParam(value = "name", required = false + model.addAttribute("data", str); + return "hello"; + } + + @GetMapping("hello-api-str") + @ResponseBody + public String helloString(@RequestParam("name") String str) { + return str; + } + + @GetMapping("hello-api-json") + @ResponseBody + public Hello helloApi(@RequestParam("name") String str) { + Hello hello = new Hello(); + hello.setName(str); + return hello; + } + static class Hello { + private String name; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + //단축키 alt + insert + } +} diff --git a/mvc & api/README.md b/mvc & api/README.md new file mode 100644 index 0000000..cc36cbf --- /dev/null +++ b/mvc & api/README.md @@ -0,0 +1,3 @@ +![image](https://user-images.githubusercontent.com/101636590/215345497-4e454d14-43cd-481b-b555-ee9b40f777d7.png) + +정리글 https://khs20010327.tistory.com/129 diff --git a/mvc & api/hello.html b/mvc & api/hello.html new file mode 100644 index 0000000..257f7ad --- /dev/null +++ b/mvc & api/hello.html @@ -0,0 +1,10 @@ + + + + Hello + + + +

안녕하세요. 손님

+ + diff --git a/settings.gradle b/settings.gradle deleted file mode 100644 index 73e37fd..0000000 --- a/settings.gradle +++ /dev/null @@ -1 +0,0 @@ -rootProject.name = 'springstudy' diff --git a/springboot/README.md b/springboot/README.md new file mode 100644 index 0000000..627b887 --- /dev/null +++ b/springboot/README.md @@ -0,0 +1,10 @@ +![image](https://user-images.githubusercontent.com/101636590/215336786-ab19fe26-630f-4609-88ef-305582c7dd50.png) + + +링크 https://start.spring.io/ + + +![image](https://user-images.githubusercontent.com/101636590/215337523-9a5dd004-c383-4bc1-ab5c-56aad2e45abc.png) + +빠른 실습을 위해 gradle이 아닌 intellij로 실행하도록 설정 +file -> setting -> gradle(검색) -> diff --git a/springboot/hello-spring.zip b/springboot/hello-spring.zip new file mode 100644 index 0000000..60a18ee Binary files /dev/null and b/springboot/hello-spring.zip differ diff --git a/src/test/java/com/dku/springstudy/SpringStudyApplicationTests.java b/src/test/java/com/dku/springstudy/SpringStudyApplicationTests.java deleted file mode 100644 index 79d9975..0000000 --- a/src/test/java/com/dku/springstudy/SpringStudyApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.dku.springstudy; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class SpringStudyApplicationTests { - - @Test - void contextLoads() { - } - -}