java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Data JPA 整合QueryDSL

Spring Data JPA 整合QueryDSL的使用案例

作者:宁漂打工仔

QueryDSL 是一个用于构建类型安全的 SQL 查询的 Java 库,它的主要目标是简化在 Java 中构建和执行 SQL 查询的过程,同时提供类型安全性和更好的编码体验,对Spring Data JPA 整合QueryDSL使用案例感兴趣的朋友跟随小编一起看看吧

QueryDSL是什么?

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

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的文章就介绍到这了,更多相关Spring Data JPA 整合QueryDSL内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文