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

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-data-sqlfile

Build Status

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>16e42f6ffd</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);

    @SqlFromResource(path = "select_user_by_name.sql")
    User findByUsername(String username);
}

Recompile project with mvn clean compile and you'll get generated repository classes with SQL queries which are injected from resources folder. See example for more details.

@Repository
public interface UserRepositoryImpl 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(int userId);

  @Query(
      value = "SELECT *     FROM users     WHERE username = :username",
      nativeQuery = true
  )
  User findByUsername(String username);
}

Configuration

You can specify class postfix in maven-compile-plugin configuration (which is Impl by default):

<compilerArgs>
	<arg>-AclassPostfix=Impl</arg>
</compilerArgs>

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