기록해! 정리해!

JPAJarThymeleaf-2 (Test) 본문

SpringBoot

JPAJarThymeleaf-2 (Test)

zsuling 2022. 10. 19. 13:11

책 260p

1. BoardTest에 값 넣기 insert

: BoardTest Junit으로 실행시킴

package com.rubypaper.test;

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 com.rubypaper.board.Board;
import com.rubypaper.board.BoardRepository;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BoardTest {
    
	@Autowired
	private BoardRepository boardRepo;
	
	@Test
	public void test() {
		
		for(int i=1; i<=5; i++) {
		Board board = new Board();
		board.setTitle("JPA 공부하기"+i);
		board.setWriter("닭강정"+i);
		board.setContent("JPA 열심히 공부합시다."+i);
		board.setPassword("1234");
		
		boardRepo.save(board);
		}
	}
}

2. select

	@Test  // ( selectAll )
	public  void  testSelectAll() {
	
		List<Board>  li = (List<Board>)boardRepo.findAll();
		for(Board m :li ) {
			
		  System.out.println(m.toString());	
		  
		}
		
	}

3.selectOne (값 하나만 찾기)

	@Test  // ( selectOne )
	public  void  testSelectOne() {
	      // Optional 이란? Null Point Exception 을 방지해준다.
		  Optional<Board>  m =  boardRepo.findById(3L);					
		  System.out.println(m.toString());			  
	}


+ 제목에 JPA가 들어있는 레코드만 출력해보시오

1) BoardRepository에 코드추가

List<Board> findByTitleContaining(String searchkeyword);

2) BoardTest

	@Test 
	public  void  selectTitleLike() {
	      String str = "JPA";
	      List<Board>  li = boardRepo.findByTitleContaining(str);			
		  for(Board m:li) {
	      System.out.println("==>" + m.toString());			  
	  }	
	}


+ 보기와 같은 제목을 찾아보시오

1) BoardRepository에 코드추가

실제로 사용할 땐 메소드 이름에 One 빼야함.

2) BoardTest

	@Test 
	public  void  selectTest() {
		
		// 정해진 레코드 여러개 찾을 수 있다
		
		Optional<Board> m1 =boardRepo.findById(9L);
		System.out.println("==>" + m1.toString());

		List<Board>  li2 =boardRepo.findBoardByTitle("JAVA 공부하기1");
		for(Board m2:li2) {
		System.out.println("==>" + m2.toString());
		}
		  String title = "JAVA 공부하기1";
		  String writer = "밀크티1";
		  List<Board>  li3 = boardRepo.findBoardByTitleAndWriter(title, writer);			
		  for(Board m3:li3) {
		  System.out.println("==>" + m3.toString());		
		  }
   }

>>

 

 

4. @Before

: @Test전에 먼저 실행됨

: 조회하기 전에 값 추가하기

    @Before //@Test 실행 전에 먼저 실행됨
 	public void test() {
		
		for(int i=1; i<=4; i++) {
		Board board = new Board();
		board.setTitle("RPA 공부하기"+i);
		board.setWriter("우롱티"+i);
		board.setContent("RPA 열심히 공부합시다."+i);
		board.setPassword("7878");
		
		boardRepo.save(board);
		}
	}
		
		
	@Test  // ( selectAll )
	public  void  testSelectAll() {
	
		List<Board>  li = (List<Board>)boardRepo.findAll();
		for(Board m :li ) {
			
		  System.out.println(m.toString());	
		  
		}
		
	}

5. SeqBetween

1) BoardRepository에 코드추가

	List<Board> findByTitle(String str);
	List<Board> findBySeqBetween(Long k1, Long k2);

2) BoardTest

	@Test
	public void selectTest() {
		List<Board> li =boardRepo.findBySeqBetween(3L, 7L);
		for(Board m : li) {
			System.out.println(m);
		 }
		}

>>

6. SeqLessThan

1) BoardRepository

	List<Board> findBySeqLessThan(Long k1);

2) BoardTest

	@Test
	public void selectLessThan() {
		List<Board> li =boardRepo.findBySeqLessThan(5L);
		for(Board m : li) {
			System.out.println(m);
		 }
		}

7. null값 넣기

1) BoardTest

    @Test 
 	public void test() {
		
		for(int i=1; i<=7; i++) {
		Board board = new Board();
		board.setTitle("공부하기"+i);
		board.setWriter("공차"+i);
		board.setContent("열심히 공부합시다."+i);
		board.setPassword("");
		
		boardRepo.save(board);
		}
	}

>>오라클

8. is null

1) BoardRepository

	List<Board> findByPasswordIsNull();

2) BoardTest

	@Test
	public void selectLessThan() {
		List<Board> li =boardRepo.findByPasswordIsNull();
		for(Board m : li) {
			System.out.println(m);
		 }
		}

>>

9. Like /Containing 차이

1) BoardRepository

	List<Board> findByTitleLike(String str);
	List<Board> findByTitleContaining(String str);

2) BoardTest

	@Test
	public void selectLike() {
		String str="%S%";
		List<Board> li =boardRepo.findByTitleLike(str);
		for(Board m : li) {
			System.out.println(m);
		 }
		
		String str1="S";
		List<Board> li1 =boardRepo.findByTitleContaining(str1);
		for(Board m1 : li1) {
			System.out.println(m1);
		 }
		
		}

 

>>

 

10. 정렬

1) BoardRepository

	List<Board> findByTitleContainingOrderBySeqDesc(String str);

2) BoardTest

	@Test
	public void selectLike() {
		String str="Spring";
		List<Board> li =boardRepo.findByTitleContainingOrderBySeqDesc(str);
		for(Board m : li) {
			System.out.println(m);
		 }
	}

>>

+

1) BoardRepository

List<Board> findByTitleContainingOrderBySeqDesc(String str);
List<Board> findByOrderBySeqDesc();

2) BoardTest

@Test
	public void selectLike() {
		String str="Spring";
		
		List<Board> li =boardRepo.findByTitleContainingOrderBySeqDesc(str);
		for(Board m : li) {
			System.out.println(m);
		 }
		
		List<Board> li1 =boardRepo.findByOrderBySeqDesc();
		for(Board m1 : li1) {
			System.out.println(m1);
		 }
	}

11. Or 연산

List<Board> findByTitleContainingOrWriterContaining(String str1, String str2);
	@Test
	public void selectLike() {
		String str="Spring";
		
		List<Board> li =boardRepo.findByTitleContainingOrderBySeqDesc(str);
		for(Board m : li) {
			System.out.println(m);
		 }
		
		List<Board> li1 =boardRepo.findByTitleContainingOrWriterContaining("J", "6");
		for(Board m1 : li1) {
			System.out.println(m1);
		 }
	}

>>

Comments