controller代碼
java
package cn.internet.userinfo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; 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.ResponseBody; import cn.internet.userinfo.pojo.User; import cn.internet.userinfo.service.UserService; @RequestMapping("rest/user") @Controller public class RestUserController { @Autowired private UserService userService; /** * 新增 * Title: saveUser * @param user * @return */ @RequestMapping(method=RequestMethod.POST) //@ResponseBody public ResponseEntity<Void> saveUser(User user){ try { this.userService.saveUser(user); //返回201 return ResponseEntity.status(HttpStatus.CREATED).build(); } catch (Exception e) { e.printStackTrace(); } //500服務器內部錯誤 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); } /** * 刪除 * Title: deleteUser * @param id * @return */ @RequestMapping(value="{id}",method=RequestMethod.DELETE) public ResponseEntity<Void> deleteUser(@PathVariable("id") Long id){ try { this.userService.deleteUser(id); //返回204 return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); } catch (Exception e) { e.printStackTrace(); } //500服務器內部錯誤 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); } /** * 修改 * Title: updateUser * @param user * @return */ @RequestMapping(method=RequestMethod.PUT) public ResponseEntity<Void> updateUser(User user){ try { this.userService.updateUser(user); //返回204 return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); } catch (Exception e) { e.printStackTrace(); } //500服務器內部錯誤 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); } /** * 根據id查詢 * Title: queryUserById * @param id * @return */ @RequestMapping(value="{id}",method=RequestMethod.GET) public ResponseEntity<User> queryUserById(@PathVariable("id") Long id){ try { User user = this.userService.queryUserById(id); //返回200 //return ResponseEntity.ok(user); return ResponseEntity.ok().body(user); } catch (Exception e) { e.printStackTrace(); } //500服務器內部錯誤 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); } }
service代碼git
package cn.internet.userinfo.service; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import cn.internet.userinfo.mapper.UserMapper; import cn.internet.userinfo.pojo.User; @Service public class UserService { @Autowired private UserMapper userMapper; /** * 根據id查詢用戶數據 * * @param id * @return */ public User queryUserById(Long id) { User user = this.userMapper.selectByPrimaryKey(id); return user; } /** * 分頁查詢用戶數據 * * @param page 頁碼 * @param rows 每頁顯示數據條數 * @return */ public PageInfo<User> queryListByPage(Integer page, Integer rows) { PageHelper.startPage(page, rows); List<User> list = this.userMapper.select(null); PageInfo<User> pageInfo=new PageInfo<User>(list); return pageInfo; } /** * 保存 * Title: saveUser */ public void saveUser(User user) { user.setId(null); } /** * 刪除 * Title: deleteUser * @param id */ public void deleteUser(Long id) { this.userMapper.deleteByPrimaryKey(id); } /** * 修改 * Title: updateUser * @param user */ public void updateUser(User user) { user.setUpdated(new Date()); this.userMapper.updateByPrimaryKeySelective(user); } }