之前一段时间学习了一些springboot的一些基础使用方法和敲了一些例子,是时候写一个简单的crud来将之前学的东西做一个整合了 —— 一个员工列表的增删改查。
这次的CRUD将使用restful api的风格,restful api的风格现在非常流行,那restful api和传统的api有什么区别呢?
举个例子来说吧
查找某个用户
添加用户
restful api 使用统一资源标识符(URI)来寻址资源。所带的参数直接用“/”跟在后面,“/”用来表示指示层级关系,而不会用?key=value的方式来定义URI。
另外,restful api中对http请求方法的使用也有讲究, GET:查询,POST:新增,PUT:更新,DELETE:删除。restful api用HTTP请求方式区分对资源CRUD操作。
而在传统的api中一般简单的就用GET,复杂一点的就用POST,传统api使用crud来区分操作。
下面我先定义一下这次实现restful crud 的请求架构:
功能 | 请求URI | 请求方式 |
---|---|---|
查询所有员工 | /emps | GET |
查询某个员工 | /emp/{id} | GET |
来到添加页面 | /emp | GET |
添加员工 | /emp | POST |
修改员工 | /emp | PUT |
删除员工 | /emp/{id} | DELETE |
thymeleaf是springboot推荐使用的模板引擎,我用起来也是十分舒服的,再也不用像之前那样苦逼地在js中拼接字符串了。
同时,前端页面用的是基于bootstrap 的一套模板页面。
就先介绍这么多了,有其他要补充的就要下面的实践中说吧,下面,我们就开始一步步构建这个restful crud!!
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
spring:
#数据库配置
datasource:
url: jdbc:mysql://localhost:3306/restFulCrud?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
#mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.jotal.springboot08restfulcrud.entities
mapper-location就是放置mybatis映射文件的地方,classpath: 表示包名下 src->main->resources/
type-aliases-package 为实体类添加别名,方便使用
Employee员工类
package com.jotal.springboot08restfulcrud.entities;
public class Employee {
private Integer emp_id;
private String lastName;
private String email;
//1 male, 0 female
private Integer gender;
private Department department;
private Date birth;
//getter、setter、constructor、toString
}
Department部门类
package com.jotal.springboot08restfulcrud.entities;
public class Department {
private Integer departmentId;
private String departmentName;
//getter、setter、constructor、toString
}
User系统用户类
package com.jotal.springboot08restfulcrud.entities;
public class User {
private String username;
private String password;
//getter、setter、constructor、toString
}
DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
`departmentId` int(10) NOT NULL AUTO_INCREMENT,
`departmentName` varchar(50) NOT NULL,
PRIMARY KEY (`departmentId`)
) ENGINE=InnoDB AUTO_INCREMENT=104 DEFAULT CHARSET=utf8;
INSERT INTO `department` VALUES ('101', 'A-A');
INSERT INTO `department` VALUES ('102', 'B-B');
INSERT INTO `department` VALUES ('103', 'C-C');
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`emp_id` int(10) NOT NULL AUTO_INCREMENT,
`lastName` varchar(20) NOT NULL,
`email` varchar(100) NOT NULL,
`gender` tinyint(2) NOT NULL DEFAULT '1' COMMENT '0:女\r\n1:男',
`birth` date NOT NULL,
`department` int(10) DEFAULT NULL,
PRIMARY KEY (`emp_id`),
KEY `dept` (`department`),
CONSTRAINT `dept` FOREIGN KEY (`department`) REFERENCES `department` (`departmentId`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1008 DEFAULT CHARSET=utf8;
INSERT INTO `employee` VALUES ('1001', 'AA01', '1001@j.com', '1', '2019-08-12', '101');
INSERT INTO `employee` VALUES ('1002', 'BB02', '1002@j.com', '1', '2019-08-17', '102');
INSERT INTO `employee` VALUES ('1003', 'CC03', '1003@j.com', '1', '2019-08-05', '103');
INSERT INTO `employee` VALUES ('1004', 'CC04', '1004@j.com', '0', '2019-08-12', '103');
INSERT INTO `employee` VALUES ('1005', 'jotal', '1066@qq.com', '1', '1997-10-07', '102');
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('admin', '123');
INSERT INTO `user` VALUES ('jotal', '123');
今天我们先讲到这里,今天主要就是介绍了一下这个crud的一些要点,例如使用什么技术,是什么风格的。还有创建了项目,添加了数据,紧接着要做的就是写dao层,看能不能成功从数据库中取出数据。see you!
使用springboot实现一个简单的restful crud——01、项目简介以及创建项目
原文:https://www.cnblogs.com/Jotal/p/11343206.html