前提・実現したいこと
SpringBootを利用してwebアプリケーションを作成しています。
部分一致検索機能を実装しようとしているのですが、試しに完全に一致するテキストで検索をかけてみても、「一致するものがない」という結果になってしまいます。
どこか修正すべき箇所がありましたら、ご指摘いただけますと助かります。
また、検索結果が複数ある場合は降順で表示させたいのですが、どうすれば良いのでしょうか。
現在@Queryの部分にorder by descを書いていますが、一度全件表示させた時に(その時は空で検索しても全件出していました)順番がランダムになってしまいました。
該当のソースコード
repository
1public interface TodoRepository extends JpaRepository <Todo, Long> { 2 3 @Query("FROM Todo t WHERE t.text like '%text%' order by text desc ") 4 List<Todo> findAllByLikeText(@Param("text") String text); 5 6}
Service
1@Service 2public class TodoServiceImpl{ 3 4 @Autowired 5 TodoRepository todoRepository; 6 7 public List<Todo> findAllByLikeText(String text){ 8 List<Todo> result = todoRepository.findAllByLikeText(text); 9 return result; 10 } 11}
Controller
1(一部) 2@Controller 3public class TodoController { 4 5 @RequestMapping(method = RequestMethod.POST) 6 public ModelAndView findAllByLikeText(ModelAndView mav 7 , @RequestParam(defaultValue = "") String text) { 8 mav.setViewName("/todo/find"); 9 mav.addObject("text", text); 10 List<Todo> result = todoServiceImpl.findAllByLikeText(text); 11 mav.addObject("result", result); 12 mav.addObject("resultSize", result.size()); 13 return mav; 14 } 15 16}
補足
データベースのテーブル名は「todo」
textというカラムを検索しようとしています。
使用ツールなど
SpringBoot(gradle)
Mysql@5.7
IntelliJ IDEA
回答1件
あなたの回答
tips
プレビュー