Skip to content

Store big SQL queries for your Spring Data repositories in resources

License

Notifications You must be signed in to change notification settings

VEINHORN/spring-data-sqlfile

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-data-sqlfile

When your SQL queries become huge, spring-data-sqlfile helps you to load them from resources folder. It makes your code clean and you can work with well-formed SQL queries in your IDE.

Install

Add JitPack repository

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Add dependency

<dependency>
    <groupId>com.github.VEINHORN.spring-data-sqlfile</groupId>
    <artifactId>sqlfile-processor</artifactId>
    <version>99228ac18b</version>
</dependency>

Usage

Just mark methods with SqlFromResource annotation and provide valid path to the SQL query.

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
    @SqlFromResource(path = "select_top_users.sql")
    List<User> findAll();

    @SqlFromResource(path = "select_user_by_id.sql")
    User findById(int userId);
}

Recompile project and you'll give generated repository with SQL queries injected from resources folder. See example for more details.

@Repository
public interface UserRepositoryGenerated extends JpaRepository<User, Integer> {
  @Query(
      value = "SELECT *     FROM users     WHERE id = 2;",
      nativeQuery = true
  )
  List<User> findAll();

  @Query(
      value = "SELECT *     FROM users     WHERE id = :userId",
      nativeQuery = true
  )
  User findById();
}

How it works

spring-data-sqlfile is an annotation processor which generates code during compile time.

About

Store big SQL queries for your Spring Data repositories in resources

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages