为了方便调试,前期用php实现了CTS项目的Beta版服务端。客户端(Android/iOS/微信小程序)的基本功能已经完成,准备把服务端重构一下,切换到Java(Spring+SpringMVC+Mybatis)。第一次用SSM,花了2个小时,把Demo跑通。总体感觉比以前用的组合(Struts+Spring+Hibernate/ibatis),在xml配置以及代码量方面,更加简洁方便。
@Controller @RequestMapping("/user") public class UserController { // 注入用户服务类 @Autowired private UserService userService = null; // 获取用户 // {"user_id":28,"user_name":"蒋智昊"} @RequestMapping(value = "/getUser0", method = RequestMethod.GET) public @ResponseBody User getUserJson(@RequestParam("id") Long id) { User user = userService.getUser(id); return user; } // 获取用户 // {"user":{"user_id":1,"user_name":"蒋智昊"}} @RequestMapping(value = "/getUser1", method = RequestMethod.GET) public ModelAndView getUser(@RequestParam("id") Long id) { User user = userService.getUser(id); ModelAndView mv = new ModelAndView(); // 给数据模型添加一个用户对象 mv.addObject("user", user); // 指定视图类型 mv.setView(new MappingJackson2JsonView()); return mv; } // 获取用户 // jsp页面赋值 @RequestMapping(value = "/getUser2", method = RequestMethod.GET) public ModelAndView getUserDetail(@RequestParam("id") Long id) { User user = userService.getUser(id); ModelAndView mv = new ModelAndView(); // 给数据模型添加一个用户对象 mv.addObject("user", user); // 指定视图类型 mv.setViewName("user"); return mv; } }