How do I configure (XML) QueryDSL to be used with

2019-08-06 02:05发布

问题:

Quick reference for those looking to configure Spring MVC, Spring Data, and QueryDSL using XML config.

回答1:

In pom.xml:

<properties>
    <querydsl.version>3.6.7</querydsl.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.9.0.RELEASE</version>
    </dependency>

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

    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>${querydsl.version}</version>
    </dependency>

</dependencies>

<build>

    <plugins>

        <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.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

In mvc-dispatcher-servlet.xml:

<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService"/>

<mvc:annotation-driven conversion-service="conversionService">
  <mvc:argument-resolvers>
    <bean class="org.springframework.data.web.querydsl.QuerydslPredicateArgumentResolver">
      <constructor-arg>
        <bean class="org.springframework.data.querydsl.binding.QuerydslBindingsFactory">
          <constructor-arg>
            <value type="org.springframework.data.querydsl.SimpleEntityPathResolver">INSTANCE</value>
          </constructor-arg>
        </bean>
      </constructor-arg>
      <constructor-arg ref="conversionService"/>
    </bean>
  </mvc:argument-resolvers>
</mvc:annotation-driven>

In FooRepository.java:

public interface FooRepository extends JpaRepository<Foo, Long>, QueryDslPredicateExecutor<Foo> {

}

In FooController.java:

@RequestMapping("/foo")
String getPage(
  @QuerydslPredicate(root = Foo.class) Predicate predicate, 
  Pageable pageable, 
  Model model
) {

    Page<Foo> page = fooRepository.findAll(predicate, pageable);

    model.addAttribute("page", page);

    return "foo/index";
}

Then you can query your repository like this:

GET /foo?bar=baz

Assuming bar is a property of foo