1. 环境(理论上不限)
- IDEA 2019.3.5
- springboot 2.3.0
- gradle 6.3
- querydsl 4.2.1
- JDK 1.8
2. 核心配置(build.gradle )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | buildscript { ext { queryDslVersion = '4.2.1' lombokVersion = '1.18.12' } repositories { maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} } } dependencies { // QueryDSL implementation("com.querydsl:querydsl-core:${queryDslVersion}") implementation("com.querydsl:querydsl-jpa:${queryDslVersion}") //关键地方(记得开启annotationProcessor) annotationProcessor("com.querydsl:querydsl-apt:${queryDslVersion}:jpa", "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final", "javax.annotation:javax.annotation-api:1.3.2", "org.projectlombok:lombok") // Lombok(记得安装IDEA插件) compileOnly "org.projectlombok:lombok:${lombokVersion}" annotationProcessor "org.projectlombok:lombok:${lombokVersion}" implementation("org.projectlombok:lombok:${lombokVersion}") } |
至此,配置已经完成,IDEA已经可以正确生成Q文件,且编译时自动引用,不用像其他的教程还要配置七七八八的src source目录等
看到这,如果你懂,那就应该解决了,下面是demo工程搭建:
- 新建spring boot gradle项目(记得选JPA)

- 设置中开启annotationProcessor和安装lombok的IDEA 插件

- 配置JPAQueryFactory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.example.querydsldemo; import com.querydsl.jpa.impl.JPAQueryFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import javax.persistence.EntityManager; @SpringBootApplication public class QuerydsldemoApplication { public static void main(String[] args) { SpringApplication.run(QuerydsldemoApplication.class, args); } //让Spring管理JPAQueryFactory @Bean public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) { return new JPAQueryFactory(entityManager); } } |
- 编写Entity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.example.querydsldemo.entity; import lombok.Data; import org.hibernate.annotations.GenericGenerator; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Data @Entity public class UserEntity { @Id @GeneratedValue(generator = "idGenerator") @GenericGenerator(name = "idGenerator", strategy = "native") private Long id; private String userName; } |
build一下项目,build目录已经正确生成Q文件

5. 编写Service
1 2 3 4 5 6 7 8 9 10 11 12 | package com.example.querydsldemo.service; import com.example.querydsldemo.entity.UserEntity; public interface UserService { UserEntity save(UserEntity userEntity); UserEntity findByUserName(String userName); void deleteById(Long id); } |
- 编写Repository
1 2 3 4 5 6 7 8 9 | package com.example.querydsldemo.service.repository; import com.example.querydsldemo.entity.UserEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.querydsl.QuerydslPredicateExecutor; public interface UserRepository extends JpaRepository<UserEntity,Long>, QuerydslPredicateExecutor<UserEntity> { } |
- 编写ServiceImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.example.querydsldemo.service.impl; import com.example.querydsldemo.entity.QUserEntity; import com.example.querydsldemo.entity.UserEntity; import com.example.querydsldemo.service.UserService; import com.example.querydsldemo.service.repository.UserRepository; import com.querydsl.jpa.impl.JPAQueryFactory; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { private final JPAQueryFactory jpaQueryFactory; private final UserRepository userRepository; public UserServiceImpl(JPAQueryFactory jpaQueryFactory, UserRepository userRepository) { this.jpaQueryFactory = jpaQueryFactory; this.userRepository = userRepository; } @Override public UserEntity save(UserEntity userEntity) { return userRepository.saveAndFlush(userEntity); } @Override public UserEntity findByUserName(String userName) { QUserEntity qUserEntity = QUserEntity.userEntity; return jpaQueryFactory.select(qUserEntity).from(qUserEntity).where(qUserEntity.userName.eq(userName)).fetchFirst(); } @Override public void deleteById(Long id) { userRepository.deleteById(id); } } |
- 测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.example.querydsldemo; import com.example.querydsldemo.entity.UserEntity; import com.example.querydsldemo.service.UserService; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Before; import org.assertj.core.api.Assert; import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class QuerydsldemoApplicationTests { @Autowired private UserService userService; @Test void contextLoads() { UserEntity userEntity = new UserEntity(); userEntity.setUserName("张三"); UserEntity saveEntity = userService.save(userEntity); UserEntity queryEntity = userService.findByUserName(saveEntity.getUserName()); assert queryEntity != null; userService.deleteById(queryEntity.getId()); queryEntity = userService.findByUserName(saveEntity.getUserName()); assert queryEntity == null; } } |
完成!
demo地址:https://gitee.com/megoc/SpringBoot-Gradle-JAP-QueryDSL-Demo

