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