欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring boot JPA+Gradle+QueryDSL 完美配置生成Q文件依赖

程序员文章站 2022-04-15 14:44:33
...

1. 环境(理论上不限)

  1. IDEA 2019.3.5
  2. springboot 2.3.0
  3. gradle 6.3
  4. querydsl 4.2.1
  5. JDK 1.8

2. 核心配置(build.gradle )

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工程搭建:

  1. 新建spring boot gradle项目(记得选JPA)
    Spring boot JPA+Gradle+QueryDSL 完美配置生成Q文件依赖
  2. 设置中开启annotationProcessor和安装lombok的IDEA 插件
    Spring boot JPA+Gradle+QueryDSL 完美配置生成Q文件依赖
  3. 配置JPAQueryFactory
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);
   }
}

  1. 编写Entity
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文件
Spring boot JPA+Gradle+QueryDSL 完美配置生成Q文件依赖
5. 编写Service

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);
}

  1. 编写Repository
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> {

}

  1. 编写ServiceImpl
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. 测试
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