spring-data-jpa-extra

repository·master·Indexed 19 days ago

https://github.com/slyak/spring-data-jpa-extra

An extension for Spring Data JPA that provides MyBatis-like dynamic SQL support using Freemarker templates via the @TemplateQuery annotation. It allows returning arbitrary types, provides @TemplateQueryObject for parameter rendering, and includes GenericJpaRepository with utility methods such as mget, toggleStatus, and fakeDelete.

Tokens
1.4K
Snippets
4
Records
6
Agent score
14%

What's inside spring-data-jpa-extra

  1. Use @TemplateQueryObject for parameter rendering

    master
    The @TemplateQueryObject annotation is used to tell the content processing engine to render parameters provided by the properties of the annotated object. This is useful for passing complex parameter sets to a template query.
  2. Use Template Queries with @TemplateQuery

    master

    The @TemplateQuery annotation allows you to define dynamic native queries using external template files instead of writing JPQL/SQL in the repository interface. This enables MyBatis-like dynamic SQL capabilities.

    1. Define the Repository

    Extend GenericJpaRepository instead of the standard JpaRepository. Annotate your methods with @TemplateQuery.

    public interface SampleRepository extends GenericJpaRepository<Sample, Long> {
        @TemplateQuery
        Page<Sample> findByContent(@Param("content") String content, Pageable pageable);
    
        @TemplateQuery
        CustomVO findCustomVO(@Param("id") Long id);
    }

    2. Create the Template File

    Create a file in your classpath following the naming rule {EntityName}.sftl. By default, it looks in classpath:/sqls/. You can change this location using the property spring.jpa.template-location.

    • Naming Rule: {EntityName}.sftl. The EntityName is derived from the @Entity annotation or the class name.
    • Format: .sftl files use Freemarker syntax mixed with SQL.

    Example Sample.sftl:

    --findByContent
    SELECT * FROM t_sample WHERE 1=1
    <#if content??>
    AND content LIKE :content
    </#if>
    
    --findCustomVO
    SELECT id,name as viewName FROM t_sample WHERE id=:id

    3. IDE Support (IntelliJ IDEA)

    To get syntax highlighting for .sftl files:

    1. Go to Settings -> Editor -> File Types.
    2. Find Freemarker Template.
    3. Register a new type named *.sftl.
    4. Choose a Template Data Language (e.g., MYSQL).
  3. Configure spring-data-jpa-extra in Spring

    master

    You must configure your repositories to use the custom base class and factory bean provided by the library.

    Using Java Configuration: Use @EnableJpaRepositories and specify GenericJpaRepositoryImpl.class as the repositoryBaseClass and GenericJpaRepositoryFactoryBean.class as the repositoryFactoryBeanClass.

    Using XML Configuration: Use the <jpa:repositories> tag with repository-base-class="com.slyak.spring.jpa.GenericJpaRepositoryImpl" and repository-factory-bean-class="com.slyak.spring.jpa.GenericJpaRepositoryFactoryBean".

    @EnableJpaRepositories(
        basePackages = "your.packages", 
        repositoryBaseClass = GenericJpaRepositoryImpl.class, 
        repositoryFactoryBeanClass = GenericJpaRepositoryFactoryBean.class
    )
    <jpa:repositories 
        base-package="your.packages" 
        repository-base-class="com.slyak.spring.jpa.GenericJpaRepositoryImpl" 
        repository-factory-bean-class="com.slyak.spring.jpa.GenericJpaRepositoryFactoryBean"/>
  4. Install spring-data-jpa-extra

    master

    To use spring-data-jpa-extra, add the Maven dependency to your project. Choose the version based on your Spring/Spring Boot version:

    • Spring 3 (Spring Boot 1.5.x): Use 2.1.x.RELEASE.
    • Spring 4 (Spring Boot 2.0.x) or Spring 5: Use 3.0.0.RELEASE or later.

    If you are using Spring Boot, it is recommended to use the springboot-starter-jpa-extra starter instead.

    <dependency>
        <groupId>com.slyak</groupId>
        <artifactId>spring-data-jpa-extra</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>
  5. Use extra utility methods in GenericJpaRepository

    master

    By extending GenericJpaRepository, you gain access to several utility methods for common tasks:

    • mget(Collection<ID> ids): Batch get items and put the result into a Map<ID, T>.
    • mgetOneByOne(Collection<ID> ids): Get items one by one (useful for cache population) and return a Map<ID, T>.
    • findAllOneByOne(Collection<ID> ids): Get items one by one and return a List<T>.
    • toggleStatus(ID id): Toggles the entity status if the entity has a Status property.
    • fakeDelete(ID... id): Sets the entity status to Status.DELETED if the entity has a Status property.
    // batch get items and put the result into a map
    Map<ID, T> mget(Collection<ID> ids);
    
    // get items one by one for cache
    Map<ID, T> mgetOneByOne(Collection<ID> ids);
    
    // get items one by one for cache
    List<T> findAllOneByOne(Collection<ID> ids);
    
    // toggle entity status if it has a Status property
    void toggleStatus(ID id);
    
    // set entity status to Status.DELETED if it has a Status property
    void fakeDelete(ID... id);