Querydsl 使用和工具类

Querydsl 简化使用

1、在使用Querydsl时,需要定义EntityManager EntityManagerFactory 会存在许多重复性的代码,下面将使用进行归口,减少重复的引入操作

import com.blazebit.persistence.Criteria;
import com.blazebit.persistence.CriteriaBuilderFactory;
import com.blazebit.persistence.querydsl.BlazeJPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.ToLongFunction;

/**
 * 用于查询的类;只读
 * @version 1.0
 */
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class SearchService {

    @PersistenceContext
    private final EntityManager entityManager;
    @PersistenceUnit
    private final EntityManagerFactory entityManagerFactory;

    public <R> R search(BiFunction<EntityManager, EntityManagerFactory, R> searchFunction) {
        return searchFunction.apply(entityManager, entityManagerFactory);
    }

    public <R> R search(Function<JPAQueryFactory, R> searchFunction) {
        JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);
        return searchFunction.apply(queryFactory);
    }

    public <R> R searchBlaze(Function<BlazeJPAQuery<?>, R> searchFunction) {
        CriteriaBuilderFactory criteriaBuilderFactory = Criteria.getDefault().createCriteriaBuilderFactory(this.entityManagerFactory);
        BlazeJPAQuery<?> queryFactory = new BlazeJPAQuery<>(this.entityManager, criteriaBuilderFactory);
        return searchFunction.apply(queryFactory);
    }

    /**
     * 直接获取EntityManager 进行查询
     */
    public <R> R searchNative(Function<EntityManager, R> searchFunction) {
        return searchFunction.apply(entityManager);
    }

    /**
     * 用于执行更新和删除
     * @param	executeFunction 执行的内容
     * @return   java.lang.Long
     */
    @Transactional(rollbackFor = Exception.class)
    public Long execute(ToLongFunction<JPAQueryFactory> executeFunction) {
        JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);
        return executeFunction.applyAsLong(queryFactory);
    }
}

简单使用展示 QClass 和 BooleanBuilder 、Bean 的引入 不进行赘述

public Page<Model> findAll(QueryCommand command) {
        QModelEntity modelEntity = QModelEntity.modelEntity;
        BooleanBuilder builder = builderPredicate(command, modelEntity);
        Pageable pageable = command.getPageable();
        return searchService.search(query -> {
            List<Model> models = query.select(Projections.fields(Model.class,
                    modelEntity.id,
                    modelEntity.orgId,
                    modelEntity.relatedId,
                    modelEntity.type,
                    modelEntity.source,
                    modelEntity.createdAt,
                    modelEntity.createdBy,
                    modelEntity.updatedAt,
                    modelEntity.updatedBy,
                    modelEntity.config
            )).from(modelEntity).where(builder).orderBy(modelEntity.id.desc()).offset(pageable.getOffset()).limit(pageable.getPageSize()).fetch();

            int size = query.select(modelEntity.id).from(modelEntity).where(builder).fetch().size();

            return new PageImpl<>(declarationsModels, pageable, size);
        });
    }
posted @ 2023-11-21 16:32  代码红了一大片  阅读(325)  评论(0编辑  收藏  举报