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. 【转】自动化测试框架: pytest&allure ,提高自动化健壮性和稳定性

    序 在之前,我写过一个系列“从零开始搭建一个简单的ui自动化测试框架(pytest+selenium+allure)”,在这个系列里,主要介绍了如何从零开始去搭建一个可用的自动化工程框架,但是还缺乏了 ...

  2. TOJ 4976: 新生数(深搜)

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4976 时间限制(普通/Java): ...

  3. Python+Selenium学习--简单对象定位

    场景 测试对象的定位和操作是webdriver的核心内容,其中操作又是建立在定位的基础之上,因此对象定位就越发显得重要了. 定位对象的目的一般有下面几种 操作对象 获得对象的属性,如获得测试对象的cl ...

  4. Camera插件推荐,解锁电影大师级视角控制

    相机在游戏中的重要性是不言而喻的,尤其是一些MMORPG或FPS等类型的游戏,相机不仅需要跟随游戏主角进行移动,可能还要随时准备切换焦点,这就要求开发者将游戏相机管理得井井有条,能顺应游戏中可能瞬息发 ...

  5. Practice 10

    通过关键字extends来创建一个类的子类.一个类通过关键字implements声明自己使用一个或者多个接口. settext是根据数组新建的pets[1]object来的,对其调用抽象方法talk, ...

  6. Struts框架的数据封装二之模型驱动方式

    Struts2中提供了两类数据封装的方式? * 第二种方式:模型驱动 > 使用模型驱动的方式,也可以把表单中的数据直接封装到一个JavaBean的对象中,并且表单的写法和之前的写法没有区别! & ...

  7. 长时间没有操作putty就会断开连接是怎么回事?

    seconds between keepalives 设置为10就好了, 这个值有什么含义,服务器为了节省资源采取了一些措施,其中一条就是如果检测一个会话(session)几分钟或者几小时没有数据流入 ...

  8. Employee类

    package demo; import java.time.LocalDate; public class Employee { private String name; private doubl ...

  9. DOM系列基础知识

    DOM (Document Object Model) 即文档对象模型, 针对 HTML 和 XML 文档的 API (应用程序接口) .DOM 描绘了一个层次化的节点树,运行开发人员添加.移除和修改 ...

  10. hdu 2444(二分图) The Accomodation of Students

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 大意是给定n个学生,他们之间可能互相认识,首先判断能不能将这些学生分为两组,使组内学生不认识: 现想将学生 ...