2022(112)
-
스프링 DB 접근 기술 - 순수 JDBC
build.gradle 파일에 jdbc, h2 데이터베이스 관련 라이브러리 추가 implementation 'org.springframework.boot:spring-boot-starter-jdbc' - 자바가 DB와 붙기위해서 JDBC 필요 runtimeOnly 'com.h2database:h2' - DB와 붙을 때 DB가 제공하는 클라이언트가 필요 scr>main>resources>application.properties 파일에 DB 접속 정보 추가 spring.datasource.url=jdbc:h2:tcp://localhost/~/test spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa 이렇게 하면 Sp..
2022.07.31 -
스프링 DB 접근 기술 - H2 데이터베이스 설치
H2 DB 설치 Downloads (h2database.com) 로 접속해서 Platform-Independent Zip으로 설치하여 압축을 푼다. h2>bin>h2.bat OR h2w.bat 를 실행시킨다. 아래와 같은 경로로 화면이 띄워진다. 게속 파일로 접근하게 되면 동시에 애플리케이션과 웹 콘솔이랑 같이 접근이 안되고 충돌될 수 있기 때문에 JDBC URL을 jdbc:h2:tcp://localhost/~/test 로 파일이 아닌 소켓을 통해 접근한다. 테이블 생성 insert into member(name) values('spring') 로 spring, spring2인 name 값을 삽입
2022.07.30 -
회원 관리 예제 - 웹 MVC 개발 & 등록 & 조회
아래와 같은 화면이 나오게 된다. * 앞전에 index.html을 만들면서 동일한 URL ( = "/")에서 표출되는 화면을 정의한 적이 있다. 하지만 요청이 오면 스프링 컨테이너의 컨트롤러 안에 관련된 컨트롤러가 있는지 확인한 이후에 templates아래 html 파일이 있는지 찾기 때문에 우선순위는 전자 즉, 위와 같은 화면이 나오게 되는것이다. 등록 MemberController.java의 createFrom메소드를 통해 /members/new와 매핑이 되어 createMemberForm.html로 넘어감 CreateMemberForm.html로 부터 이름(name) 입력 받음 -> action의 url (="members/new")로 host 방식으로 넘어감 2번으로 부터 받은 name 값은 Me..
2022.07.30 -
스프링 빈과 의존관계 - 자바 코드로 직접 스프링 빈 등록하기
MemberService와 MemberRepository를 스프링 컨테이너에 빈으로 올려준다. 컨트롤러는 스프링에서 관리하는 것이기 때문에 컴포넌트 스캔으로 올라간다. 스프링이 뜰때만 주입해주고, 변경 할 방법이 따로 없다. 누군가 MemberController로 호출했을 때, Public으로 열려있어야 한다. 애플리케이션 로딩 시점(조립 = 스프링 컨테이너 올라가고 나머지가 세팅되는 시점)할때 한번 변경이 될 뿐, 한번 세팅이 된 이후에는 memberService를 변경할 일이 없는데, setMemberService가 Pulic하게 노출된다. 따라서 의존관계가 실행 중에 동적으로 변하는 경우는 거의 없기 때문에 생성자 주입을 권장한다. * 실무에서는 주로 정형화된 컨트롤러, 서비스, 리포지토리의 경우 ..
2022.07.30 -
스프링 빈과 의존관계 - 컴포넌트 스캔과 자동 의존관계 설정
화면에 붙이기 위해서는 Controller와 View Templates가 필요하다. 그러기 위해 MemberController가 필요한데, 이는 MemberService를 통해 회원가입하고, 데이터를 조회할 수 있어야한다. 이를 MemberController가 MemberService에 의존한다고 한다. MemberService는 스프링 컨테이너에 딱 한번 등록하고 공용으로 사용하는 것이 좋다. new로 생성하는 경우, 다양한 Controller에서도 호출이 가능해진다. private final MemberService memberService; MemberController를 생성하면 그 아래 생성자를 자동으로 호출하게 되는데, @Autowired가 있으면 컨테이너에 등록된 스프링 빈 memberSer..
2022.07.30 -
회원 관리 예제 - 회원 서비스 테스트
테스트 할 클래스 클릭 후, ctrl+shift+t 하면 아래와 같이 테스트 껍데기를 만들기 위한 생성 창이 뜨게 된다. 회원가입 테스트 **** 테스트는 정상 플로우도 중요하지만 예외 플로우가 훨씬 중요하다. **** 중복회원 예외 테스트 * 전체 서비스 테스트 * 이전 MemoryMemberRepository 에서 clear를 해줬던 것 처럼, 여기서도 두번의 서비스 로직(회원가입 & 중복_회원_예외)을 돌면서 중복 이름이 DB에 쌓여 생기는 에러를 방지하기 위해 clear를 해주어야만한다. static은 인스턴스와 상관없이 클래스에 붙는다. 따라서 MemoryMemberRepository의 store을 각각 new로 새롭게 만들게 되면 서로 다른 repository가 생성된다. 따라서 MemberS..
2022.07.30