Loading... # 引言 随着RESTFul风格的快速兴起,这次主要运用一下RESTFul风格,顺便温习一下总是在自动生成的MyBatis,手写一遍。。 # 什么是RESTFul 通常情况下,如果我们想查询一个订单信息,我们需要提供订单编号,然后通过调用Controller层的方法,比如我们要调用queryOrder(Long orderId)和deleteOrder(Long orderId)方法,传统上我们的URL地址是这样的。 ```text url: http(s)://ip:port/order/queryOrder?id=订单号 method: GET url: http(s)://ip:port/order/deleteOrder?id=订单号 method: GET ``` 然而不是很优雅,所以采用了RESTFul风格,同样的方法,你看有什么区别呢? ```text url: http(s)://ip:port/order/订单号 method: GET url: http(s)://ip:port/order/订单号 method: POST ``` # 扩充 突然想到了URL URI了,所以附上wiki的地址,需要的可以去看一下奥。 https://en.wikipedia.org/wiki/Uniform_Resource_Identifier # 表结构与数据 ```sql CREATE TABLE `stu` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `score` double DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8; INSERT INTO `stu` VALUES (1, 'zunmx', 22, 100); INSERT INTO `stu` VALUES (2, 'howl', 18, 100); INSERT INTO `stu` VALUES (3, 'johnny', 23, 98); INSERT INTO `stu` VALUES (4, 'shally', 23, 97); INSERT INTO `stu` VALUES (5, 'Teq5', 23, 96); INSERT INTO `stu` VALUES (6, 'M.White', 21, 95); INSERT INTO `stu` VALUES (7, 'kung', 24, 94); INSERT INTO `stu` VALUES (8, 'JDroid', 23, 93); INSERT INTO `stu` VALUES (9, 'Gnu.a', 22, 92); INSERT INTO `stu` VALUES (10, 'Aiu.w', 20, 91); INSERT INTO `stu` VALUES (11, 'known', 1, 59); ``` # 展示 ![query](https://www.zunmx.top/usr/uploads/2021/05/1499495083.png) ![query](https://www.zunmx.top/usr/uploads/2021/05/811496300.png) ![insert](https://www.zunmx.top/usr/uploads/2021/05/3423269858.png) ![modify](https://www.zunmx.top/usr/uploads/2021/05/2626307226.png) ![delete](https://www.zunmx.top/usr/uploads/2021/05/3632984060.png) # 代码部分 ## pom.xml ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>top.zunmx</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBoot_MyBatis</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project> ``` ## pojo ```java package top.zunmx.demo.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Student { private Integer id; private String name; private Integer age; private Double score; } ``` ## service StudentService ```java package top.zunmx.demo.service; import top.zunmx.demo.pojo.Student; import java.util.List; public interface StudentService { //insert public Integer insertStudent(Student student); //modify public boolean modifyStudent(Integer id, Student student); //queryOne public Student queryStudent(Integer id); //getList public List<Student> queryAll(); //deleteStudent public int deleteStudent(Integer id); } ``` StudentServiceImpl ``` package top.zunmx.demo.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import top.zunmx.demo.mapper.StudentMapper; import top.zunmx.demo.pojo.Student; import top.zunmx.demo.service.StudentService; import java.util.List; @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentMapper studentMapper; @Override public Integer insertStudent(Student student) { studentMapper.insertStudent(student); return student.getId(); } @Override public boolean modifyStudent(Integer id, Student student) { return studentMapper.modifyStudent(id,student)>0; } @Override public Student queryStudent(Integer id) { return studentMapper.queryStudent(id); } @Override public List<Student> queryAll() { return studentMapper.queryAll(); } @Override public int deleteStudent(Integer id){return studentMapper.deleteStudent(id); } } ``` ## mapper ```java package top.zunmx.demo.mapper; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import top.zunmx.demo.pojo.Student; import java.util.List; @Mapper public interface StudentMapper { public int insertStudent(Student student); // 注意 当使用SELECT LAST_INSERT_KEY 时不能加@Param注解嗷 public int modifyStudent(Integer id,@Param("student")Student student); public Student queryStudent(Integer id); public List<Student> queryAll(); public int deleteStudent(Integer id); } ``` ## controller ```java package top.zunmx.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import top.zunmx.demo.pojo.Student; import top.zunmx.demo.service.StudentService; import java.util.List; @RestController public class StudentController { @Autowired private StudentService studentService; @RequestMapping("/") public String index() { return "THIS IS INDEX PAGE"; } @GetMapping("/student") public List<Student> list() { return studentService.queryAll(); } @GetMapping("/student/{id}") public Student query(@PathVariable("id") Integer id) { return studentService.queryStudent(id); } @PostMapping("/student") public String insert(Student student) { Integer id = studentService.insertStudent(student); return id > 0 ? "插入成功-->" + id : "插入失败"; } @PutMapping("/student/{id}") public String modify(@PathVariable("id") Integer id, Student student) { return studentService.modifyStudent(id, student) ? "修改成功" : "修改失败"; } @DeleteMapping("/student/{id}") public String delete(@PathVariable("id") Integer id) { return studentService.deleteStudent(id) > 0 ? "删除成功" : "删除失败"; } } ``` ## Mapper.xml ```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="top.zunmx.demo.mapper.StudentMapper"> <resultMap id="BaseResultMap" type="top.zunmx.demo.pojo.Student"> <result column="id" jdbcType="INTEGER" property="id"/> <result column="name" jdbcType="VARCHAR" property="name"/> <result column="age" jdbcType="INTEGER" property="age"/> <result column="score" jdbcType="FLOAT" property="score"/> </resultMap> <sql id="QAll">select * from stu</sql> <select id="queryAll" resultMap="BaseResultMap"> <include refid="QAll"></include> </select> <select id="queryStudent" resultMap="BaseResultMap"> <include refid="QAll"></include> where id = #{id} </select> <delete id="deleteStudent" parameterType="int"> delete from stu where id = #{id} </delete> <update id="modifyStudent"> update stu SET name = #{student.name},age=#{student.age},score = #{student.score} where id = #{id} </update> <insert id="insertStudent"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT LAST_INSERT_ID() AS id </selectKey> insert into stu values (null,#{name},#{age},#{score}) </insert> </mapper> ``` ## yml ```yml spring: datasource: url: jdbc:mysql://localhost:3306/studentmanager driver-class-name: com.mysql.cj.jdbc.Driver username: study password: 123456 type: com.alibaba.druid.pool.DruidDataSource mybatis: mapper-locations: classpath:mapping/*Mapper.xml type-aliases-package: top.zunmx.demo.pojo ``` # RESTFul风格的实例 > 其实和上面的整合是一样的 ```java package top.zunmx.demo.controller; import org.springframework.web.bind.annotation.*; import top.zunmx.demo.pojo.Student; @RestController public class DemoTest { //RESTFUL @RequestMapping("/testDemo") public String index() { return "THIS IS INDEX PAGE"; } @GetMapping("/test") public String list() { return "THIS IS LIST PAGE"; } @GetMapping("/test/{id}") public String query(@PathVariable("id") Integer id) { return "THIS IS QUERY PAGE, QUERY ID ->" + id; } @PostMapping("/test") public String insert(Student student) { return "THIS IS INSERT PAGE, CONTENT ->" + student; } @PutMapping("/test/{id}") public String modify(@PathVariable("id") Integer id, Student student) { return "THIS IS MODIFY PAGE, MODIFY ID ->" + id + " TO" + student; } @DeleteMapping("/test/{id}") public String delete(@PathVariable("id") Integer id) { return "THIS IS DELETE PAGE, DELETE ID ->" + id; } } ``` # 附件 [train_sb_m.rar](https://www.zunmx.top/usr/uploads/2021/05/1672849693.rar) © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏