[Java] Regarding application creation with springboot

1 minute read

We are now creating an app with the following features. ・Data input and registration ・List display of registration data ・Delete registered data Ambiguous search of registered data

Of the above, only the fuzzy search has not yet been completed due to errors.

Can you tell me where and how to fix the error and get the search function working? ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆ The database ▽ sp → Variable to put the tourist spot (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 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;

It’s a sequel.

//Top

@RequestMapping(“/”)

public String index() {

return “index”;

}

It’s a sequel.

// input

@RequestMapping(“/write”)

public ModelAndView write(@ModelAttribute(“formModel”) MyData mydata,ModelAndView mv) {

mv.setViewName(“write”);

return mv;

}

It’s a sequel.

@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:/”);

}

It’s a sequel.

// list

@RequestMapping(value=”/list”,method=RequestMethod.GET)

public ModelAndView list(ModelAndView mv) {

mv.setViewName(“list”);

List list = repository.findAll();

mv.addObject(“datalist”, list);

return mv;

}

It’s a sequel.

// details

@RequestMapping(value=”/data/{id}”,method=RequestMethod.GET)

public ModelAndView data(@ModelAttribute MyData mydata, @PathVariable int id, ModelAndView mv) {

mv.setViewName(“data”);

Optional data = repository.findById(id);

mv.addObject(“formModel”, data.get());

return mv;

}

It’s a sequel.

//Delete

@RequestMapping(value=”/delete/{id}”,method=RequestMethod.GET)

public ModelAndView delete(@PathVariable int id, ModelAndView mv) {

mv.setViewName(“delete”);

Optional 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:/”);

}

It’s a sequel.

// search

@RequestMapping(value=”/search”)

public ModelAndView search(@RequestParam(“sp”) String sp,ModelAndView mv) {

List list = repository.findBySpLike("%" + sp + "%");

mv.setViewName(“list”);

mv.addObject(“dataList”, list);

return mv;

}

}

◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆

<!DOCTYPE html>

Top page

Travel app





◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆

<–search.html–>

<!DOCTYPE html>

Search screen
Tourist destinations: