Skip to content

Latest commit

 

History

History
94 lines (78 loc) · 3.61 KB

File metadata and controls

94 lines (78 loc) · 3.61 KB

Querydsl Tutorial

Prerequisites

  • Installed devonfw IDE

  • User should have basic Java development experience

Learning goals.

Here in this tutorial you will learn * To integrate Querydsl in your project * To build Querydsl expressions

restoreDevonfwIde(["java","mvn"])

devonfw-ide has been installed for you.

First, clone the QueryDslTutorial repository from GitHub. It contains an application with a simple REST service.

Clone QueryDslTutorial repository

In order to use Querydsl, we need to add the Querydsl dependencies to our Maven project and configure the Maven APT plugin. The JPAAnnotationProcessor will find domain types annotated with the javax.persistence.Entity annotation and generate query types for them.

== Add Querydsl dependencies to Maven Project

changeFile("QueryDslTutorial/pom.xml" , {"file": "files/querydsl-dependencies.txt", "placeholder": "<QueryDslDependencies>"}) changeFile("QueryDslTutorial/pom.xml" , {"file": "files/querydsl-annotation-processor.txt", "placeholder": "<AnnotationProcessor>"})

Next, navigate to the devonfw/QueryDslTutorial directory.

changeWorkspace("devonfw/workspaces/main/QueryDslTutorial")

The data model consists of a Fruit entity with Id, name, color, and price fields.

To extend the application with custom queries in Querydsl, we need to create a FruitFragment-interface and its implementation. This will be extended by the FruitRepository along with the CrudRepository.

== Create FruitFragment Interface

createFile("src/main/java/org/acme/spring/data/jpa/repo/fruit/FruitFragment.java","files/FruitFragment.java")

== Extend FruitRepository with FruitFragment Interface

changeFile("src/main/java/org/acme/spring/data/jpa/repo/fruit/FruitRepository.java", {"content": "public interface FruitRepository extends CrudRepository<Fruit, Long>, FruitFragment {", "placeholder": "public interface FruitRepository extends CrudRepository<Fruit, Long> {"})

== Implement FruitFragment Interface

createFile("src/main/java/org/acme/spring/data/jpa/repo/fruit/FruitFragmentImpl.java","files/FruitFragmentImpl.java")

== Querying with Querydsl

displayContent("Querydsl Expressions", [{ "file": "files/findAllQueryDslFunc.asciidoc" }])

== Implement GET-Request in FruitResource

changeFile("src/main/java/org/acme/spring/data/jpa/repo/fruit/FruitResource.java", {"placeholder": "return null;", "file": "files/function.txt"})

Now you can run the application in development mode.

Run Application

executeCommand("devon mvn compile quarkus:dev", "devon mvn compile quarkus:dev", "quarkus.http.host=0.0.0.0")

Accessing the application with the following link will send a GET-request for fruits named Cherry. The FruitResource will call the FindByName method and pass the name parameter to the FruitRepository’s `findAllQueryDslName method, which it inherents from the FruitFragment interface. The querying occurs in the method’s implementation, as explained in step 6. https://-8080-.environments.katacoda.com/fruits/name/Cherry

Conclusion

To summarize, we learned how to integrate Querydsl into your Maven project and querying with Querydsl. For more information on queries, visit the Querydsl documentation