Springboot:jpa:@repositoryの使い方


1.一般照会の書き方
@Repository
@Transactional
public interface CalPositionsRepository extends JpaRepository {

    @Query(value = "SELECT * from t_cal_positions  where id =?1", nativeQuery = true)
    public CalPositionsEntity  queryOnePosition(String id);

}

2.クエリはlistを返します
@Repository
@Transactional
public interface UserRoleRepository extends JpaRepository {

    @Query(value = "select * from t_user_role where user_id=?1" ,nativeQuery = true)
    List selectUserRoleByUserId(String userId);
}

3.Long同intに戻る
@Repository
@Transactional
public interface CallOutDurationRepository extends JpaRepository {

    @Query(value = "select sum(total_times) from t_callout_duration  where monthT=?1", nativeQuery = true)
    public Long queryTotalByMonthT(String monthT);
}

4.Integerに戻る
@Repository
@Transactional
public interface CalResumeBuyEfficiencyRepository extends JpaRepository {

    @Modifying
    @Query(value = "insert into t_cal_resume_buy_efficiency (avg_day_efficiency,statistic_time,create_time) values(?1,?2,?3) ", nativeQuery = true)
    public Integer  insertBuyResumeEfficiency(String value,String statisticTime,String createTime);

}

5.like文の書き方
@Repository
@Transactional
public interface VResourceRepository extends JpaRepository,JpaSpecificationExecutor {

    @Query(value = "select * from v_resource where resource_name like CONCAT('%',?1,'%') " ,nativeQuery = true)
    List queryResourcesByName(String resourceName);
}