From d1ec2af606d7e32b243a0f6cbc645c9e2f97e8ba Mon Sep 17 00:00:00 2001 From: javahongxi Date: Wed, 24 Jan 2018 11:06:05 +0800 Subject: [PATCH] =?UTF-8?q?RequestBody=20-=20=E4=BB=A5json=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E6=8E=A5=E6=94=B6=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spring/boot/controller/NewController.java | 22 +++++++++---------- .../whatsmars/spring/boot/dao/UserMapper.java | 4 ++++ .../spring/boot/service/UserService.java | 5 +++++ .../boot/service/impl/UserServiceImpl.java | 10 +++++++++ .../src/main/resources/mapper/UserMapper.xml | 12 ++++++++++ 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/controller/NewController.java b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/controller/NewController.java index d8495825..e959c82a 100644 --- a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/controller/NewController.java +++ b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/controller/NewController.java @@ -22,14 +22,14 @@ public class NewController { @Autowired private UserService userService; - @RequestMapping(value = "/t", method = RequestMethod.GET) + @GetMapping("/t") public Map query() { Map m = new HashMap(); m.put("domain", "toutiao.im"); return m; } - @RequestMapping(value = "/t", method = RequestMethod.POST) + @PostMapping("/t") public HttpStatus add(@RequestParam(name = "name") String username, @RequestParam(required = false) String nickname, @RequestParam(required = false, defaultValue = "1") Integer gender, @@ -43,18 +43,16 @@ public class NewController { return HttpStatus.OK; } - @RequestMapping(value = "/t", method = RequestMethod.PUT) - public Map update() { - Map m = new HashMap(); - m.put("domain", "toutiao.im"); - return m; + @PutMapping("/t") + public HttpStatus update(@RequestBody User user) { // 以json格式接收参数 + userService.update(user); + return HttpStatus.OK; } - @RequestMapping(value = "/t", method = RequestMethod.DELETE) - public Map delete() { - Map m = new HashMap(); - m.put("domain", "toutiao.im"); - return m; + @DeleteMapping("/t") + public HttpStatus delete(Long id) { + userService.delete(id); + return HttpStatus.OK; } @RequestMapping(value = "/e", method = RequestMethod.GET) diff --git a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/dao/UserMapper.java b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/dao/UserMapper.java index 84cd7a99..a7183da4 100644 --- a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/dao/UserMapper.java +++ b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/dao/UserMapper.java @@ -18,4 +18,8 @@ public interface UserMapper { void insertBatch(List users); List query(); + + void update(User user); + + void delete(Long id); } diff --git a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/UserService.java b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/UserService.java index ed7a1176..46ef2e18 100644 --- a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/UserService.java +++ b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/UserService.java @@ -12,5 +12,10 @@ public interface UserService { void add(User user); + void update(User user); + + void delete(Long id); + Page query(int offset, int limit); + } diff --git a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/impl/UserServiceImpl.java b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/impl/UserServiceImpl.java index 99c317a6..2d43e642 100644 --- a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/impl/UserServiceImpl.java +++ b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/impl/UserServiceImpl.java @@ -32,6 +32,16 @@ public class UserServiceImpl implements UserService { logger.info("add user success, username: {}", user.getUsername()); } + @Override + public void update(User user) { + userMapper.update(user); + } + + @Override + public void delete(Long id) { + userMapper.delete(id); + } + @Override public Page query(int offset, int limit) { return PageHelper.offsetPage(offset, limit).doSelectPage(() -> userMapper.query()); diff --git a/whatsmars-spring-boot/src/main/resources/mapper/UserMapper.xml b/whatsmars-spring-boot/src/main/resources/mapper/UserMapper.xml index a0439f56..c3aa2818 100644 --- a/whatsmars-spring-boot/src/main/resources/mapper/UserMapper.xml +++ b/whatsmars-spring-boot/src/main/resources/mapper/UserMapper.xml @@ -22,4 +22,16 @@ select * from user + + update user set update_date = NOW + , username = #{username} + , nickname = #{nickname} + , age = #{age} + where id = #{id} + + + + delete from user where id = #{id} + + \ No newline at end of file -- GitLab