|
| 1 | +package com.controller; |
| 2 | + |
| 3 | +import com.compont.Author; |
| 4 | +import com.service.AuthorService_mybatis; |
| 5 | +import org.mybatis.spring.annotation.MapperScan; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.web.bind.annotation.*; |
| 8 | + |
| 9 | +import javax.servlet.http.HttpServletRequest; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +/** |
| 15 | + * Created by zhuzhengping on 2017/3/10. |
| 16 | + */ |
| 17 | +@RestController |
| 18 | +@RequestMapping(value = "/data/mybatis/author") |
| 19 | +@MapperScan("com.dao") |
| 20 | +public class AuthorController_mybatis { |
| 21 | + |
| 22 | + @Autowired |
| 23 | + private AuthorService_mybatis authorServiceMybatis; |
| 24 | + |
| 25 | + @RequestMapping(method = RequestMethod.GET) |
| 26 | + public Map<String,Object> getAuthorList(HttpServletRequest request){ |
| 27 | + |
| 28 | + List<Author> authorList = this.authorServiceMybatis.findAuthorList(); |
| 29 | + |
| 30 | + Map<String,Object> param = new HashMap<String,Object>(); |
| 31 | + |
| 32 | + param.put("total",authorList.size()); |
| 33 | + |
| 34 | + param.put("rows",authorList); |
| 35 | + |
| 36 | + return param; |
| 37 | + } |
| 38 | + |
| 39 | + @RequestMapping(value = "/{userId}",method = RequestMethod.GET) |
| 40 | + public Author getAuthor(@PathVariable("userId") int userId, HttpServletRequest request){ |
| 41 | + Author author = this.authorServiceMybatis.findAuthor((long) userId); |
| 42 | + if(author == null){ |
| 43 | + throw new RuntimeException("查询错误"); |
| 44 | + } |
| 45 | + return author; |
| 46 | + } |
| 47 | + |
| 48 | + @RequestMapping(method = RequestMethod.POST) |
| 49 | + public void add(@RequestBody Map<String,Object> request){ |
| 50 | + String userId = (String) request.get("user_id"); |
| 51 | + String realName = (String) request.get("real_name"); |
| 52 | + String nickName = (String) request.get("nick_name"); |
| 53 | + try { |
| 54 | + this.authorServiceMybatis.add(realName,nickName); |
| 55 | + }catch (Exception e){ |
| 56 | + e.printStackTrace(); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @RequestMapping(value = "/{userId}",method = RequestMethod.PUT) |
| 62 | + public void update(@PathVariable("userId") Long userId,@RequestBody Map<String,Object> request){ |
| 63 | + Author author = this.authorServiceMybatis.findAuthor(userId); |
| 64 | + String realName = (String) request.get("real_name"); |
| 65 | + String nickName = (String) request.get("nick_name"); |
| 66 | + try { |
| 67 | + this.authorServiceMybatis.update(realName,nickName,author.getId()); |
| 68 | + }catch (Exception e){ |
| 69 | + e.printStackTrace(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @RequestMapping(value = "/{userId}",method = RequestMethod.DELETE) |
| 74 | + public void delete(@PathVariable("userId") Long userId){ |
| 75 | + try { |
| 76 | + this.authorServiceMybatis.delete(userId); |
| 77 | + }catch (Exception e){ |
| 78 | + e.printStackTrace(); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments