spring-data-jpa querydsl使用

1.添加依赖

<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<scope>provided</scope>
</dependency>

2.添加插件,可以自动生成表
<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>

3.创建实体,添加entity注解。
@Entity
public class Person implements Serializable {
@Id
private Long id;
private String name;
private Integer age;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}
}

4.创建查询接口。
public interface PersonRepository extends QuerydslPredicateExecutor<Person> {
}
5.使用 mvn compile 后自动生成QPerson,生成后可以将类复制到需要的地方。
6.也可以使用

@Autowired
//@PersistenceContext
private EntityManager entityManager;
 @PostConstruct
// public void init() {
// queryFactory = new JPAQueryFactory(entityManager);
// }


posted @ 2018-05-20 13:59  chenhonggao  阅读(916)  评论(0)    收藏  举报