Get query from file in SPRING BOOT using @Query

后端 未结 3 965
借酒劲吻你
借酒劲吻你 2021-01-25 03:11

I have a proyect that use to load queries this:

@Query(value = SELECT_BY_USER_ID, nativeQuery = true)
Employee findByUserId(@Param(\"userId\") String userId);
         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-25 03:37

    If you need to load SQL from resources folder, you can try spring-data-sqlfile library. It supports loading SQL queries from resources. So you just need to put your SQL queries to the resources folder and than you can reference them in SqlFromResource annotation:

    @Repository
    public interface UserRepository extends JpaRepository {
        @SqlFromResource(path = "select_user_by_id.sql")
        User findById(int userId);
    }
    

    The output will be like:

    @Repository
    public interface UserRepositoryGenerated extends JpaRepository {    
      @Query(
          value = "SELECT *     FROM users     WHERE id = :userId",
          nativeQuery = true
      )
      User findById(int userId);
    }
    

提交回复
热议问题