서비스 =>mybatis => 설계 + 구현(SQL문)
저장소 =>jpa, hibernate => 설계 + 구현(SQL)
https://docs.spring.io/spring-data/mongodb/docs/3.0.9.RELEASE/reference/html/#mongodb.repositories.queries
- Application.java에 어노테이션 추가하기
@EnableMongoRepositories(basePackages = "com.example.repository")
@Repository
public interface BookRepository
extends MongoRepository<Book, Long> {
interface를 만들고 MongoRepository를 extends한다.
<엔티티, 기본타입>
공식문서에 나와있는 repository query method 사용
List<Board> findByTitle(String title);
List<Board> findByWriter(String writer);
// 조회수가 100인 것 조회
List<Board> findByHit(long hit);
// 크다 작다
// 조회수가 200미만인것 목록으로 조회
List<Board> findByHitLessThan(long hit);
// 조회수가 300이상 인 것 목록으로 조회
List<Board> findByHitGreaterThanEqual(long hit);
// 여러개 포함,
// 글 번호가 컬렉션에 담긴 1, 5, 7만 조회
List<Board> findByNoIn(Collection<Long> nos);
List<Board> findByNoNotIn(Collection<Long> nos);
직접 쿼리문 작성
// 검색한 개수 + 페이지네이션
@Query(value = "{title: {$regex: ?0}}")
List<Book> getBookList(String title, Pageable pageable);
// 코드가 일치하는 것 개수 조회
@Query(value = "{title : {$regex: ?0}}", count = true)
long getBookCount(String title);
// 코드가 일치하는 것 내용 조회
@Query(value = "{code : ?0}")
Book getBookOne(long code);
// 코드가 일치하는 것 삭제
@Query(value = "{code : ?0}", delete = true)
long deleteBookCode(long code);
// 존재 유무 확인
@Query(value = "{code : ?0}", exists = true)
boolean isBookCode(long code);
// 코드가 여러개 인 것 조회
@Query(value = "{code : {$in: ?0}}")
List<Book> getBookCodeList(Collection<Long> code);
오라클 문과 쿼리문 비교
// 제목과 가격이 일치하는 것 조회
@Query(value = "{title: ?0, price: ?1}")
List<Book> getBookTitleAndPriceList(String title, long price);
// 제목과 가격이 일치하는 것 조회 in Oracle
@Query(value = "{title: #{#title}, price: #{#price}}")
List<Book> getBookTitleAndPriceList1(@Param("price")
long p, @Param("title") String t);
2개가 같은 건데 밑에꺼는 순서가 바뀌어도 됨
위에꺼는 순서로 하고, 밑에꺼는 네임으로 지정함
엔티티로 줬을 때는 꺼내서 줘야하는데 ,순서로 받으면 이게 안됨.
가격따로 주고 코드 따로주는 게 안되는 것도 있음 mybatis는 파라미터가 1개밖에 안됨
@Query(value = "{title: #{#book.title}, price: #{#book.price}}")
List<Book> getBookTitleAndPriceList2(@Param("book") Book book);
// 전체 목록 가져오기 code기준 내림차순
@Query(value = "{}", sort = "{code : -1}")
List<Book> getBookListSortCode();
// 제목과 가격이 일치하는 것 조회
@Query(value = "{$or : [{title: ?0}, price: ?1]}")
List<Book> getBookListTitleOrPriceList();
// 가격이 ?0 이상 인 것($gte, $gt, $lte, $lt)
@Query(value = "{price : {$gte: ?0}}")
List<Book> getBookPriceGte(long price);