Gradle Project, Java, 2.4.8
Group : com.zerock
Artifact : boot01
Dependencies
Add Dependencies : Spring boot devTools, Lombok, Spring Web, mustache, JPA, Oracle Driver 6개
Generate
Zip 파일 다운르드
Community에서 Spring.io를 이용하는 것은 Ultimate에서 프로젝트 새로 만들 때 Spring initializer로 하는 것과 같음
Open Project - Trust Project
스프링 버전이 달라지면 모듈을 새로 받음
Setting - File Encodings - 세 개 UTF-8로 바꾸기
Build, Executions, Deployment - Gradle - Build and run using, Run test Using - IntelliJ IDEA로 바꾸기
Dependencies에 Lombok, Spring Web 두 개만 추가하고 새로 Generate
application.properties에 server.port = 7070 추가
com.zerock 밑에 SampleController.java 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.zerock.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class SampleController { @GetMapping("/") public String indexing() { return "Main Page"; } @GetMapping("/hello") public String sayHello() { return "Hello World"; } } | cs |
localhost:7070 접속하면 Main Page 확인
localhost:7070/hello 접속하면 Hello World 확인
34페이지
Lombok의 어노테이션
@NonNull
@Cleanup
@Getter/Setter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor
@Data
@Value
@Log
@Builder
@SneakyThrows
@Synchronized
@Getter(lazy=true)
com.zerock 밑에 domain 패키지 생성
domain 밑에 SampleVO.java 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.zerock.domain; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Getter @Setter @ToString public class SampleVO { private String val1; private String val2; private String val3; } | cs |
@Getter, @Setter를 사용하면
val1, val2, val3의 Getter, Setter를 굳이 해주지 않아도 된다
SampleController 수정
1 2 3 4 5 | @GetMapping("/sample") public SampleVO makeSample() { SampleVO vo = new SampleVO(); return vo; } | cs |
SampleController 수정
1 2 3 4 5 6 7 8 9 | @GetMapping("/sample") public SampleVO makeSample() { SampleVO vo = new SampleVO(); vo.setVal1("v1"); vo.setVal2("v2"); vo.setVal3("v3"); System.out.println(vo); //vo.toString()과 같음 return vo; } | cs |
localhost:7070/sample 접속하면
{"val1":"v1","val2":"v2","val3":"v3"} 출력
@Getter, @Setter를 한 번에 이용할거면 @Data로 가능
@ToString(exclude = {"val3"})
val3 제외하고 ToString
test 패키지에
@RunWith하면 빨갛게 뜸
-> JUnit 임포트
-> class 임포트
@RunWith(SpringRunner.class)
@WebMvcTest(SampleController.class)
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package com.zerock.controller; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; @RunWith(SpringRunner.class) //@SpringBootTest @WebMvcTest(SampleController.class) public class SampleControllerTest { @Autowired MockMvc mock; @Test public void testHello() throws Exception { mock.perform(get("hello")).andExpect(content().string("Hello World")); } } | cs |
새 프로젝트
spring.io
Gradle
Java
Spring Boot : 2.5.2
Group : com.zerock
Artifact : boot02
package name : com.zerock
Pacakaging : Jar
Java : 8
dependencies
lobmok, jpa, web, dev tools, oracle driver
Generate
Open Project
application.properties 수정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | server.port = 7070 spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe spring.datasource.username=hr spring.datasource.password=hr #스키마 생성(create) #새롭게 만들기 create 연속해서 update spring.jpa.hibernate.ddl-auto=update #DDL 생성 시 데이터베이스 고유의 기능을 사용하는가? spring.jpa.generate-ddl=false #실행되는 SQL문을 보여줄 것인가? spring.jpa.show-sql=true #데이터베이스는 무엇을 사용하는가? spring.jpa.database=oracle #로그 레벨 상세로그 debug 간략 info logging.level.org.hibernate=debug #logging.level.org.hibernate.SQL=debug #오라클 드라이버 상세 지정 #10g 11g 같다 spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect | cs |
처음에는 update 대신 create
57, 58, 59페이지
datasource 설정
com.zerock 밑에 domain 패키지 추가
domain 밑에 Board.java 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.zerock.domain; import lombok.Getter; import lombok.Setter; import lombok.ToString; import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; import java.sql.Timestamp; @Getter @Setter @ToString public class Board { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BOARD_SEQ") @SequenceGenerator(sequenceName = "BOARD_SEQ", allocationSize = 1, name = "BOARD_SEQ") private Long bno; private String title; private String writer; private String content; @CreationTimestamp private Timestamp regdate; @UpdateTimestamp private Timestamp updatedate; } | cs |
BoardRepository 인터페이스 추가
1 2 3 4 5 6 7 | package com.zerock.domain; import org.springframework.data.repository.CrudRepository; public interface BoardRepository extends CrudRepository<Board, Long> { } | cs |
Board.java 수정
@ToString 밑에
@Entity
@Table(name="tbl_boards") 추가
BoardRepositoryTest.java 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.zerock.domain; import com.sun.javaws.IconUtil; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.stream.Stream; @RunWith(SpringRunner.class) @SpringBootTest public class BoardRepositoryTest { @Autowired private BoardRepository boardRepo; @Test public void inspect() { // 실제 객체의 클래스 이름 Class<?> clz = boardRepo.getClass(); System.out.println(clz.getName()); // 클래스가 구현하고 있는 인터페이스 이름 Class<?>[] interfaces = clz.getInterfaces(); Stream.of(interfaces).forEach(inter-> System.out.println(inter.getName())); // 클래스의 부모 클래스 Class<?> superClass = clz.getSuperclass(); System.out.println(superClass.getName()); } } | cs |
삽입 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package com.zerock.domain; import com.sun.javaws.IconUtil; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.stream.Stream; @RunWith(SpringRunner.class) @SpringBootTest public class BoardRepositoryTest { @Autowired private BoardRepository boardRepo; @Test public void inspect() { // 실제 객체의 클래스 이름 Class<?> clz = boardRepo.getClass(); System.out.println(clz.getName()); // 클래스가 구현하고 있는 인터페이스 이름 Class<?>[] interfaces = clz.getInterfaces(); Stream.of(interfaces).forEach(inter-> System.out.println(inter.getName())); // 클래스의 부모 클래스 Class<?> superClass = clz.getSuperclass(); System.out.println(superClass.getName()); } @Test public void testInsert() { Board board = new Board(); board.setTitle("게시글의 제목"); board.setContent("게시글의 내용 넣기"); board.setWriter("user00"); boardRepo.save(board); } } | cs |
application.properties에 create를 update로 수정
수정, 삭제, 검색 기능 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | package com.zerock.domain; import com.sun.javaws.IconUtil; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Optional; import java.util.stream.Stream; @RunWith(SpringRunner.class) @SpringBootTest public class BoardRepositoryTest { @Autowired private BoardRepository boardRepo; @Test public void inspect() { // 실제 객체의 클래스 이름 Class<?> clz = boardRepo.getClass(); System.out.println(clz.getName()); // 클래스가 구현하고 있는 인터페이스 이름 Class<?>[] interfaces = clz.getInterfaces(); Stream.of(interfaces).forEach(inter-> System.out.println(inter.getName())); // 클래스의 부모 클래스 Class<?> superClass = clz.getSuperclass(); System.out.println(superClass.getName()); } @Test public void testInsert() { Board board = new Board(); board.setTitle("게시글의 제목"); board.setContent("게시글의 내용 넣기"); board.setWriter("user00"); boardRepo.save(board); } @Test public void testRead() { boardRepo.findById(1L).ifPresent((board) -> { System.out.println(board); }); } @Test public void testUpdate() { System.out.println("Read First.........................."); Optional<Board> board = boardRepo.findById(2L); System.out.println("Update Title........................"); board.get().setTitle("수정된 제목입니다."); System.out.println("Call Save()........................."); boardRepo.save(board.get()); } @Test public void testDelete() { System.out.println("DELETE Entity "); boardRepo.deleteById(1L); } } | cs |
89페이지
BoardRepository
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.zerock.domain; import org.springframework.data.repository.CrudRepository; import java.util.Collection; import java.util.List; public interface BoardRepository extends CrudRepository<Board, Long> { // 제목검색 기능 만들기 public List<Board> findBoardByTitle(String title); // 작성자 검색기능 만들기 public Collection<Board> findByWriter(String writer); // 작성자에 대한 like % 키워드 public Collection<Board> findByWriterContaining(String writer); } | cs |
Boot03ApplicationTest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package com.zerock; import com.zerock.domain.Board; import com.zerock.domain.BoardRepository; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Collection; @RunWith(SpringRunner.class) @SpringBootTest class Boot03ApplicationTests { @Autowired private BoardRepository repo; @Test void contextLoads() { } @Test public void testInsert200() { for (int i = 1; i <= 200; i++) { Board board = new Board(); board.setTitle("제목.." + i); board.setContent("내용...." + i + "채우기 "); board.setWriter(("user0" + (i % 10))); repo.save(board); } } @Test public void testByTitle() { //Java8 repo.findBoardByTitle("제목..177") .forEach(board -> System.out.println(board)); } @Test public void testByWriter() { Collection<Board> result = repo.findByWriter("user00"); result.forEach( board -> System.out.println(board) ); } @Test public void testByWriterContaning() { Collection<Board> results = repo.findByWriterContaining("05"); results.forEach(board -> System.out.println(board)); } } | cs |
'Java' 카테고리의 다른 글
| 스프링 게시판 프로젝트 2일차 springBoot gradle setting (0) | 2021.12.25 |
|---|---|
| 스프링 게시판 프로젝트 시작 1일차 프로젝트 생성, 깃, 깃허브 연동 (0) | 2021.12.24 |
| 스마트팩토리 13주 60일차 java 10일차 (0) | 2021.07.22 |
| 스마트팩토리 12주 57일차 java 9일차 (0) | 2021.07.16 |
| 스마트팩토리 12주 56일차 java 8일차 (0) | 2021.07.15 |




최근댓글