|
|
|
@ -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<SongInfo> fuzzySearch(String keyword, PageRequest pageRequest) {
|
|
|
|
|
Specification<SongInfo> specification = fuzzySpecification(keyword);
|
|
|
|
|
return songInfoDao.findAll(specification,pageRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Specification<SongInfo> fuzzySpecification(String keyword) {
|
|
|
|
|
return new Specification<SongInfo>() {
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
@Override
|
|
|
|
|
public Predicate toPredicate(Root<SongInfo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
|
|
|
|
|
List<Predicate> predicateList = new ArrayList<Predicate>();
|
|
|
|
|
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()]));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|