Chapter 1: How to configure QueryDSL environment in Maven environment

QueryDSL is a general query framework. The core principle of the framework is to create a safe type of query. At first, QueryDSL only supports Hibernate (HQL). After the continuous open source people joined the QueryDSL team, they successively released JPA, JDO, JDBC, Lucene, Hibernate Search, MangoDB, Collections and RDF (Relational Data Format) beans, etc.

Objectives of this chapter

Our chapters in this series mainly use QueryDSL and SpringDataJPA to integrate and use in the SpringBoot development environment. At present, the integration of SpringDataJPA and QueryDSL can be said to be a perfect combination. With a perfect combination, you have no reason to refuse to use them to complete the development of enterprise-level projects. Let's first use the idea development tool to build a maven project, and explain the dependencies and plugin configuration required by QueryDSL.

Build the project

We use idea to create a SpringBoot project and add dependencies such as Web, JPA, MySQL, etc. The pom configuration file is shown in the following code block:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yuqiyu.querydsl.sample</groupId>
    <artifactId>chapter1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>chapter1</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--<scope>provided</scope>--> <!--这里一定要注释,否则使用Application方式运行异常.-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

You can see that our project foundation has been built in the above code. Next, we introduce the relevant maven dependencies of QueryDSL, and add dependencies as shown in the following code block:

<!--queryDSL-->
  <dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
    <version>${querydsl.version}</version>
  </dependency>
  <dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <version>${querydsl.version}</version>
    <scope>provided</scope>
  </dependency>

Here we only introduce QueryDSL's JPA and APT-related dependencies to use. Since the QueryDSL framework needs to use the plug-in to configure the entity annotated with @Entity to automatically create a QBean as a query condition and automatically generate a QPath, we need to modify the pom.xml configuration file to add the QueryDSL plug-in, as shown in the following code block:

<build>
    <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--添加QueryDSL插件支持-->
            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources/java</outputDirectory>
                            <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    </plugins>
</build>

The first plugin in the plugin list is automatically added by SpringBoot for us, and it is a dependency plugin of SpringBoot related maven. The following plug-in is the related QueryDSL plug-in we added. The plug-in will automatically scan the entity class configured with @Entity in the project, and automatically create the Q[entity class name] through JPAAnnotationProcessor according to the fields defined in the entity and the associated class. Query the entity. After the creation is completed, the entity will be stored in the directory where we configure the outputDirectory property.

Summarize

So far, the content of this chapter has been explained. The main content of this chapter is how to configure the Maven environment of QueryDSL under the SpringBoot architecture. This series of projects is equivalent to the extension of the SpringBoot series of articles , and the later articles are all based on the SpringBoot framework.
The code of this chapter has been uploaded to the code cloud:
SpringBoot supporting source code address: https://gitee.com/hengboy/spring-boot-chapter
SpringCloud supporting source code address: https://gitee.com/hengboy/spring-cloud-chapter
SpringBoot related For the series of articles, please visit: Catalog: SpringBoot Learning Catalogue
QueryDSL related series of articles, please visit: QueryDSL General Query Framework Learning Catalog
Spring DataJPA-related series of articles, please visit: Catalog: Spring DataJPA Learning Catalog
Thank you for reading!
Welcome to join the QQ technical exchange group and make progress together.
QQ technical exchange group

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324736225&siteId=291194637