ProductRepository 2022/04/19

파일명 ProductCountRepository.java

package com.example.repository;

import com.example.entity.ProductCountEntity;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;



@Repository
public interface ProductCountRepository extends JpaRepository<ProductCountEntity, Long>{
    
    @Query(value = "SELECT SUM(CNT) FROM PRODUCTCOUNT WHERE PRODUCT_NO=:no GROUP BY PRODUCT_NO", nativeQuery = true)
    public long selectProductCountGroup(
        @Param(value = "no")Long ProductNo );

    // nativeQuery에서 insert, update, delete는 데이터의 변화가 생심 @Modifying(clearAutomatically = true) 사용
    @Modifying(clearAutomatically = true)
    @Query(value = "DELETE FROM PRODUCT WHERE CNT >= :cnt", nativeQuery = true)
    public long deleteProductCountGroup(
        @Param(value = "cnt") Long cnt   );
    
    @Modifying(clearAutomatically = true)
    int deleteByCnt(long cnt);

    // findBy... SELECT
    
    // deleteBy ... DELETE

    // INSERT, UPDATE는 사용 X
}

파일명 ProductViewRepository.java

package com.example.repository;

import com.example.entity.ProductViewEntity;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductViewRepository extends JpaRepository<ProductViewEntity, Long>{
    
}

좋은 웹페이지 즐겨찾기