기록해! 정리해!

JPAJarThymeleaf-2 (동적DSL) 본문

SpringBoot

JPAJarThymeleaf-2 (동적DSL)

zsuling 2022. 10. 20. 10:10

1. DynamicBoardRepository

:QuerydslPredicateExecutor

package com.rubypaper.board;

import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.CrudRepository;

public interface DynamicBoardRepository 
        extends CrudRepository <Board, Long>, 
                QuerydslPredicateExecutor<Board>{

}

2. DynamicQueryTest

package com.rubypaper.test;

import java.util.List;

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.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;

import com.querydsl.core.BooleanBuilder;
import com.rubypaper.board.Board;
import com.rubypaper.board.DynamicBoardRepository;
import com.rubypaper.board.QBoard;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DynamicQueryTest {

	 
		@Autowired
		private DynamicBoardRepository boardRepo;
		
	   
	    @Test 
	 	public void testDynamicQuery() {
		 
	    	String searchCondition = "TITLE";
	    	String searchKeyword = "J";
	    	
	    	BooleanBuilder builder = new BooleanBuilder();
	    	
	    	QBoard qboard = QBoard.board;
	    	
	    	if(searchCondition.equals("TITLE")) {
	    		builder.and(qboard.title.like("%"+ searchKeyword + "%"));
	    	}else if(searchCondition.equals("CONTENT")) {
	    		builder.and(qboard.content.like("%"+ searchKeyword + "%"));
	    	}
	    	
	    	Pageable paging = PageRequest.of(0, 5);
	    	Page<Board> boardList= boardRepo.findAll(builder, paging);
	        
	    	for(Board m:boardList) {
	    		System.out.println("==>" + m.toString());

	    	}
	    }
}

>>

: 기존에는 BoardRepository 에 계속 메소드를 추가해서 사용했는데 

동적DSL은 DynamicBoardRepository에 추가를 하지않아도 됐음

 

 

Comments