Spring Data JPA 整合QueryDSL

QueryDSL是什么?

QueryDSL 是一个用于构建类型安全的 SQL 查询的 Java 库。它的主要目标是简化在 Java 中构建和执行 SQL 查询的过程,同时提供类型安全性和更好的编码体验。
QueryDSL 可以与许多关系型数据库一起使用,如 MySQL、PostgreSQL、Oracle 等。
QueryDSL 提供了一种以编程方式构建查询的方式,它使用了 Fluent API 风格的链式调用,使得查询语句更易读、易写,并且能够在编译时捕获一些常见的错误。它还提供了代码生成工具,可以生成查询实体、属性和查询类型的元数据,从而增强了代码的类型安全性。

  1. 类型安全性:通过使用 Java 编程语言,QueryDSL 提供了编译时的类型安全性,避免了运行时错误。
  2. Fluent API:QueryDSL 的 API 设计使得查询可以使用链式调用,提高了代码的可读性。
  3. 支持多种查询类型:QueryDSL 支持查询、更新和删除操作,并且提供了丰富的表达式操作符和函数。
  4. 支持关联查询:可以在查询中轻松地处理实体之间的关联关系,包括一对一、一对多、多对一和多对多等。
  5. 支持 JPA 和 SQL:QueryDSL 可以与 JPA(Java Persistence API)和 SQL 一起使用,适用于不同的持久化框架和数据库。
  6. 代码生成工具:QueryDSL 提供了代码生成工具,可以生成查询实体和属性的元数据,帮助提高代码的可维护性。

maven配置

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

 <querydsl.version>5.0.0</querydsl.version>



<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- query dsl build Q Entity -->
            <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>
        </plugins>
    </build>

需执行 maven clean install

配置类

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Wang
 */
@Configuration
public class JpaQueryConfig {
    @Bean
    public JPAQueryFactory jpaQuery(EntityManager entityManager) {
        return new JPAQueryFactory(entityManager);
    }
}

使用案例


import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.woodare.cdw.core.Cons;
import com.woodare.cdw.jpa.data.SecondaryInvitingData;
import com.woodare.cdw.jpa.entity.QAccountEntity;
import com.woodare.cdw.jpa.entity.QBoatEntity;
import com.woodare.cdw.jpa.entity.QSecondaryInvitingEntity;
import com.woodare.cdw.jpa.entity.SecondaryInvitingEntity;
import com.woodare.cdw.jpa.enumdata.DeleteStateEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Wang
 */
@RequiredArgsConstructor
@Service
public class SecondaryInvitingDsl {

    final JPAQueryFactory queryFactory;

    public List<SecondaryInvitingData> findSecondaryInvitingByBoatId(String boatId) {
        QSecondaryInvitingEntity secondaryInvitingEntity = QSecondaryInvitingEntity.secondaryInvitingEntity;
        QAccountEntity accountEntity = QAccountEntity.accountEntity;
        return queryFactory.select(
                        Projections.bean(SecondaryInvitingData.class,
                                secondaryInvitingEntity.id,
                                secondaryInvitingEntity.accountId,
                                secondaryInvitingEntity.secondaryAccountId,
                                secondaryInvitingEntity.status,
                                secondaryInvitingEntity.inviteDate,
                                secondaryInvitingEntity.respondDate,
                                secondaryInvitingEntity.deleteDate,
                                secondaryInvitingEntity.permissions,
                                secondaryInvitingEntity.secondaryFirstName,
                                secondaryInvitingEntity.secondaryLastName,
                                secondaryInvitingEntity.secondaryEmail,
                                secondaryInvitingEntity.deleteBy,
                                accountEntity.avatar
                        ))
                .from(secondaryInvitingEntity)
                .leftJoin(accountEntity)
                .on(secondaryInvitingEntity.secondaryAccountId.eq(accountEntity.id))
                .where(
                        secondaryInvitingEntity.boatId.eq(boatId),
                        secondaryInvitingEntity.deleteState.eq(DeleteStateEnum.VALID.getValue())
                )
                .orderBy(secondaryInvitingEntity.createDate.desc())
                .fetch();
    }

    /**
     * CN 根据被邀人邮箱查询邀请记录, 并关联boatName
     *
     * @param secondaryEmail secondaryEmail
     * @param status         status
     * @return List<SecondaryInvitingData>
     */
    public List<SecondaryInvitingData> findListBySecondaryUser(String secondaryEmail, String status) {
        QSecondaryInvitingEntity secondaryInvitingEntity = QSecondaryInvitingEntity.secondaryInvitingEntity;
        QBoatEntity boatEntity = QBoatEntity.boatEntity;
        return queryFactory.select(
                        Projections.bean(SecondaryInvitingData.class,
                                secondaryInvitingEntity.id,
                                secondaryInvitingEntity.accountId,
                                secondaryInvitingEntity.secondaryAccountId,
                                secondaryInvitingEntity.status,
                                secondaryInvitingEntity.inviteDate,
                                secondaryInvitingEntity.respondDate,
                                secondaryInvitingEntity.deleteDate,
                                secondaryInvitingEntity.permissions,
                                secondaryInvitingEntity.secondaryFirstName,
                                secondaryInvitingEntity.secondaryLastName,
                                secondaryInvitingEntity.secondaryEmail,
                                secondaryInvitingEntity.deleteBy,
                                boatEntity.boatName
                        ))
                .from(secondaryInvitingEntity)
                .leftJoin(boatEntity)
                .on(secondaryInvitingEntity.boatId.eq(boatEntity.id))
                .where(
                        secondaryInvitingEntity.secondaryEmail.eq(secondaryEmail),
                        secondaryInvitingEntity.status.eq(status),
                        secondaryInvitingEntity.deleteState.eq(DeleteStateEnum.VALID.getValue()),
                        boatEntity.deleteState.eq(DeleteStateEnum.VALID.getValue())
                )
                .orderBy(secondaryInvitingEntity.createDate.desc())
                .fetch();
    }


    /**
     * CN 根据被邀人邮箱, 主邀人用户id查询邀请记录, 排除expire状态
     *
     * @param secondaryEmail secondaryEmail
     * @param accountId         accountId
     * @return List<SecondaryInvitingData>
     */
    public List<SecondaryInvitingEntity> findListBySecondaryEmailAndAccountId(String secondaryEmail, String accountId) {
        QSecondaryInvitingEntity secondaryInvitingEntity = QSecondaryInvitingEntity.secondaryInvitingEntity;
        return queryFactory.select(secondaryInvitingEntity)
                .from(secondaryInvitingEntity)
                .where(
                        secondaryInvitingEntity.secondaryEmail.eq(secondaryEmail),
                        secondaryInvitingEntity.accountId.eq(accountId),
                        secondaryInvitingEntity.status.ne(Cons.InvitingStatus.EXPIRED)
                )
                .orderBy(secondaryInvitingEntity.createDate.desc())
                .fetch();
    }
}


import cn.hutool.core.util.StrUtil;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.woodare.cdw.jpa.entity.QSensorSortEntity;
import com.woodare.cdw.jpa.entity.SensorSortEntity;
import com.woodare.cdw.jpa.enumdata.DeleteStateEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Wang
 */
@RequiredArgsConstructor
@Service
public class SensorSortDsl {

    final JPAQueryFactory queryFactory;

    public List<SensorSortEntity> findSensorSortList(String sirenDeviceId) {
        QSensorSortEntity sensorSortEntity = QSensorSortEntity.sensorSortEntity;
        return queryFactory.selectFrom(sensorSortEntity)
                .where(
                        sensorSortEntity.sirenDeviceId.eq(sirenDeviceId)
                )
                .orderBy(sensorSortEntity.parentType.asc(), sensorSortEntity.childrenType.desc())
                .fetch();
    }


    public void deleteByParam(String sirenDeviceId, String parentType, String childrenType) {
        QSensorSortEntity sensorSortEntity = QSensorSortEntity.sensorSortEntity;
        BooleanBuilder condition = new BooleanBuilder();
        if (StrUtil.isNotBlank(sirenDeviceId)) {
            condition.and(sensorSortEntity.sirenDeviceId.eq(sirenDeviceId));
        }
        if (StrUtil.isNotBlank(parentType)) {
            condition.and(sensorSortEntity.parentType.eq(parentType));
        }
        if (StrUtil.isNotBlank(childrenType)) {
            condition.and(sensorSortEntity.childrenType.eq(childrenType));
        }
        queryFactory.update(sensorSortEntity)
                .set(sensorSortEntity.deleteState, DeleteStateEnum.DELETE.getValue())
                .where(condition)
                .execute();
    }
}

你可能感兴趣的:(Spring,Data,JPA,QueryDSL)