728x90
반응형
웹 애플리케이션 계층 구조
@Controller : 웹 MVC의 컨트롤러 역할
@Controller
public class HomeController {
private final MainBoardService mainBoardService;
@Autowired
public HomeController(MainBoardService mainBoardService) {
this.mainBoardService = mainBoardService;
}
@GetMapping("/mainProductBoard")
public String list(Model model){
List<MainBoard> mainBoardList = mainBoardService.mainBoard();
model.addAttribute("mainproducts",mainBoardList);
return "boards/board";
}
/mainProductBoard get요청이 되면 return이
resources / templates / boards / board 로 이동한다
@Service : 핵심 비즈니스 로직 구현
스프링의 Service 인터페이스와 구현 클래스 패키지
CRUD등 비즈니스 단 기능들이 수행됨
Repository를 거쳐 DB에 접근
@Service
@Transactional
public class MainBoardService {
private final MainBoardRepository mainBoardRepository;
public MainBoardService(MainBoardRepository mainBoardRepository) {
this.mainBoardRepository = mainBoardRepository;
}
/**
* 게시글 등록
*/
public int Insert(MainBoard mainBoard){
mainBoardRepository.Insert(mainBoard);
return mainBoard.getMainProductId();
}
public List<MainBoard> mainBoard(){
return mainBoardRepository.findAll();
}
public Optional<MainBoard> findOne(int mainProductId){
return mainBoardRepository.findBoard(mainProductId);
}
public int removeOne(MainBoard mainBoard){
mainBoardRepository.delBoard(mainBoard);
return mainBoard.getMainProductId();
}
@Repository : 데이터베이스에 접근, 도메인 객체를 DB에 저장하고 관리
public interface MainBoardRepository {
List<MainBoard> findAll();
MainBoard Insert(MainBoard mainBoard);
Optional<MainBoard> findBoard(int mainProductId);
MainBoard delBoard(MainBoard mainBoard);
}
repository는 interface로 만들고
public class JpaMainBoardRepository implements MainBoardRepository{
private final EntityManager em;
public JpaMainBoardRepository(EntityManager em) {
this.em = em;
}
@Override
public List<MainBoard> findAll() {
return em.createQuery("select m from MainBoard m order by m.mainProductId desc", MainBoard.class) //MainBoard Entity에 query 하는것, m = as m
.getResultList();
}
@Override
public MainBoard Insert(MainBoard mainBoard) {
em.persist(mainBoard);
return mainBoard;
}
//Optional 반환형 사용 (Null값 처리에 용이)
@Override
public Optional<MainBoard> findBoard(int mainProductId) {
MainBoard mainBoard = em.find(MainBoard.class,mainProductId);
return Optional.ofNullable(mainBoard);
}
@Override
public MainBoard delBoard(MainBoard mainBoard) {
em.remove(mainBoard);
return mainBoard;
}
}
상속받아 JPA repository를 사용한다
EntityManager를 사용한다
@Domain : 비즈니스 도메인 객체, ex) 회원,주문,쿠폰 등등 주로 데이터베이스에 저장하고 관리됨
VO, DTO 클래스들의 패키지
@Entity
@Table(name="mainproduct")
public class MainBoard {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // PK
@Column(name="mainProductId")
private int mainProductId;
@Column(name = "mainProductTitle")
private String mainProductTitle;
@Column(name = "mainProductContent")
private String mainProductContent;
@Column(name = "mainProductHit")
private int mainProductHit;
@Column(name = "mainProductDate")
private String mainProductDate;
@Column(name = "mainProductCategory")
private String mainProductCategory;
public String getMainProductCategory() {
return mainProductCategory;
}
public void setMainProductCategory(String mainProductCatagory) {
this.mainProductCategory = mainProductCatagory;
}
public int getMainProductId() {
return mainProductId;
}
public void setMainProductId(int mainProductId) {
this.mainProductId = mainProductId;
}
public String getMainProductTitle() {
return mainProductTitle;
}
public void setMainProductTitle(String mainProductTitle) {
this.mainProductTitle = mainProductTitle;
}
public String getMainProductContent() {
return mainProductContent;
}
public void setMainProductContent(String mainProductContent) {
this.mainProductContent = mainProductContent;
}
public int getMainProductHit() {
return mainProductHit;
}
public void setMainProductHit(int mainProductHit) {
this.mainProductHit = mainProductHit;
}
public String getMainProductDate() {
return mainProductDate;
}
public void setMainProductDate(String mainProductDate) {
this.mainProductDate = mainProductDate;
}
}
mainproduct 테이블의 column들을 작성하고
getter/setter를 정의했다
반응형
'Spring' 카테고리의 다른 글
[SpringBoot/IntelliJ] Mysql연동 (0) | 2022.04.14 |
---|---|
[SpringBoot/IntelliJ] Thymeleaf URL표현(th:href) (0) | 2022.04.14 |
[Spring Boot/IntelliJ] ResponseBody to JSON (0) | 2022.04.13 |
[Spring Boot/IntelliJ] Spring MVC (0) | 2022.04.13 |
[Spring Boot/IntelliJ] SpringBoot 시작 (0) | 2022.04.13 |