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.
cloneRepository("", "https://github.com/EduardKrieger/QueryDslTutorial.git")
In the next step, integrate Querydsl into your Maven project.
== 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.
executeCommand("mvn compile quarkus:dev", "mvn compile quarkus:dev")
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
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