Restful
1.特点
RESTful是一种架构风格,强调简单、轻量级和对资源的状态less
操作。 RESTful是通过HTTP协议进行通信的。 RESTful的应用程序可以调用运行在不同服务器上的服务或函数。 RESTful的接口通常使用JSON,但实际上它们都支持多种数据格式。 RESTful的接口都是无状态的,意味着每个请求都是独立的,服务器不会保存之前请求的状态。 RESTful的接口通常使用JSON。JSON更简洁,易于解析。 RESTful的接口通常不使用SOAP协议,也不遵循WS-*标准,更加简单和灵活。 RESTful的接口遵循REST架构原则,使用标准的HTTP方法(如GET, POST, PUT, DELETE)进行资源的操作。 RESTful的接口更简单、更轻量级,通常更易于开发和维护
2.Rest设计规范
3.基于通用Mapper的Rest编写API
启动类
package com. saddam ; import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
import tk. mybatis. spring. annotation. MapperScan ; @SpringBootApplication
@MapperScan ( basePackages = "com.saddam.mapper" )
public class Application { public static void main( String [ ] args) { SpringApplication. run( Application. class , args) ; }
}
mapper
package com. saddam. mapper ; import com. saddam. my. mapper. MyMapper ;
import com. saddam. pojo. Stu ; public interface StuMapper extends MyMapper < Stu > {
}
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.saddam.mapper.StuMapper" > < resultMap id = " BaseResultMap" type = " com.saddam.pojo.Stu" > < id column = " id" property = " id" jdbcType = " INTEGER" /> < result column = " name" property = " name" jdbcType = " VARCHAR" /> < result column = " age" property = " age" jdbcType = " INTEGER" /> </ resultMap>
</ mapper>
service
package com. saddam. service ; import com. saddam. pojo. Stu ; public interface StuService { public Stu getStuInfo ( int id) ; public void saveStu ( ) ; public void updateStu ( int id) ; public void deleteStu ( int id) ;
}
impl
package com. saddam. service. impl ; import com. saddam. mapper. StuMapper ;
import com. saddam. pojo. Stu ;
import com. saddam. service. StuService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import org. springframework. transaction. annotation. Propagation ;
import org. springframework. transaction. annotation. Transactional ; @Service
public class StuServiceImpl implements StuService { @Autowired public StuMapper stuMapper; @Transactional ( propagation = Propagation . SUPPORTS ) @Override public Stu getStuInfo ( int id) { return stuMapper. selectByPrimaryKey ( id) ; } @Transactional ( propagation = Propagation . REQUIRED ) @Override public void saveStu ( ) { Stu stu = new Stu ( ) ; stu. setName ( "jack" ) ; stu. setAge ( 19 ) ; stuMapper. insert ( stu) ; } @Transactional ( propagation = Propagation . REQUIRED ) @Override public void updateStu ( int id) { Stu stu = new Stu ( ) ; stu. setId ( id) ; stu. setName ( "lucy" ) ; stu. setAge ( 20 ) ; stuMapper. updateByPrimaryKey ( stu) ; } @Transactional ( propagation = Propagation . REQUIRED ) @Override public void deleteStu ( int id) { stuMapper. deleteByPrimaryKey ( id) ; }
}
pojo
package com. saddam. pojo ; import javax. persistence. Id ; public class Stu { @Id private Integer id; private String name; private Integer age; public Integer getId ( ) { return id; } public void setId ( Integer id) { this . id = id; } public String getName ( ) { return name; } public void setName ( String name) { this . name = name; } public Integer getAge ( ) { return age; } public void setAge ( Integer age) { this . age = age; }
}
controller
package com. saddam. controller ; import com. saddam. service. StuService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. PostMapping ;
import org. springframework. web. bind. annotation. RestController ;
@RestController
public class StuFooController { @Autowired private StuService stuService; @GetMapping ( "/getStu" ) public Object getStu ( int id) { return stuService. getStuInfo ( id) ; } @PostMapping ( "/saveStu" ) public Object saveStu ( ) { stuService. saveStu ( ) ; return "OK" ; } @PostMapping ( "/updateStu" ) public Object updateStu ( int id) { stuService. updateStu ( id) ; return "OK" ; } @PostMapping ( "/deleteStu" ) public Object deleteStu ( int id) { stuService. deleteStu ( id) ; return "OK" ; }
}