spring, springboot

springBoot

campanula 2023. 8. 21. 17:16

src/main/java

com.boot.example.dao

 SubjectDAO =>인터페이스 생성

src/test/java

com.boot.example.dao

 SubjectTests => 단위테스트를 위한 클래스 생성

com.boot.example.service

 SubjectService =>인터페이스 생성(다형성 - 결합도가 낮은 프로그램)

 SubjectServiceImpl => 구현 클래스 생성

com.boot.example.controller

 SubjectController => controller 클래스 생성

 공통 매핑: /subject/로 명시

 

학과 리스트 매핑: /subject/subjectList

뷰단: /WEB-INF/views/subject/subjectList.jsp

학과 입력 매핑: /subject/subjectInsert

 

application.properties에 위치 설정

# 뷰리졸버 설정

spring.mvc.view.prefix=/WEB-INF/views/

spring.mvc.view.suffix=.jsp

return 앞에 prefix, 뒤에 suffix

 

src/main/java: 로직을 실행시키기 위한 폴더.

src/test/java: 단위테스트를 위한 폴더로 로직이 나오지 않음.

 

1.controller

인스턴스가 제일 먼저 만들어져야함.

어노테이션을 통해 @Controller (인스턴스 생성)

 

1-1.서비스를 구현해서 구현클래스를 통해 인스턴스 생성

@Service

1-2.

controller에 subject매핑

@RequestMapping("/subject/*")

@Slf4j:controller에 원래 안씀, 연습하기 위해 사용

1-3.매핑 /subjectList

서비스가 subjectList 호출

데이터가 아니라 뷰

package com.boot.example.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.RequestMapping;

import com.boot.example.domain.SubjectVO;
import com.boot.example.service.SubjectService;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/subject/*")
@Slf4j
public class SubjectController {
	
	@Setter(onMethod_=@Autowired)
	private SubjectService subjectService;
	
	@GetMapping("/subjectList")
	public String subjectList(Model model) {
		log.info("subjectList 메서드 호출");
		
		List<SubjectVO> subjectList = subjectService.subjectList();
		model.addAttribute("subjectList", subjectList);	//속성 추가
		
		return "subject/subjectList";
	}

}

 

2.service는 DAO가 필요

serviceImpl에서 만든 subjectList를 controller에서 받음.

package com.boot.example.service;
import java.util.List;

import com.boot.example.domain.SubjectVO;
public interface SubjectService {
	public List<SubjectVO> subjectList();

}
package com.boot.example.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boot.example.dao.SubjectDAO;
import com.boot.example.domain.SubjectVO;

import lombok.Setter;

@Service
public class SubjectServiceImpl implements SubjectService{

	//service 는 DAO없이는 안되고
	//control단은 service 없이는 안됨.
	@Setter(onMethod_=@Autowired)
	private SubjectDAO subjectDAO;
	
	@Override
	public List<SubjectVO> subjectList() {
		List<SubjectVO> list = null;
		list = subjectDAO.subjectList();
		return list;
	}

}

인터페이스를 구현해서 사용하면되니 개발자가 구현 클래스만 수정하면 됨.

결합도가 낮으면 수정하기 쉬워야함.

 

단위테스트를 하는게 훨씬 속도가 빠르다.

입력, 수정, 삭제는 사용 후 다시 불러와야하므로 redirect 사용

:/