diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java index 64e2557..ea21ac4 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java @@ -23,10 +23,12 @@ import org.springframework.data.domain.PageRequest; import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.JournalSong; +import com.luoo.music.pojo.SongInfo; import com.luoo.music.pojo.Tag; import com.luoo.music.service.JournalService; import com.luoo.music.service.JournalSongService; import com.luoo.music.service.SearchService; +import com.luoo.music.service.SongInfoService; import com.luoo.music.service.TagService; import annotation.GlobalInterceptor; import annotation.VerifyParam; @@ -50,7 +52,7 @@ public class SearchController { private JournalService journalService; @Autowired - private JournalSongService journalSongService; + private SongInfoService songInfoService; @Autowired private SearchService searchService; @@ -76,7 +78,7 @@ public class SearchController { @VerifyParam(required = true) FuzzySearchReq query) throws InterruptedException, ExecutionException { PageRequest pageRequest = PageRequest.of(getPageNum(query.getPageNum()), getPageSize(query.getPageSize())); - Page pageResults=journalSongService.fuzzySearch(query.getKeyword(),pageRequest); + Page pageResults=songInfoService.fuzzySearch(query.getKeyword(),pageRequest); List list = pageResults.stream().map(SongMapper::getSongRespDTO) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); 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 730ff2e..96a8ee0 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 @@ -156,26 +156,4 @@ public class JournalSongService { public List orderByField(List objectIds) { return journalSongDao.orderByField(objectIds); } - - public Page fuzzySearch(String keyword, PageRequest pageRequest) { - Specification specification = fuzzySpecification(keyword); - return journalSongDao.findAll(specification,pageRequest); - } - - 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()])); - } - }; - } - } diff --git a/luoo_music/src/main/java/com/luoo/music/service/SongInfoService.java b/luoo_music/src/main/java/com/luoo/music/service/SongInfoService.java index c8511a4..9d82ef9 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/SongInfoService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/SongInfoService.java @@ -4,9 +4,19 @@ import com.luoo.music.dao.SongInfoDao; import com.luoo.music.pojo.SongInfo; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; + +import java.util.ArrayList; import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; + @Service public class SongInfoService { @@ -24,4 +34,24 @@ public class SongInfoService { public SongInfo findById(String id) { return songInfoDao.getSongById(id); } + + public Page fuzzySearch(String keyword, PageRequest pageRequest) { + Specification specification = fuzzySpecification(keyword); + return songInfoDao.findAll(specification,pageRequest); + } + + 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+"%")); + query.orderBy(criteriaBuilder.asc(root.get("name"))); + return criteriaBuilder.or(predicateList.toArray(new Predicate[predicateList.size()])); + } + }; + } }