spring-data-jpa-extra

spring data jpa with template dynamic query (eg: freemarker,velocity etc.) like mybatis. The master branch already support springboot2+ spring5+

Github星跟蹤圖

spring-data-jpa-extra

use spring data jpa more comfortable

I love spring-data-jpa, she set my hands free, crud methods are boring! However she is not perfect on dynamic native query and her return type must be an entity, although she provide us a specification solution, but i think it's heavy and not easy to use.

spring-data-jpa-extra comes to solve three problem:

  • dynamic native query support like mybatis
  • return type can be anything
  • no code, just sql

Example

  • first add ComponentScan

by java bean

@ComponentScan({"com.slyak","your.base.package"})

by xml

<context:component-scan base-package="com.slyak,your.base.package"/>
  • second extends GenericJpaRepository instead of JpaRepository
	public interface SampleRepository extends GenericJpaRepository<Sample, Long> {
		@TemplateQuery
		Page<Sample> findByContent(@Param("content")String content, Pageable pageable);
		@TemplateQuery
		CustomVO findCustomVO(@Param("id")Long id);
	}
  • third create a file named Sample.sftl in your classpath:/sqls/ (you can change this path by setting placeholder spring.jpa.template-location)

    • *.sftl naming rule: {EntityName}.sftl , @Entity("{EntityName}"), a simple class name will be used as EntityName if not set.
  • In version >=2.0.0.RELEASE , new template 'sftl' take place of xml (xml is complex, but it is also supported).
    sftl is a ftl template mixed with sqls. The template will be simplified as below. It can be recognized in Intellij IDEA , follow these steps:
    settings->Editor->File Types , find "Freemarker Template" and register a new type named '*.sftl',choose it's 'Template Data Language' such as 'MYSQL'.

--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

How to use?

you can use it by using source code or adding a maven dependency

notice: if the spring-data-jpa version >= 1.11.x (spring-boot version >= 1.5.x) please use 2.1.x.RELEASE .

notice: if you are using spring-boot 2.0+ or spring5 , please use the latest version 3.0.0.RELEASE, spring version, spring-data-jpa-extra version, :----------:, :-------------:, spring 3 (spring-boot 1.5.x), 2.1.x.RELEASE, spring 4 (spring-boot 2.0.x), 3.0.0.RELEASE, ```xml

com.slyak
spring-data-jpa-extra
2.1.2.RELEASE


config with annotation
```java
@EnableJpaRepositories(basePackages = "your.packages", repositoryBaseClass = GenericJpaRepositoryImpl.class, repositoryFactoryBeanClass = GenericJpaRepositoryFactoryBean.class)

or with xml

<jpa:repositories base-package="your.packages" repository-base-class="com.slyak.spring.jpa.GenericJpaRepositoryImpl" repository-factory-bean-class="com.slyak.spring.jpa.GenericJpaRepositoryFactoryBean"/>

Notice: if you are using spring-boot, the springboot-starter-jpa-extra should be the best choice!

2 Minute Tutorial

Template Query

Methods annotated with @TemplateQuery tells QueryLookupStrategy to look up query by content,this is often used by dynamic query.

Template Query Object

Object annotated with @TemplateQueryObject tells content process engine render params provided by object properties.

Entity Assemblers

Entity assemblers can assembler entity with other entities, such as one to many relation or one to one relation.

More Useful Methods (eg: mget togglestatus fakedelete)

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

TODO List

  • More types of content support (now freemarker)
  • More JPA comparison support (now hibernate)
  • Performance test and do some optimization
  • More other useful features
  • Change to the latest spring-data-jpa version

Change Log

CHANGELOG

How to use in old version ( <2.0 )

old version guide

主要指標

概覽
名稱與所有者slyak/spring-data-jpa-extra
主編程語言Java
編程語言Java (語言數: 1)
平台
許可證Apache License 2.0
所有者活动
創建於2016-03-11 16:09:55
推送於2022-05-30 22:42:04
最后一次提交2019-03-28 16:15:04
發布數2
最新版本名稱2.0.3.RELEASE (發布於 )
第一版名稱1.0.0 (發布於 )
用户参与
星數400
關注者數35
派生數116
提交數142
已啟用問題?
問題數55
打開的問題數24
拉請求數1
打開的拉請求數3
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?