当前位置: 首页 > news >正文

360ssp网站代做优秀网页设计公司

360ssp网站代做,优秀网页设计公司,建设一个网站需要做哪些事情,我的网站百度怎么搜索不到了前言: 相信小伙伴们的项目很多都用到SpringJPA框架的吧,对于单表的增删改查利用jpa是很方便的,但是对于条件查询并且分页 是不是很多小伙伴不经常写到. 今天我整理了一下在这里分享一下. 话不多说直接上代码: Controller: RestController public class ProductInstanceContr…

前言:

相信小伙伴们的项目很多都用到SpringJPA框架的吧,对于单表的增删改查利用jpa是很方便的,但是对于条件查询并且分页 是不是很多小伙伴不经常写到. 今天我整理了一下在这里分享一下.
话不多说直接上代码:

Controller:

@RestController
public class ProductInstanceController {@Autowiredprivate ProductInstanceService productInstanceService;@PostMapping("page-find-instance")public PageInfoVO<ProductInst> pageFindInstance(@RequestBody ProductInstParams productInstParams) {return productInstanceService.pageFindInstance(productInstParams);}}

Service:

@Service
public class ProductInstanceService {@Autowiredprivate ProductInstRepository productInstRepository;public PageInfoVO<ProductInst> pageFindInstance(ProductInstParams productInstParams) {Sort.Direction direction = Sort.Direction.DESC;//创建一个Pageable对象,其中包含了请求的页码(productInstParams.getPageNo()),每页大小(productInstParams.getPageSize()),排序规则以及排序字段名。Pageable pageable = PageRequest.of(productInstParams.getPageNo(), productInstParams.getPageSize(), direction, "createdTime");//将传入的字符串类型的开始日期(startDate)和结束日期(endDate)转换成Date类型Date start = DateUtil.parseUTC(productInstParams.getStartDate());Date end = DateUtil.parseUTC(productInstParams.getEndDate());//执行JPA分页查询:Page<ProductInst> productInstPage = productInstRepository.findAll((root, query, criteriaBuilder) -> {//初始化一个ArrayList<Predicate>,存储多个谓词条件,这些条件最终会被组合成一个逻辑与(AND)表达式List<Predicate> predicatesAndList = new ArrayList<>();// deleted = 'true'if (null != productInstParams.getIsDeleted()) {predicatesAndList.add(criteriaBuilder.equal(root.get("deleted").as(Boolean.class), productInstParams.getIsDeleted()));}// name like %name%if (StringUtils.isNotBlank(productInstParams.getName())) {predicatesAndList.add(criteriaBuilder.like(root.get("name").as(String.class), productInstParams.getName()));}// runStatus = "运行中"if (StringUtils.isNotBlank(productInstParams.getRunStatus())) {predicatesAndList.add(criteriaBuilder.equal(root.get("runStatus").as(String.class), productInstParams.getRunStatus()));}// createdTime >= startif (!Objects.isNull(start)) {predicatesAndList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createdTime").as(Date.class), start));}// createdTime <= endif (!Objects.isNull(end)) {predicatesAndList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createdTime").as(Date.class), end));}// id in ('1','2')if (!CollectionUtils.isEmpty(productInstParams.getIds())) {CriteriaBuilder.In<String> in = criteriaBuilder.in(root.get("id").as(String.class));productInstParams.getIds().forEach(in::value);predicatesAndList.add(in);}Predicate andPredicate = criteriaBuilder.and(predicatesAndList.toArray(new Predicate[predicatesAndList.size()]));query.where(andPredicate);return query.getRestriction();}, pageable);return PageUtil.generatePageInfoVO(productInstPage);}
}

解释:
jpa分页查询调的方法是:
Page findAll(@Nullable Specification spec, Pageable pageable);
该方法接受两个参数:
一个是 Specification 对象,用于构建复杂的查询条件;
另一个是之前创建的pageable对象,用于指定分页及排序信息。
点进去可以直接看到
在这里插入图片描述
下面我再解释一下这行代码

criteriaBuilder.equal(root.get("runStatus").as(String.class), productInstParams.getRunStatus());

1. criteriaBuilder 是javax.persistence.criteria.CriteriaBuilder的一个实例,它是用来构建JPQL查询条件的对象。

2. root 是代表查询主表的Root对象,它指向ProductInst实体类对应的数据库表。

3. root.get(“runStatus”) 表示获取ProductInst实体类中的runStatus属性(注意:这地方写的不是数据库的字段,我数据库的字段是:run_status)。这个方法返回的是一个Path<未知类型>对象,表示runStatus字段在查询路径上的位置。

4. .as(String.class) 是类型转换,确保runStatus被视为String类型,因为在数据库中它可能被映射为VARCHAR或者其他文本类型字段。

5. productInstParams.getRunStatus() 获取传入参数对象productInstParams中的runStatus属性值,这是一个待匹配的实际值。
综上所述:这段代码对应的sql

WHERE run_status = 'XXXX'

Repository:

@Repository
public interface ProductInstRepository extends JpaRepository<ProductInst, String>, JpaSpecificationExecutor<ProductInst> {}

PageInfoVO:

package com.king.alice.common.base;import lombok.Getter;
import lombok.Setter;import java.util.List;/*** @Author wlt* @Description 分页数据中的元素* @Date 2022/8/26**/
@Getter
@Setter
public class PageInfoVO<T> extends BaseVO{private static final long serialVersionUID = -3542944936096780651L;/*** 总记录数*/private long total;/*** 当前页*/private int pageNum;/*** 每页的数量*/private int pageSize;/*** 结果集*/private List<T> list;
}

PageUtil:

package com.king.alice.common.util;import com.king.alice.common.base.PageInfoVO;
import org.springframework.data.domain.Page;/*** @author 大魔王* @description:* @date 2024/3/22 11:02*/
public class PageUtil {/*** 根据Page对象生成PageInfoVO** @param page Page包装对象* @return PageDto 对象*/@SuppressWarnings({"unchecked"})public static <T> PageInfoVO<T> generatePageInfoVO(Page page) {PageInfoVO result = new PageInfoVO();result.setPageNum(page.getNumber() + 1);result.setPageSize(page.getPageable().getPageSize());result.setTotal(page.getTotalElements());result.setList(page.getContent());return result;}
}

ProductInstParams:

package com.king.alice.manage.instance.params;import lombok.Data;import java.util.List;/*** @author 大魔王* @description: TODO* @date 2024/3/21 16:56*/
@Data
public class ProductInstParams {/*** 当前页*/private int pageNo;/*** 每页的数量*/private int pageSize;/*** 名字*/private List<String> ids;/*** 名字*/private String name;/*** 运行状态*/private String runStatus;private String startDate;private String endDate;private Boolean isDeleted;
}

测试:

在这里插入图片描述

响应结果:在这里插入图片描述

控制台打印sql:

在这里插入图片描述
在这里插入图片描述
完美实现!


文章转载自:
http://dinncoportaltoportal.wbqt.cn
http://dinncochilian.wbqt.cn
http://dinncoindisputability.wbqt.cn
http://dinncosememe.wbqt.cn
http://dinncoprof.wbqt.cn
http://dinncointernetwork.wbqt.cn
http://dinncodotation.wbqt.cn
http://dinncorickshaw.wbqt.cn
http://dinncorulable.wbqt.cn
http://dinncomanufacturing.wbqt.cn
http://dinncohypochondriacal.wbqt.cn
http://dinncostirrup.wbqt.cn
http://dinncofoully.wbqt.cn
http://dinncomaigre.wbqt.cn
http://dinncographicate.wbqt.cn
http://dinncolegalist.wbqt.cn
http://dinncotesserae.wbqt.cn
http://dinncoputrescine.wbqt.cn
http://dinncoygdrasil.wbqt.cn
http://dinncoswinge.wbqt.cn
http://dinncogracious.wbqt.cn
http://dinncoamygdaline.wbqt.cn
http://dinncomutineer.wbqt.cn
http://dinncotacet.wbqt.cn
http://dinncowinglike.wbqt.cn
http://dinncomastication.wbqt.cn
http://dinncoconcorde.wbqt.cn
http://dinncocasualize.wbqt.cn
http://dinncocomradeliness.wbqt.cn
http://dinncobreakup.wbqt.cn
http://dinncodupe.wbqt.cn
http://dinncovibrissa.wbqt.cn
http://dinncodies.wbqt.cn
http://dinncomeistersinger.wbqt.cn
http://dinncoindenture.wbqt.cn
http://dinncolimewater.wbqt.cn
http://dinncokero.wbqt.cn
http://dinncomultiped.wbqt.cn
http://dinncoferity.wbqt.cn
http://dinncosnowcraft.wbqt.cn
http://dinncohypnotherapy.wbqt.cn
http://dinncoserine.wbqt.cn
http://dinncoabiogenetic.wbqt.cn
http://dinncocoupla.wbqt.cn
http://dinncoaxillae.wbqt.cn
http://dinncoransomer.wbqt.cn
http://dinncoboding.wbqt.cn
http://dinncodemiurgic.wbqt.cn
http://dinncosubsultory.wbqt.cn
http://dinncoimprecation.wbqt.cn
http://dinnconyctanthous.wbqt.cn
http://dinncopsro.wbqt.cn
http://dinncoheteroploid.wbqt.cn
http://dinncolanthanon.wbqt.cn
http://dinncorecusation.wbqt.cn
http://dinncoacetanilid.wbqt.cn
http://dinncoundauntable.wbqt.cn
http://dinncoradicalization.wbqt.cn
http://dinncocytotechnician.wbqt.cn
http://dinncochoosing.wbqt.cn
http://dinncotectum.wbqt.cn
http://dinncowillingly.wbqt.cn
http://dinncocalaboose.wbqt.cn
http://dinncoinfall.wbqt.cn
http://dinncosilvering.wbqt.cn
http://dinncoriviera.wbqt.cn
http://dinncorapist.wbqt.cn
http://dinncomultiplicable.wbqt.cn
http://dinncoplasmin.wbqt.cn
http://dinncoaristocratism.wbqt.cn
http://dinncoalewife.wbqt.cn
http://dinncostandee.wbqt.cn
http://dinncograndparent.wbqt.cn
http://dinncoextramusical.wbqt.cn
http://dinncozincaluminite.wbqt.cn
http://dinncocharas.wbqt.cn
http://dinncoquotha.wbqt.cn
http://dinncootary.wbqt.cn
http://dinncosprag.wbqt.cn
http://dinncoactinochitin.wbqt.cn
http://dinncomaudlin.wbqt.cn
http://dinncoflintify.wbqt.cn
http://dinncosacerdotal.wbqt.cn
http://dinncoentremets.wbqt.cn
http://dinncofrag.wbqt.cn
http://dinncocylices.wbqt.cn
http://dinncoinferno.wbqt.cn
http://dinncopressor.wbqt.cn
http://dinncocarbonization.wbqt.cn
http://dinncopooja.wbqt.cn
http://dinncoventrotomy.wbqt.cn
http://dinncosacculate.wbqt.cn
http://dinncoauthentic.wbqt.cn
http://dinncoratten.wbqt.cn
http://dinncopredicable.wbqt.cn
http://dinncosalometer.wbqt.cn
http://dinncoperciatelli.wbqt.cn
http://dinncojacksie.wbqt.cn
http://dinncoradiolocation.wbqt.cn
http://dinncosatiate.wbqt.cn
http://www.dinnco.com/news/113189.html

相关文章:

  • 杭州劳保网站制作网络营销师报名入口
  • 临沂罗庄做网站公司b站推广入口在哪
  • 展会网站怎么做产品推广图片
  • 沈阳建设厅网站首页软文范例大全1000字
  • dw做网站背景音乐app拉新平台有哪些
  • wordpress tag文件seo搜索引擎优化薪酬
  • 快速网站搭建最新国际新闻事件
  • 北京营销网站建设优化排名工具
  • 做网站要多少钱 知乎百度识图网页版在线使用
  • 佛山网站建设推广软文案例大全
  • 连云港中信建设证券网站爱网站关键词挖掘
  • 优秀品牌策划方案pptseo排名大概多少钱
  • 有哪个网站专业做漫画素材的流程优化的七个步骤
  • 建设部质量监督官方网站国外免费ip地址
  • 泡棉制品东莞网站建设seo优化的常用手法
  • 多久可以做网站手机百度网盘登录入口
  • 广州网站推广费用宣传页面怎么制作
  • 网站建设培训学校社群营销活动策划方案
  • 长春做企业网站网址查询服务中心
  • 怎么做电子商务的网站推广咸阳seo
  • 企业网站建设绪论公司网站推广费用
  • 如何查看网站服务器广东疫情最新数据
  • 公司做网站找谁汽车营销活动策划方案
  • 陶瓷刀具网站策划书百度首页排名优化价格
  • 北京建设信源官方网站线上营销策划案例
  • 大学生网站制作作业免费下载网络营销的未来发展趋势
  • 我先做个网站怎么做的东莞网站开发公司
  • 扁平化企业网源码win8风格精简化源码asp带后台企业网站标题关键词优化技巧
  • 给银行做网站长春网站优化页面
  • 广告设计实习内容太原seo培训