本文主要是介绍QueryDSL-在springboot中使用querydsl,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
pom.xml添加依赖
<dependencies><!--QueryDsl start==============================--><dependency><groupId>com.querydsl</groupId><artifactId>querydsl-jpa</artifactId><version>4.2.1</version></dependency><dependency><groupId>com.querydsl</groupId><artifactId>querydsl-sql</artifactId><version>4.2.1</version></dependency><dependency><groupId>com.querydsl</groupId><artifactId>querydsl-apt</artifactId><scope>provided</scope></dependency><dependency><groupId>com.querydsl</groupId><artifactId>querydsl-sql-spring</artifactId><version>4.2.1</version></dependency><dependency><groupId>com.querydsl</groupId><artifactId>querydsl-sql-codegen</artifactId><version>4.2.1</version><scope>provided</scope></dependency><!--QueryDsl end================================-->
</dependencies>
<build><finalName>${project.artifactId}</finalName><plugins><!--springboot-maven插件--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><!--QueryDsl插件--><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插件获得相应的查询类(Q开头)
idea右侧栏的maven工具点击complie
在target文件夹下
使用querydsl动态查询
@Service
public class ItemSearchService {@Autowired@PersistenceContextprivate EntityManager entityManager;//查询工厂实体private JPAQueryFactory queryFactory;//实例化控制器完成后执行该方法实例化JPAQueryFactory@PostConstructpublic 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()));}//返回的mapList<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;}
这篇关于QueryDSL-在springboot中使用querydsl的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!