We are currently creating an app that has the following functions. ・ Data entry and registration ・ List display of registered data ・ Delete registration data ・ Ambiguous search of registered data
Of the above, only the ambiguous search has not yet been completed due to an error or the like.
Can you tell me where and how to fix it so that the error goes away and the search function works? ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆ Database sp → Variable to enter tourist spots (String type)
◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
code //MyDataRepository.java
package com.example.demo;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MyDataRepository extends JpaRepository<MyData, Integer> {
List<MyData> findBySpLike(String sp);
}
◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
//MyController.java
package com.example.demo;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@Autowired
MyDataRepository repository;
//トップ
@RequestMapping("/")
public String index() {
return "index";
}
//入力
@RequestMapping("/write")
public ModelAndView write(@ModelAttribute("formModel") MyData mydata,ModelAndView mv) {
mv.setViewName("write");
return mv;
}
@RequestMapping(value="/form", method=RequestMethod.POST)
@Transactional(readOnly=false)
public ModelAndView form(@ModelAttribute("formModel") MyData mydata, ModelAndView mv) {
repository.saveAndFlush(mydata);
return new ModelAndView("redirect:/");
}
//一覧
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView list(ModelAndView mv) {
mv.setViewName("list");
List<MyData> list = repository.findAll();
mv.addObject("datalist", list);
return mv;
}
//詳細
@RequestMapping(value="/data/{id}",method=RequestMethod.GET)
public ModelAndView data(@ModelAttribute MyData mydata, @PathVariable int id, ModelAndView mv) {
mv.setViewName("data");
Optional<MyData> data = repository.findById(id);
mv.addObject("formModel", data.get());
return mv;
}
//削除
@RequestMapping(value="/delete/{id}",method=RequestMethod.GET)
public ModelAndView delete(@PathVariable int id, ModelAndView mv) {
mv.setViewName("delete");
Optional<MyData> data = repository.findById(id);
mv.addObject("formModel", data.get());
return mv;
}
@RequestMapping(value="/delete",method=RequestMethod.POST)
@Transactional(readOnly = false)
public ModelAndView remove(@RequestParam int id) {
repository.deleteById(id);
return new ModelAndView("redirect:/");
}
//検索
@RequestMapping(value="/search")
public ModelAndView search(@RequestParam("sp") String sp,ModelAndView mv) {
List<MyData> list = repository.findBySpLike("%" + sp + "%");
mv.setViewName("list");
mv.addObject("dataList", list);
return mv;
}
}
◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
Travel app h2> th> tr> |
---|
◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
<--search.html-->
Recommended Posts