기록해! 정리해!

JPAJarTymeleaf-1 검색하기 ( 동적 DSL 사용하기 ) 본문

SpringBoot

JPAJarTymeleaf-1 검색하기 ( 동적 DSL 사용하기 )

zsuling 2022. 10. 18. 17:52

1.pom.xml

dependecy에 추가

	<!--  querydsl Setting 추가(1) -->
		<dependency>
			<groupId>com.querydsl</groupId>
			<artifactId>querydsl-jpa</artifactId>
		</dependency>
		
		<!--  querydsl Setting 추가(2) -->
		<dependency>
			<groupId>com.querydsl</groupId>
			<artifactId>querydsl-apt</artifactId>
		</dependency>

build에 추가

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			
			<!-- 동적 DSL을 위한  plugin 추가 -->
			<plugin>
				<groupId>com.mysema.maven</groupId>
				<artifactId>apt-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>process</goal>
						</goals>
					    <configuration>
					    	<outputDirectory>src/main/querydsl</outputDirectory>
					    	<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
					    </configuration>
					</execution>				
				</executions>					
			</plugin>			
		</plugins>
	</build>

</project>

+ Maven Update

>> 설정완료.


2. BoardRepository (책 257p)

findBoardBy Title Containing 

= 메소드(엔터티는 생략가능) 변수 Like문

 

3. BoardService

: getBoardList 괄호안에 vo 받아줫음

4. BoardServiceImpl

: getBoardList 괄호안에 vo 받아줫음

package com.rubypaper.board;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BoardServiceImpl implements BoardService{

	@Autowired
	private BoardRepository boardRepo;
	
	@Override
	public void insert(Board vo) {
		boardRepo.save(vo);
	}

	@Override
	public List<Board> getBoardList(Board vo) {

		return (List<Board>)boardRepo.findAll();
	}

	@Override
	public Optional<Board> getBoard(Long seq) {

		return boardRepo.findById(seq);
	}

	@Override
	public void delete(Long seq) {
	
		boardRepo.deleteById(seq);
	}

	@Override
	public void update(Board vo) {

		boardRepo.save(vo);
	}

	@Override
	public List<Board> getfindByTitle(Board vo) {

		return boardRepo.findByTitleContaining(vo.getSearchKeyword());
	}

	@Override
	public List<Board> getfindByWriter(Board vo) {
		
		return boardRepo.findByWriterContaining(vo.getSearchKeyword());
	}

}

5. BoardController

	@RequestMapping("/getBoardList.do")
	public  void getBoardList(Model  model, Board vo) {
		
		if (vo.getSearchCondition() == null || vo.getSearchKeyword() == null ||  vo.getSearchKeyword().equals("") ) {
		  
			model.addAttribute("li", boardService.getBoardList(vo));
		
		} else if(vo.getSearchCondition().equals("title")) {
						
			model.addAttribute("li", boardService.getfindByTitle(vo));
		
		} else if (vo.getSearchCondition().equals("writer")) {
			
			model.addAttribute("li", boardService.getfindByWriter(vo));
		}
	}

6. getBoardList

<form action="getBoardList.do">
<select name=searchCondition>
<option value="title"> 제목 </option>
<option value="writer"> 작성자 </option>
</select>
<input type=text name=searchKeyword>
<input type=submit value="검색하기">
</form>

>>

>100

> 앙

Comments