Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The Spring Dynamic R2DBC will make it easy to implement dynamic queries with R2DBC.

License

NotificationsYou must be signed in to change notification settings

joutvhu/spring-dynamic-r2dbc

Repository files navigation

The Spring Dynamic R2DBC will make it easy to implement dynamic queries with R2DBC.

How to use?

Install dependency

  • Add dependency
implementation'com.github.joutvhu:spring-dynamic-r2dbc:3.0.2'
<dependency>    <groupId>com.github.joutvhu</groupId>    <artifactId>spring-dynamic-r2dbc</artifactId>    <version>3.0.2</version></dependency>
  • Please choose theSpring Dynamic R2DBC version appropriate with your spring version.

    Spring Boot versionSpring Dynamic R2DBC version
    2.6.x1.4.2
    2.7.x1.5.2
    3.0.x3.0.2

Also, you have to choose aDynamic Query Template Provider to use,the Dynamic Query Template Provider will decide the style you write dynamic query template.

In this document, I will useSpring Dynamic Freemarker.If you migrated from a lower version, you should use it.

implementation'com.github.joutvhu:spring-dynamic-freemarker:1.0.0'
<dependency>    <groupId>com.github.joutvhu</groupId>    <artifactId>spring-dynamic-freemarker</artifactId>    <version>1.0.0</version></dependency>

Configuration

  • First you need to create a bean ofDynamicQueryTemplateProvider, that depending on which the Dynamic Query Template Provider you are using.
@BeanpublicDynamicQueryTemplateProviderdynamicQueryTemplateProvider() {FreemarkerQueryTemplateProviderprovider =newFreemarkerQueryTemplateProvider();provider.setTemplateLocation("classpath:/query");provider.setSuffix(".dsql");returnprovider;}
  • Next, you need to set the r2dbc repository'srepositoryFactoryBeanClass property toDynamicR2dbcRepositoryFactoryBean.class.
@EnableR2dbcRepositories(repositoryFactoryBeanClass =DynamicR2dbcRepositoryFactoryBean.class)

Dynamic query

  • Use annotation@DynamicQuery to define dynamic queries.
publicinterfaceUserRepositoryextendsR2dbcRepository<User,Long> {@DynamicQuery(value ="select * from USER where FIRST_NAME = :firstName\n" +"<#if lastName?has_content>\n" +"  and LAST_NAME = :lastName\n" +"</#if>"    )Flux<User>findUserByNames(LongfirstName,StringlastName);@Query(value ="select * from USER where FIRST_NAME = :firstName")Flux<User>findByFirstName(StringfirstName);@DynamicQuery(value ="select USER_ID from USER\n" +"<#if name??>\n" +"  where concat(FIRST_NAME, ' ', LAST_NAME) like %:name%\n" +"</#if>"    )Flux<Long>searchIdsByName(Stringname);@DynamicQuery(value ="select * from USER\n" +"<#if role??>\n" +"  where ROLE = :role\n" +"</#if>"    )Flux<User>findByRole(Stringrole);}

Load query template files

  • If you do not specify the query template on the@DynamicQuery annotation.TheDynamicQueryTemplateProvider will find them from external template files based on theTemplateLocation andSuffix that you specify in the provider.

  • If you don't want to load the template from external template files you can use the following codeprovider.setSuffix(null);.

  • Each template will start with a template name definition line. The template name definition line must be start with two dash characters (--). The template name will have the following syntax.

    queryMethodName
    • queryMethodName can be provided through field@DynamicQuery.name. If@DynamicQuery.name is not provided,queryMethodName will beentityName:methodName whereentityName is entity class name,methodName is query method name
  • Query templates (Ex:resoucers/query/user-query.dsql)

--User:findUserByNamesselect*from USERwhere FIRST_NAME= :firstName<#if lastName?has_content>and LAST_NAME= :lastName</#if>-- User:searchIdsByNameselect USER_IDfrom USER<#if name??>where concat(FIRST_NAME,'', LAST_NAME)like %:name%</#if>-- get_user_by_username_and_emailselect*from USER<@where><#if username??>and USERNAME= :username</#if><#if email??>and EMAIL= :email</#if></@where>
  • Now you don't need to specify the query template on@DynamicQuery annotation.
publicinterfaceUserRepositoryextendsReactiveCrudRepository<User,Long> {@DynamicQueryFlux<User>findUserByNames(LongfirstName,StringlastName);@Query(value ="select * from USER where FIRST_NAME = :firstName")Flux<User>findByFirstName(StringfirstName);@DynamicQueryFlux<Long>searchIdsByName(Stringname);@DynamicQuery(name ="get_user_by_username_and_email")Flux<User>getUserWithUsernameAndEmail(Stringusername,Stringemail);}

About

The Spring Dynamic R2DBC will make it easy to implement dynamic queries with R2DBC.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

 
 
 

Languages


[8]ページ先頭

©2009-2025 Movatter.jp