-
[Spring Boot] Spring Boot (2) - 기본 로직 + MySQL 연동JAVA/SPRING 2020. 1. 7. 21:36728x90
1. MySQL 연동 설정
//application.properties # MVC View spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp #MySQL spring.datasource.url=jdbc:mysql://localhost:3306/boot_study?serverTimezone=UTC spring.datasource.username=root spring.datasource.password=1111 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.database=mysql spring.jpa.show-sql=true
plugins { id 'org.springframework.boot' version '2.2.2.RELEASE' id 'io.spring.dependency-management' version '1.0.8.RELEASE' id 'java' } apply plugin: 'io.spring.dependency-management' group = 'com.ms' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' runtimeOnly 'com.h2database:h2' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } //jsp 연동 compile('org.apache.tomcat.embed:tomcat-embed-jasper') compile('javax.servlet:jstl:1.2') //mysql implementation 'org.hsqldb:hsqldb' implementation group: 'org.lazyluke', name: 'log4jdbc-remix', version: '0.2.7' runtime('mysql:mysql-connector-java') } test { useJUnitPlatform() }
DB와 Table 생생
2. domain 클래스 생성
domain 클래스는 Table과 동일한 구조를 갖고 있습니다.
만약 동일한 구조가 아니라면 JPA를 사용하는데 문제가 발생합니다.
package com.ms.study.domain; import java.time.LocalDateTime; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @Entity //domain 클래스라는 것을 나타냄 @Table(name="board") @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) //Lazy 예외를 방지 public class Board { @Id //Primary key 컬럼인 것을 나타냄 @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id") private Integer id; @Column(name="writer") private String writer; @Column(name="title") private String title; @Column(name="content") private String content; @CreationTimestamp @Column(name="created_time") private LocalDateTime created_time; @UpdateTimestamp @Column(name="updated_time") private LocalDateTime updated_time; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getWriter() { return writer; } public void setWriter(String writer) { this.writer = writer; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public LocalDateTime getCreated_time() { return created_time; } public void setCreated_time(LocalDateTime created_time) { this.created_time = created_time; } public LocalDateTime getUpdated_time() { return updated_time; } public void setUpdated_time(LocalDateTime updated_time) { this.updated_time = updated_time; } }
3. Repository 인터페이스 생성
DB와 연동
package com.ms.study.repository; import org.springframework.data.jpa.repository.JpaRepository; import com.ms.study.domain.Board; public interface BoardRepository extends JpaRepository<Board, Integer> { }
4. Service 클래스 생성
비지니스 로직을 구현
package com.ms.study.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ms.study.domain.Board; import com.ms.study.repository.BoardRepository; @Service public class BoardService { @Autowired BoardRepository boardRepo; public List<Board> findAll() { List<Board> list = boardRepo.findAll(); return list; } }
5. Controller 클래스 생성
클라이언트에게 받은 요청에 대한 응답을함
package com.ms.study.Controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.servlet.ModelAndView; import com.ms.study.domain.Board; import com.ms.study.service.BoardService; @Controller public class BoardController { @Autowired BoardService service; @GetMapping("/board") public ModelAndView boardList() { List<Board> list = service.findAll(); ModelAndView nextView = new ModelAndView("board/list"); nextView.addObject("boardList", list); return nextView; } }
6. View 페이지(jsp) 생성
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body> <table> <thead> <tr> <th>No</th> <th>Title</th> <th>Writer</th> </tr> </thead> <tbody> <c:forEach var="board" items="${boardList }"> <tr> <td>${board.id }</td> <td><a href="/board/${board.id }">${board.title }</a></td> <td>${board.writer }</td> </tr> </c:forEach> </tbody> </table> </body> </html>
728x90'JAVA > SPRING' 카테고리의 다른 글
Gradle vs Maven (0) 2020.01.12 [Spring Boot] Spring Boot (3) - CRUD (0) 2020.01.08 [Spring Boot] Spring Boot (1) - 개발환경 설정 (0) 2020.01.05 [SPRING] MAPPER 비교연산자 에러(The content of elements must consist of well-formed character data or markup) (0) 2019.08.29 [SPRING] MultipartFile 파일업로드 없을 경우 (1) 2019.08.21