|
| 1 | +package com.isea533.mybatis.controller.demo; |
| 2 | + |
| 3 | +import com.github.pagehelper.PageInfo; |
| 4 | +import com.isea533.mybatis.model.Country; |
| 5 | +import com.isea533.mybatis.service.CountryService; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.stereotype.Controller; |
| 8 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 9 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 10 | +import org.springframework.web.bind.annotation.RequestParam; |
| 11 | +import org.springframework.web.servlet.ModelAndView; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author liuzh_3nofxnp |
| 17 | + * @since 2015-09-19 17:15 |
| 18 | + */ |
| 19 | +@Controller |
| 20 | +public class CountryController { |
| 21 | + |
| 22 | + @Autowired |
| 23 | + private CountryService countryService; |
| 24 | + |
| 25 | + private String page_list = "index"; |
| 26 | + |
| 27 | + private String redirect_list = "redirect:list"; |
| 28 | + |
| 29 | + @RequestMapping(value = {"list", "index", "index.html", ""}) |
| 30 | + public ModelAndView getList(Country country, |
| 31 | + @RequestParam(required = false, defaultValue = "1") int page, |
| 32 | + @RequestParam(required = false, defaultValue = "10") int rows) { |
| 33 | + ModelAndView result = new ModelAndView(page_list); |
| 34 | + List<Country> countryList = countryService.selectByCountry(country, page, rows); |
| 35 | + result.addObject("pageInfo", new PageInfo<Country>(countryList)); |
| 36 | + result.addObject("queryParam", country); |
| 37 | + result.addObject("page", page); |
| 38 | + result.addObject("rows", rows); |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + @RequestMapping(value = "view", method = RequestMethod.GET) |
| 43 | + public ModelAndView view(Country country) { |
| 44 | + ModelAndView result = new ModelAndView(); |
| 45 | + if (country.getId() != null) { |
| 46 | + country = countryService.selectByKey(country.getId()); |
| 47 | + } |
| 48 | + result.addObject("country", country); |
| 49 | + return result; |
| 50 | + } |
| 51 | + |
| 52 | + @RequestMapping(value = "save", method = RequestMethod.POST) |
| 53 | + public ModelAndView save(Country country) { |
| 54 | + ModelAndView result = new ModelAndView(redirect_list); |
| 55 | + if (country.getId() != null) { |
| 56 | + countryService.updateAll(country); |
| 57 | + } else { |
| 58 | + countryService.save(country); |
| 59 | + } |
| 60 | + return result; |
| 61 | + } |
| 62 | + |
| 63 | + @RequestMapping("delete") |
| 64 | + public String delete(Integer id) { |
| 65 | + countryService.delete(id); |
| 66 | + return redirect_list; |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments