🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# @Controller注解 在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controller层等等的注入配置.使用过程中,在Service层中的实现类头上加@Compopnet注解,在Controller类头加@Controller注解,便完成了配置。 ``` package com.cui.jack.controller; import com.cui.jack.domain.User; import com.cui.jack.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.*; import java.util.List; @Controller public class UserController { @Autowired private UserRepository userRepository; @RequestMapping(value = "/user/{id}" , method = RequestMethod.GET) public String userFindOne(@PathVariable("id") Long id, ModelMap map){ User user = userRepository.findOne(id); map.addAttribute("name",user.getUserName()); return "userList"; } @RequestMapping(value = "/mob/{mobile}" , method = RequestMethod.GET) public List<User> userFindMobile(@PathVariable("mobile") Long mob){ return userRepository.findByMobile(mob); } @RequestMapping(value="/users",method = RequestMethod.GET) public String userList(ModelMap map){ List<User> list = userRepository.findAll(); map.addAttribute("list", list ); return "userList"; } @PutMapping(value="/user/{id}") public User userUpdate( @PathVariable("id") Long id, @RequestParam("mobile") Long mobile, @RequestParam("userName") String userName){ User user = new User(); user.setId(id); user.setUserName(userName); user.setMobile(mobile); return userRepository.save(user); } } ```