Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

72 строки
2.3KB

  1. package com.example.webapi.service;
  2. import com.example.webapi.dto.SupplyVarietyQueryDTO;
  3. import com.example.webapi.entity.SupplyVariety;
  4. import com.example.webapi.repository.SupplyVarietyRepository;
  5. import com.example.webapi.util.PageUtil;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.data.domain.Example;
  8. import org.springframework.data.domain.ExampleMatcher;
  9. import org.springframework.data.domain.Page;
  10. import org.springframework.data.domain.Pageable;
  11. import org.springframework.stereotype.Service;
  12. import java.util.Optional;
  13. @Service
  14. public class SupplyVarietyService {
  15. @Autowired
  16. private SupplyVarietyRepository repository;
  17. public Page<SupplyVariety> findAll(Pageable pageable) {
  18. return repository.findAll(pageable);
  19. }
  20. /**
  21. * 根据条件查询供应标识品种信息(分页)
  22. *
  23. * @param queryDTO 查询条件(包含分页参数)
  24. * @return 分页结果
  25. */
  26. public Page<SupplyVariety> findByConditions(SupplyVarietyQueryDTO queryDTO) {
  27. // 从QueryDTO中提取分页参数
  28. Pageable pageable = PageUtil.createPageable(queryDTO);
  29. // 创建实体对象作为查询条件
  30. SupplyVariety probe = new SupplyVariety();
  31. // 设置查询条件(只设置非空值)
  32. if (queryDTO.getSupplyId() != null) {
  33. probe.setSupplyId(queryDTO.getSupplyId());
  34. }
  35. if (queryDTO.getGood() != null) {
  36. probe.setGood(queryDTO.getGood());
  37. }
  38. if (queryDTO.getActive() != null) {
  39. probe.setActive(queryDTO.getActive());
  40. }
  41. // 创建ExampleMatcher,配置精确匹配
  42. ExampleMatcher matcher = ExampleMatcher.matching()
  43. .withIgnoreNullValues(); // 忽略空值
  44. // 创建Example对象
  45. Example<SupplyVariety> example = Example.of(probe, matcher);
  46. // 执行查询
  47. return repository.findAll(example, pageable);
  48. }
  49. public Optional<SupplyVariety> findById(Integer id) {
  50. return repository.findById(id);
  51. }
  52. public SupplyVariety save(SupplyVariety entity) {
  53. return repository.save(entity);
  54. }
  55. public void deleteById(Integer id) {
  56. repository.deleteById(id);
  57. }
  58. }