From aa1fe8082f6dbb34f0c29904c8352e0ed4119675 Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 6 Feb 2024 17:21:16 +0800 Subject: [PATCH] 1.implement fuzzy search --- .../luoo/music/service/JournalService.java | 20 ++++++++++++++++--- .../music/service/JournalSongService.java | 20 +++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java index eeda372..0294624 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java @@ -278,10 +278,24 @@ public class JournalService { public List fuzzySearch(String keyword) { - // TODO Auto-generated method stub - return Collections.emptyList(); + Specification specification = fuzzySpecification(keyword); + return journalDao.findAll(specification); + } + private Specification fuzzySpecification(String keyword) { + return new Specification() { + private static final long serialVersionUID = 1L; + @Override + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { + List predicateList = new ArrayList(); + + predicateList.add(criteriaBuilder.like(root.get("journalNo").as(String.class),"%"+keyword+"%")); + predicateList.add(criteriaBuilder.like(root.get("title").as(String.class),"%"+keyword+"%")); + + query.orderBy(criteriaBuilder.desc(root.get("journalNo").as(Integer.class))); + return criteriaBuilder.or( predicateList.toArray(new Predicate[predicateList.size()])); + } + }; } - public boolean isLatest10ByJournalId(String journalId) { return null!=journalDao.isLatest10ByJournalId(journalId); diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java index ba297cd..4afc444 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java @@ -1,6 +1,7 @@ package com.luoo.music.service; import com.luoo.music.dao.JournalSongDao; +import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.JournalSong; import org.springframework.beans.factory.annotation.Autowired; @@ -155,7 +156,22 @@ public class JournalSongService { } public List fuzzySearch(String keyword) { - // TODO Auto-generated method stub - return Collections.emptyList(); + Specification specification = fuzzySpecification(keyword); + return journalSongDao.findAll(specification); + } + private Specification fuzzySpecification(String keyword) { + return new Specification() { + private static final long serialVersionUID = 1L; + @Override + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { + List predicateList = new ArrayList(); + predicateList.add(criteriaBuilder.like(root.get("name").as(String.class),"%"+keyword+"%")); + predicateList.add(criteriaBuilder.like(root.get("artist").as(String.class),"%"+keyword+"%")); + predicateList.add(criteriaBuilder.like(root.get("album").as(String.class),"%"+keyword+"%")); + predicateList.add(criteriaBuilder.like(root.get("journalNo").as(String.class),"%"+keyword+"%")); + query.orderBy(criteriaBuilder.asc(root.get("name"))); + return criteriaBuilder.or(predicateList.toArray(new Predicate[predicateList.size()])); + } + }; } }