环境搭建
准备数据库表(dept、emp)
sql
-- 部门管理
create table dept(
id int unsigned primary key auto_increment comment '主键ID',
name varchar(10) not null unique comment '部门名称',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间'
) comment '部门表';
insert into dept (id, name, create_time, update_time) values(1,'学工部',now(),now()),(2,'教研部',now(),now()),(3,'咨询部',now(),now()), (4,'就业部',now(),now()),(5,'人事部',now(),now());
-- 员工管理(带约束)
create table emp (
id int unsigned primary key auto_increment comment 'ID',
username varchar(20) not null unique comment '用户名',
password varchar(32) default '123456' comment '密码',
name varchar(10) not null comment '姓名',
gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',
image varchar(300) comment '图像',
job tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师',
entrydate date comment '入职时间',
dept_id int unsigned comment '部门ID',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '修改时间'
) comment '员工表';
INSERT INTO emp
(id, username, password, name, gender, image, job, entrydate,dept_id, create_time, update_time) VALUES
(1,'jinyong','123456','金庸',1,'1.jpg',4,'2000-01-01',2,now(),now()),
(2,'zhangwuji','123456','张无忌',1,'2.jpg',2,'2015-01-01',2,now(),now()),
(3,'yangxiao','123456','杨逍',1,'3.jpg',2,'2008-05-01',2,now(),now()),
(4,'weiyixiao','123456','韦一笑',1,'4.jpg',2,'2007-01-01',2,now(),now()),
(5,'changyuchun','123456','常遇春',1,'5.jpg',2,'2012-12-05',2,now(),now()),
(6,'xiaozhao','123456','小昭',2,'6.jpg',3,'2013-09-05',1,now(),now()),
(7,'jixiaofu','123456','纪晓芙',2,'7.jpg',1,'2005-08-01',1,now(),now()),
(8,'zhouzhiruo','123456','周芷若',2,'8.jpg',1,'2014-11-09',1,now(),now()),
(9,'dingminjun','123456','丁敏君',2,'9.jpg',1,'2011-03-11',1,now(),now()),
(10,'zhaomin','123456','赵敏',2,'10.jpg',1,'2013-09-05',1,now(),now()),
(11,'luzhangke','123456','鹿杖客',1,'11.jpg',5,'2007-02-01',3,now(),now()),
(12,'hebiweng','123456','鹤笔翁',1,'12.jpg',5,'2008-08-18',3,now(),now()),
(13,'fangdongbai','123456','方东白',1,'13.jpg',5,'2012-11-01',3,now(),now()),
(14,'zhangsanfeng','123456','张三丰',1,'14.jpg',2,'2002-08-01',2,now(),now()),
(15,'yulianzhou','123456','俞莲舟',1,'15.jpg',2,'2011-05-01',2,now(),now()),
(16,'songyuanqiao','123456','宋远桥',1,'16.jpg',2,'2007-01-01',2,now(),now()),
(17,'chenyouliang','123456','陈友谅',1,'17.jpg',NULL,'2015-03-21',NULL,now(),now());
创建springboot工程,引入对应的起步依赖 (web、mybatis、mysql驱动、lombok)
配置文件application.properties中引入mybatis的配置信息,准备对应的实体类
bash
# 驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库连接的URL
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
# 连接数据库的用户名
spring.datasource.username=root
# 连接数据库的密码
spring.datasource.password=123456
# 配置MyBatis的日志,指定输出到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#开启mybatis的驼峰命名自动映射开关
mybatis.configuration.map-underscore-to-camel-case=true
导入准备对应的Mapper、Service(接口、实现类)、Controller基础结构
开发规范
案例基于当前最为主流的前后端分离模式进行开发
开发规范-Restful
REST(REpresentational State Transfer) ,表述性状态转换,它是一种软件架构风格
注意事项
REST是风格,是约定方式,约定不是规定,可以打破
描述模块的功能通常使用复数,也就是加s的格式来描述,表示此类资源,而非单个资源。如: users、emps、books...
查询部门数据
DeptController 类
java
package com.example.controller;
import com.example.pojo.Dept;
import com.example.pojo.Result;
import com.example.service.DeptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author hyk~
*/
@Slf4j
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
//private static Logger log = LoggerFactory.getLogger(DeptController.class);
//@RequestMapping(value = "/depts",method = RequestMethod.GET) //指定请求方式为GET
@GetMapping("/depts")
public Result list(){
log.info("查询全部部门数据");
//调用Service查询部门数据
List<Dept> deptList = deptService.list();
return Result.success(deptList);
}
}
DeptServiceImpl 类
java
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public List<Dept> list() {
return deptMapper.list();
}
}
DeptMapper 类
java
@Mapper
public interface DeptMapper {
//查询全部部门数据
@Select("select * from dept")
List<Dept> list();
}
根据id删除部门数据
DeptController类
java
//根据id删除部门
@DeleteMapping("/{id}")
public Result deleteDept(@PathVariable Integer id) { //@PathVariable 通过该注解来获取路径中id的值/{id}
log.info("根据id删除部门数据:{}", id);
//调用Service删除部门
deptService.DeleteDept(id);
return Result.success();
}
DeptService接口
java
//根据id删除部门信息
void DeleteDept(Integer id);
DeptServiceImpl实现类
java
@Override
public void DeleteDept(Integer id) {
deptMapper.DeleteDept(id);
}
DeptMapper接口
java
//根据Id删除部门信息
@Delete("delete from dept where id = #{id}")
void DeleteDept(Integer id);
整体代码(增删改查)
DeptController类(处理请求,返回响应)
java
package com.example.controller;
import com.example.pojo.Dept;
import com.example.pojo.Emp;
import com.example.pojo.Result;
import com.example.service.DeptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @author hyk~
*/
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
@Autowired
private DeptService deptService;
//private static Logger log = LoggerFactory.getLogger(DeptController.class);
//@RequestMapping(value = "/depts",method = RequestMethod.GET) //指定请求方式为GET
@GetMapping
public Result list() {
log.info("查询全部部门数据");
//调用Service查询部门数据
List<Dept> deptList = deptService.list();
return Result.success(deptList);
}
//根据id删除部门
@DeleteMapping("/{id}")
public Result deleteDept(@PathVariable Integer id) { //@PathVariable 通过该注解来获取路径中id的值/{id}
log.info("根据id删除部门数据:{}", id);
//调用Service删除部门
deptService.DeleteDept(id);
return Result.success();
}
//添加部门
@PostMapping
public Result addDept(@RequestBody Dept dept) {
log.info("添加部门数据{}", dept);
deptService.addDept(dept);
return Result.success();
}
@GetMapping("/{id}")
public Result selectByDeptId(@PathVariable Integer id) {
//日志记录
log.info("根据id查询部门:{}", id);
//调用service层功能
Dept dept = deptService.selectByDeptId(id);
//响应
return Result.success(dept);
}
@PutMapping
public Result update(@RequestBody Dept dept) {
//日志记录
log.info("修改部门:{}", dept);
//调用service层功能
deptService.update(dept);
//响应
return Result.success();
}
}
DeptService接口(业务逻辑)
java
package com.example.service;
import com.example.pojo.Dept;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author hyk~
*/
@Service
public interface DeptService {
//查询全部部门数据
List<Dept> list();
//根据id删除部门信息
void DeleteDept(Integer id);
//添加部门信息
void addDept(Dept dept);
//根据ID查询部门
Dept selectByDeptId(Integer id);
//修改部门
void update(Dept dept);
}
DeptServiceImpl实现类
java
package com.example.service.impl;
import com.example.mapper.DeptMapper;
import com.example.mapper.EmpMapper;
import com.example.pojo.Dept;
import com.example.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
/**
* @author hyk~
*/
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
//查询
@Override
public List<Dept> list() {
return deptMapper.list();
}
//删除
@Override
public void DeleteDept(Integer id) {
deptMapper.DeleteDept(id);
}
//新增
@Override
public void addDept(Dept dept) {
dept.setCreateTime(LocalDateTime.now());
dept.setUpdateTime(LocalDateTime.now() );
deptMapper.addDept(dept);
}
//根据id查询
@Override
public Dept selectByDeptId(Integer id) {
return deptMapper.selectByDeptId(id);
}
//修改
@Override
public void update(Dept dept) {
//设置修改时间为当前
dept.setUpdateTime(LocalDateTime.now());
deptMapper.update(dept);
}
}
DeptMapper接口(数据访问)
java
package com.example.mapper;
import com.example.pojo.Dept;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* @author hyk~
*/
@Mapper
public interface DeptMapper {
//查询全部部门数据
@Select("select * from dept")
List<Dept> list();
//根据Id删除部门信息
@Delete("delete from dept where id = #{id}")
void DeleteDept(Integer id);
//添加部门信息
@Insert(" insert into dept(name, create_time, update_time) values(#{name},#{createTime},#{updateTime});")
void addDept(Dept dept);
//根据id查询数据 数据回显 方便用户修改
@Select("select * from dept where id=#{id}")
Dept selectByDeptId(Integer id);
//修改数据
@Update("update dept set name = #{name},update_time=now() where id =#{id}")
void update(Dept dept);
}
总结
1.开发流程
明确需求
接口文档
思路分析
接口开发
2.接口调试
postman测试
前后端联调
3.日志小技巧
java
@slf4j
@RestController
public class DeptController {
}
注意事项
一个完整的请求路径,应该是类上的 @RequestMapping 的value属性 + 方法上的 @RequestMapping的value属性。