<title>Insert title here</title>
</head>
<body>
<a href="/members/memberForm">회원가입</a><p>
<a href="/members/memberList">회원목록</a><p>
</body>
</html>
객체생성
package com.oracle.oBootHello.domain;
public class Member1 {
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;
}
}
package com.oracle.oBootHello.reponsitory;
import com.oracle.oBootHello.domain.Member1;
public interface MemberRepository {
Member1 save(Member1 member1); //Member1에 member1 객체 저장
}
private static Long sequence = 0L;
❓시퀸스 공용으로 쓰기 위해서 ->여러사람들이 아이디 등록할수 있기 때문
원래 0L이고 아이디등록하면 1L ->2L ->3L 이렇게 알아서 됨
member1.setId(++sequence);
❓하나씩 증가하면서 값이 들어감
아이디 등록하면 1번 두번째 아이디 등록하면 2번 이렇게 됨
package com.oracle.oBootHello.reponsitory;
import java.util.HashMap;
import java.util.Map;
import com.oracle.oBootHello.domain.Member1;
public class MemoryMemberRepository implements MemberRepository {
private static Map<Long, Member1> store = new HashMap<Long, Member1>();
private static Long sequence = 0L; //롱형으로 케스팅
//시퀸스 공용으로 쓰기 위해서 아이디등록하면 1L ->2L
@Override
public Member1 save(Member1 member1) { //멤버에 저장하기
//아이디 등록하면 1번 두번째 아이디 등록하면 2번 이렇게 됨
member1.setId(++sequence); //하나씩 증가하면서 값이 들어감
store.put(member1.getId(), member1); //Map에 저장할때 put 사용
System.out.println("MemoryMemberRepository +member1.getName()->"+member1.getName());
return member1;
}
}
🔽
@Repositord이것만 설정해주기
@Repository //빈으로 설정
public class MemoryMemberRepository implements MemberRepository {
package com.oracle.oBootHello.service;
import com.oracle.oBootHello.domain.Member1;
import com.oracle.oBootHello.reponsitory.MemberRepository;
import com.oracle.oBootHello.reponsitory.MemoryMemberRepository;
public class MemberService {
MemberRepository memberRepository = new MemoryMemberRepository();
//회원가입
public Long memberSave(Member1 member1) {
memberRepository.save(member1);
return member1.getId();
}
}
🔽
@Service //자동적으로 MemberService 빈으로 등록되어짐
public class MemberService {
//전통적
//MemberRepository memberRepository = new MemoryMemberRepository();
//현재 DI 방식
private final MemberRepository memberRepository;
@Autowired
public MemberService (MemberRepository memberRepository ) {
this.memberRepository = memberRepository;
}
▶🟦3.MemoryMemberRepository에서 @Repository //빈으로 설정
package com.oracle.oBootHello.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.oracle.oBootHello.domain.Member1;
import com.oracle.oBootHello.service.MemberService;
@Controller
public class MemberController {
//전통적인방법
MemberService memberService = new MemberService();
@GetMapping(value = "members/memberForm")
public String memberForm() {
System.out.println("MemberController/members/membersForm Start.. ");
return "members/memberForm";
}
@PostMapping(value = "members/save")
public String save(Member1 member1) {
System.out.println("MemberController/members/save start... ");
Long id = memberService.memberSave(member1);
System.out.println("MemberController/members/save id>"+id);
return "redirect:/";
}
}
🔽
@Controller
public class MemberController {
//전통적인 방법
//MemberService memberService = new MemberService();
//현재 DI 방식
//MemberService에서 @service로 연결된것만 사용 가능
private final MemberService memberService; //final -->생성자를 통해서만 값을 가져옴
@Autowired
public MemberController(MemberService memberService) {
this.memberService=memberService; //memberService (이름)를 가져와서 나의 memberService와 연결시킨다
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/members/save" method="post">
<label for="name">이름</label>
<input type="text" id="name" name="name" placeholder="이름을 입력하세요">
<button type="submit">등록</button>
</form>
</body>
</html>
이름입력하면 실행되어짐 🔽
@GetMapping(value = "/members/memberList")
public String memberList(Model model) {
logger.info("memberList start. . . ");
List<Member1> memberLists = memberService.allMembers();
model.addAttribute("memberLists" ,memberLists);
logger.info("memberLists.size()-> {}",memberLists.size());
return "members/memberList";
}
//회원목록
public List<Member1> allMembers() {
System.out.println("MemberService allMembers Start. . . ");
List<Member1> memList = null;
memList =memberRepository.findAll();
System.out.println("memList.size()->"+memList.size());
return memList;
}
▶findAll 컨트롤 눌려서 자동 생성
List<Member1> findAll();
@Override
public List<Member1> findAll() {
System.out.println("MemoryMemberRepository findAll Start . . . ");
//store의 value(Member1)
List<Member1> listMember = new ArrayList<>(store.values()); //ArrayList에 value값만 가지고오면 list로 변환
System.out.println("MemoryMemberRepository findAll listMember.size() -> "+ listMember.size());
return listMember;
}
}
<!DOCTYPE html>
<html xml:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>No</th>
<th>이름</th>
</tr>
</thead>
<tbody>
<tr th:each="member : ${memberLists}" >
<td th:text="${member.id}"></td>
<td th:text="${member.name}"></td>
</tr>
</tbody>
</table>
</body>
</html>
🔽 회원가입하고 회원목록 선택
🔽Console 실행결과
- Build
1) 콘솔로 이동
2) gradlew build
3) cd build/libs
🔽여기서 내가 작업했던 파일 선택해주고 경로 복사해 오기
- Run(배포)
1) java -jar hello-spring-0.0.1-SNAPSHOT.jar
2) 실행확인
🔽스프링 닫고 포트번호 보고 localhost:8381 크롬에 입력하면 내가 작업했던 화면 실행되어짐
▶배표 완료
->만약 jdbc 빠져먹고 안넣엇다면(다른것도가능)
🔽여기 jdbc 코드 넣어주고
🔽refresh 하면 됨
서버 포트는 유일해야함 프로젝트마다 다른 번호 써주기 .
server.port=8382
#Oracle Connect
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/xe
spring.datasource.username=scott
spring.datasource.password=tiger
package com.oracle.oBootDBConnect.domain;
public class Member1 {
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;
}
}
@Controller
public class HelloController {
//로거 사용 가능
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
//Prefix --> templates
//suffix -->.html
@RequestMapping("hello")
public String hello(Model model) {
logger.info("start...");
model.addAttribute("parameter", "boot start...");
return "hello";
}
<body>
<h1>hello.html</h1>
<p th:text="안녕+${parameter}">
</body>
Static
<h1>나 index야 </h1>
<a href="/hello">hello</a><p>
<a href="/members/memberForm">회원가입</a><p>
<a href="/members/memberList">회원목록</a><p>
</body>
<body>
<h1>Home </h1>
<a href="/hello">hello</a><p>
<a href="/members/memberForm">회원가입</a><p>
<a href="/members/memberList">회원목록</a><p>
</body>
컨트롤러가 정적 파일보다 우선순위가 높음
그래서 Index.html말고 home.html이 실행되어짐
package com.oracle.oBootDBConnect.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
//컨트롤러가 정적 파일보다 우선순위가 높음
@GetMapping("/")
public String home() {
System.out.println("HomeController home start. . ");
return "home";
}
}
🔽 한바퀴 돌아가는지 확인 .. 완료
package com.oracle.oBootDBConnect.repository;
import com.oracle.oBootDBConnect.domain.Member1;
public interface MemberRepository {
Member1 save(Member1 member1); //저장 인터페이스
List<Member1> findAll();
}
package com.oracle.oBootDBConnect.repository;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.oracle.oBootDBConnect.domain.Member1;
@Repository //저장소
public class MemoryMemberRepository implements MemberRepository {
private static Map<Long, Member1> store = new HashMap<>();
private static long sequence = 0L;
@Override
public Member1 save(Member1 member1) {
System.out.println("MemoryMemberRepository save start . . . ");
member1.setId(++sequence);
store.put(member1.getId(), member1);
return member1;
}
@Override
public List<Member1> findAll() {
System.out.println("MemoryMemberRepository findAll Start . . . ");
//store의 value(Member1)
List<Member1> listMember = new ArrayList<>(store.values()); //ArrayList에 value값만 가지고오면 list로 변환
System.out.println("MemoryMemberRepository findAll listMember.size() -> "+ listMember.size());
return listMember;
}
}
package com.oracle.oBootDBConnect.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.oracle.oBootDBConnect.domain.Member1;
import com.oracle.oBootDBConnect.repository.MemberRepository;
@Service
public class MemberService {
private final MemberRepository memberRepository;
@Autowired
public MemberService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
//회원가입
public Long memberSave(Member1 member) {
memberRepository.save(member);
return member.getId();
}
//전체회원 조회
public List<Member1> findMembers() {
System.out.println("MemberService findMembers Start. . . ");
return memberRepository.findAll();
}
}
package com.oracle.oBootDBConnect.controller;
import java.util.List;
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 com.oracle.oBootDBConnect.domain.Member1;
import com.oracle.oBootDBConnect.service.MemberService;
@Controller
public class MemberController {
private final MemberService memberService;
// 생성자에 @Autowired 가 있으면 스프링이 연관된 객체를 스프링 컨테이너에서 찾아서 넣어줌
// 객체 의존관계를 외부에서 넣어주는 것을 DI (Dependency Injection), 의존성 주입이라 함
// 이전 에서는 개발자가 직접 주입했고, 여기서는 @Autowired에 의해 스프링이 주입
// 스프링 빈을 등록하는 2가지 방법
// 컴포넌트 스캔과 자동 의존관계 설정
// 자바 코드로 직접 스프링 빈 등록하기
// @Component 를 포함하는 다음 애노테이션도 스프링 빈으로 자동 등록된다.
// @Controller
// @Service
// @Repository
@Autowired
public MemberController(MemberService memberService ) {
this.memberService = memberService;
}
//회원가입
@GetMapping(value= "/members/memberForm")
public String createMemberForm() {
System.out.println("MemberController/members/memberForm start. . . ");
return "members/createMemberForm";
}
//회원가입 저장
@PostMapping(value = "/members/newSave")
public String memberSave(Member1 member) {
System.out.println("MemberController memberSave start . . . ");
memberService.memberSave(member);
return "redirect:/";
}
//회원목록
@GetMapping(value = "/members/memberList")
public String memberLists(Model model) {
List<Member1> memberList = memberService.findMembers();
model.addAttribute("members",memberList);
return "members/memberList";
}
}
<!DOCTYPE html>
<html xml:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>회원등록</h1>
<div class="container">
<form action="/members/new" method="post">
<div class="form-group">
<label for="name">이름</label>
<input type="text" id="name" name="name" placeholder="이름을 입력하세요">
</div>
<button type="submit">등록</button>
</form>
</div> <!-- container -->
</body>
</html>
<!DOCTYPE html>
<html xml:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>회원기능</h1>
<div class="container">
<div>
<table border="1">
<thead>
<tr>
<th>No</th>
<th>이름</th>
</tr>
</thead>
<tbody>
<tr th:each= "member : ${members}">
<td th:text= "${member.id}"></td>
<td th:text= "${member.name}"></td>
</tr>
</tbody>
</table>
</div>
</div> <!-- container -->
</body>
</html>