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. Math.random控制随机数范围

    let minNum= parseInt(Math.random()*7) + 1 let maxNum= parseInt(Math.random()*83) + 1 生成7~83的随机整数

  2. 二分图 最小点覆盖 poj 3041

    题目链接:Asteroids - POJ 3041 - Virtual Judge  https://vjudge.net/problem/POJ-3041 第一行输入一个n和一个m表示在n*n的网格 ...

  3. random 模块 时间模块(time) sys模块 os模块

    random  模块 1.随机小数 random.random()  0-1内的随机小数 random.uniform(1,5)  1-5范围内的随机小数 2.随机整数 random.randint( ...

  4. mysql windows安装资源

    压缩包资源 https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/ 配置流程 https://blog.csdn.net/hel ...

  5. 高级测试岗位面试题---MARK

    直接手写一个python类 直接手写一个构造函数 紧接着上面的代码,直接手写,补充完整代码,要求对列表中的人进行排序,并筛选出分数大于80的人的名单,组成一个新的列表显示出来. class Perso ...

  6. c# 关闭和重启.exe程序

    Process[] myprocess = Process.GetProcessesByName("a"); if (myprocess.Count() > 0)//判断如果 ...

  7. SVN集成compare4比较软件

    打开TortoiseSVN的Setting,选择左边的Diff Viewer 设置如下: "D:\Program Files\Beyond Compare 4\BComp.exe" ...

  8. Visual Studio 2015 自动生成 的大文件xxx.vc.db的删除问题

    用vs2015创建Visual C++项目,编写生成后,每次都会生成一个project_name.VC.db文件,而且会随着你工程修改运行变的越来越大. project_name.VC.db是sqli ...

  9. HBase数据库集群配置【转】

    https://www.cnblogs.com/ejiyuan/p/5591613.html HBase简介 HBase是Apache Hadoop中的一个子项目,是一个HBase是一个开源的.分布式 ...

  10. (OS 64)指定的网络名不再可用,winnt_accept: Asynchronous AcceptEx failed.

    在httpd.conf中添加 <IfModule mpm_winnt.c> ThreadsPerChild 150 MaxRequestsPerChild 10000 Win32Disab ...