zoukankan      html  css  js  c++  java
  • Rest架构风格的实践(使用通用Mapper技术)

    1、根据用户 id 查询用户数据

      1.1 controll控制器

    @RequestMapping("restful/user")
    @Controller
    public class RestfulUserController {
        
        @Autowired
        private NewUserService newUserService;
        
        /**
         * 根据用户 id查询
         * @param id
         * @return
         */
        @RequestMapping(value="{id}",method=RequestMethod.GET)
        @ResponseBody
        public ResponseEntity<User> queryUserByid(@PathVariable("id") Long id){
            try {
                User user= this.newUserService.queryUserid(id);
                if (user==null) {
                    //请求资源不存在 404 
                    return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
                }
                    //请求资源存在,200
    //        return ResponseEntity.status(HttpStatus.OK).body(user);
                return ResponseEntity.ok(user);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //500
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
       }

      1.2 通用Mapper

    public interface NewUserMapper extends Mapper<User>{
        
    }

      1.3 Service设置

    @Service
    public class NewUserService {
        
        @Autowired
        private NewUserMapper newUsermapper;public User queryUserid(Long id) {
            return this.newUsermapper.selectByPrimaryKey(id);
        }
    
    }

      1.4查询

    2、新增用户

      2.1 Controller

    @RequestMapping("restful/user")
    @Controller
    public class RestfulUserController {
        
        @Autowired
        private NewUserService newUserService;
    /**
         * 插入用户
         * @param user
         * @return
         */
        @RequestMapping(method=RequestMethod.POST)
        public ResponseEntity<Void> insertUser(User user){
            try {
                //添加成功
                this.newUserService.saveUser(user);
                return ResponseEntity.status(HttpStatus.CREATED).build();
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 500
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
        
    }

      2.2 service设置

    @Service
    public class NewUserService {
        
        @Autowired
        private NewUserMapper newUsermapper;public void saveUser(User user) {
            this.newUsermapper.insert(user);
        }
    
    }

    3、修改数据

      3.1 对于 PUT请求方式,默认不可以提交表单数据的,必须使用过滤器进行配置。。

    在 Web.xml中配置过滤器
    <filter> <filter-name>HttpMethodFilter</filter-name> <filter-class>org.springframeword.web.filter.HttpPutFormContentFilter</filter-calss>  此过滤器只能处理PUT请求 </filter> <filter-mapping> <filter-name>HttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

      3.2 Controller设置

    
    @RequestMapping("restful/user")
    @Controller
    public class RestfulUserController {
        
        @Autowired
        private NewUserService newUserService;
    /**
         * 更新用户数据
         * @param user
         * @return
         */
        @RequestMapping(method=RequestMethod.PUT)
        public ResponseEntity<Void> updateUser(User user){
            try {
                //修改成功
                this.newUserService.updateuser(user);
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 500
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
            
        }
        
    }

      3.3 servicer设置

    @Service
    public class NewUserService {
        
        @Autowired
        private NewUserMapper newUsermapper;public void updateuser(User user) {
            this.newUsermapper.updateByPrimaryKeySelective(user);
        }
    
    }

    4、删除数据

      4.1 默认请求方式中,DELETE方式不会提交表单的,必须在web.xml中进行配置

    <!--将POST请求转化为DELETE或者是PUT要用 _method指定真正的请求参数--> 此过滤器更加强大
    
    <filter>
          <filter-name>HiddenHttpMethodFilter</filter-name>
          <filer-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filer>
    <filter-mapping>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>

      4.2 Controller

    @RequestMapping("restful/user")
    @Controller
    public class RestfulUserController {
        
        @Autowired
        private NewUserService newUserService;

    /** * 删除用户数据 * @return */ @RequestMapping(method=RequestMethod.DELETE)  DELETE请求方式 public ResponseEntity<Void> deletedUser(@RequestParam(value="id",defaultValue="0") Long id){ try { if (id.intValue()==0) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); } this.newUserService.deleteuserByid(id); return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); } catch (Exception e) { e.printStackTrace(); } // 500 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); }
    }

      4.3 Service设置

    @Service
    public class NewUserService {
        
        @Autowired
        private NewUserMapper newUsermapper;
    public void deleteuserByid(Long id) { this.newUsermapper.deleteByPrimaryKey(id); }

    }

      

  • 相关阅读:
    Ios插件开发
    React-Native学习指南
    APP测试基本流程
    iOS开发-由浅至深学习block
    你真的会用UITableView嘛
    iOS系统右滑返回全局控制方案
    优化UITableViewCell高度计算的那些事
    UITableViewCell高度自适应探索--AutoLayout结合Frame
    UITableView优化技巧
    页面间跳转的性能优化(一)
  • 原文地址:https://www.cnblogs.com/axu521/p/10233540.html
Copyright © 2011-2022 走看看