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();
}
//
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();
}
//
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();
}
//
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();
}
//
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);
} }

  

Rest架构风格的实践(使用通用Mapper技术)的更多相关文章

  1. 【DDD】领域驱动设计实践 —— 架构风格及架构实例

    概述 DDD为复杂软件的设计提供了指导思想,其将易发生变化的业务核心域放置在限定上下文中,在确保核心域一致性和内聚性的基础上,DDD可以被多种语言和多种技术框架实现,具体的框架实现需要根据实际的业务场 ...

  2. 理解本真的REST架构风格

       http://kb.cnblogs.com/page/186516/ 引子 在移动互联网.云计算迅猛发展的今天,作为一名Web开发者,如果您还没听说过“REST”这个buzzword,显然已经落 ...

  3. 【转载】理解本真的REST架构风格

    本文将带您领略REST架构的起源.与Web的关系.REST架构的本质及特性,以及REST架构与其他架构风格之间的比较. 引子 在移动互联网.云计算迅猛发展的今天,作为一名Web开发者,如果您还没听说过 ...

  4. 架构-架构风格:REST

    ylbtech-架构-架构风格:REST REST即表述性状态传递(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博 ...

  5. 理解本真的REST架构风格(转,解释的最清楚)

    add by zhj start: Fielding在批判性继承前人研究成果的基础上,建立起来一整套研究和评价软件架构的方法论.这套方法论的核心是“架构风格”这个概念.架构风格是一种研究和评价软件架构 ...

  6. RESTful 架构风格

    在移动互联网的大潮下,『微服务』的概念也越来越被大家接受并应用于实践,日益增多的web service逐渐统一于RESTful 架构风格,如果开发者对RESTful 架构风格不甚了解,则开发出的所谓R ...

  7. 深入探索REST(2):理解本真的REST架构风格

    文章转载地址:https://www.infoq.cn/article/understanding-restful-style/,如引用请标注文章原地址 引子 在移动互联网.云计算迅猛发展的今天,作为 ...

  8. springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+freemarker+layui

    前言: 开发环境:IDEA+jdk1.8+windows10 目标:使用springboot整合druid数据源+mysql+mybatis+通用mapper插件+pagehelper插件+mybat ...

  9. 从零搭建Spring Boot脚手架(4):手写Mybatis通用Mapper

    1. 前言 今天继续搭建我们的kono Spring Boot脚手架,上一文把国内最流行的ORM框架Mybatis也集成了进去.但是很多时候我们希望有一些开箱即用的通用Mapper来简化我们的开发.我 ...

随机推荐

  1. 斐波那契数列(python)

    题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 # -*- coding:utf-8 -*- class Solut ...

  2. 关于 No buffer space available (maximum connections reached?): connect 的处理

    一.问题: hudson一个应用打包部署一直不成功,检查报错 检查项目的JOB配置,开始以为是SVN的问题,但是重启SVN后问题一直存在 二.分析: TCP协议中,关闭TCP连接的是Server端(当 ...

  3. Websocket实现群聊、单聊

    Websocket 使用的第三方模块:gevent-websocket 群聊 ws群聊.py中的内容 from flask import Flask, request, render_template ...

  4. Maximum Gap (ARRAY - SORT)

    QUESTION Given an unsorted array, find the maximum difference between the successive elements in its ...

  5. Unity之Application.runInBackground = true

    默认是False, 设置 Application.runInBackground = true; 则 void OnApplicationPause(bool pause) 不再起作用

  6. TCP/IP中的四元组、五元组、七元组

    四元组:源IP地址.目的IP地址.源端口.目的端口 五元组:源IP地址.目的IP地址.源端口.目的端口.传输层协议 七元组:源IP地址.目的IP地址.源端口.目的端口.传输层协议,服务类型以及接口索引

  7. javascript中的类型转换(进制转换|位运算)

    1:parseInt(string) : 这个函数的功能是从string的开头开始解析,返回一个整数 parseInt("123hua"); //输出 123 parseInt(& ...

  8. SQL Server 2008中的CTE递归查询得到一棵树

    ROW_NUMBER() OVER()函数用法 with CTE as     (      -->Begin 一个定位点成员       select ID, Name,Parent,cast ...

  9. [z]分区truncate操作的介绍及对全局索引和空间释放影响的案例解析

    [z]https://www.2cto.com/database/201301/181226.html 环境: [sql] [oracle@localhost ~]$ uname -r 2.6.18- ...

  10. Logback报错 no applicable action for [Encoding], current ElementPath is [[configuration][appender][Encoding]]

    老版本是0.9,移到springboot项目,解决办法,删除xml配置文件节点<Encoding>UTF-8</Encoding>