QueryDSL-在springboot中使用querydsl

pom.xml添加依赖

<dependencies>
 
        <dependency>
            <groupId>com.querydslgroupId>
            <artifactId>querydsl-jpaartifactId>
            <version>4.2.1version>
        dependency>
        <dependency>
            <groupId>com.querydslgroupId>
            <artifactId>querydsl-sqlartifactId>
            <version>4.2.1version>
        dependency>
        <dependency>
            <groupId>com.querydslgroupId>
            <artifactId>querydsl-aptartifactId>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>com.querydslgroupId>
            <artifactId>querydsl-sql-springartifactId>
            <version>4.2.1version>
        dependency>
        <dependency>
            <groupId>com.querydslgroupId>
            <artifactId>querydsl-sql-codegenartifactId>
            <version>4.2.1version>
            <scope>providedscope>
        dependency>
        
dependencies>
<build>
        <finalName>${project.artifactId}finalName>
            <plugins>
                
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
                
                <plugin>
                    <groupId>com.mysema.mavengroupId>
                    <artifactId>apt-maven-pluginartifactId>
                    <version>1.1.3version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>processgoal>
                            goals>
                            <configuration>
                                <outputDirectory>target/generated-sources/javaoutputDirectory>
                                <processor>com.querydsl.apt.jpa.JPAAnnotationProcessorprocessor>
                            configuration>
                        execution>
                    executions>
                plugin>
            plugins>

    build>

通过maven插件获得相应的查询类(Q开头)

idea右侧栏的maven工具点击complie
QueryDSL-在springboot中使用querydsl_第1张图片
在target文件夹下
QueryDSL-在springboot中使用querydsl_第2张图片

使用querydsl动态查询


@Service
public class ItemSearchService {

    @Autowired
    @PersistenceContext
    private EntityManager entityManager;

    //查询工厂实体
    private JPAQueryFactory queryFactory;

    //实例化控制器完成后执行该方法实例化JPAQueryFactory
    @PostConstruct
    public void initFactory(){
        queryFactory =  new JPAQueryFactory(entityManager);
    }
    //查询实体
    QItemEntity QItem = QItemEntity.itemEntity;
    QTypeEntity QType = QTypeEntity.typeEntity;
    QItemTypeEntity QItemType = QItemTypeEntity.itemTypeEntity;


    /**
     * 商品查询中的获取商品列表
     * @param searchForm
     * @return
     */
    public Map searchByItemForm(ItemSearchForm searchForm, Integer pageNumber, Integer pageSize){

        //基本谓语查询条件
        Predicate predicate = QItem.createTime.loe(new Date());
        //时间
        if(searchForm.getStartTime()!=null&&searchForm.getEndTime()!=null){
            predicate = ExpressionUtils.and(predicate,QItem.createTime.between(searchForm.getStartTime(),searchForm.getEndTime()));
        }
        //品牌
        if (StringUtils.isNotBlank(searchForm.getBrand())&&StringUtils.isNotEmpty(searchForm.getBrand())){
            predicate = ExpressionUtils.and(predicate,QItem.brand.eq(searchForm.getBrand()));
        }
        //商品名称
        if (StringUtils.isNotBlank(searchForm.getItemName())&&StringUtils.isNotEmpty(searchForm.getItemName())){
            predicate  = ExpressionUtils.and(predicate,QItem.itemName.like("%"+searchForm.getItemName()+"%"));
        }
        //分类
        if (StringUtils.isNotEmpty(searchForm.getClassName())&&StringUtils.isNotBlank(searchForm.getClassName())){
            predicate = ExpressionUtils.and(predicate,QType.className.eq(searchForm.getClassName()));
        }

        //返回的map
        List<ItemDTO> dtos = queryFactory.select(Projections.bean(ItemDTO.class,QItem.id,QItem.itemName,QItem.price,QType.className,QItem.brand,QItem.isSaled,QItem.createTime))
                                .distinct()
                                .from(QItemType)
                                .leftJoin(QItem).on(QItemType.itemId.eq(QItem.id))
                                .leftJoin(QType).on(QItemType.typeId.eq(QType.id))
                                .where(predicate)
                                .orderBy(QItem.createTime.desc())
                                .offset(pageNumber)
                                .limit(pageSize)
                                .fetch();
        Long count = queryFactory.select(Projections.bean(ItemDTO.class,QItem.id,QItem.itemName,QItem.price,QType.className,QItem.brand,QItem.isSaled,QItem.createTime))
                                    .distinct()
                                    .from(QItemType)
                                    .leftJoin(QItem).on(QItemType.itemId.eq(QItem.id))
                                    .leftJoin(QType).on(QItemType.typeId.eq(QType.id))
                                    .where(predicate)
                                    .orderBy(QItem.createTime.desc())
                                    .fetchCount();
        Map map = returnMap(dtos,count);
        return map;

    }

你可能感兴趣的:(springboot,querydsl)