一个专门做特卖的网站,网络公司推广,厦门软件园网站开发,泰安市诺盾网络有限公司SpringBootWeb项目
TILAS智能学习辅助系统
需求
部门管理
查询部门列表
删除部门
新增部门
修改部门
员工管理
查询员工列表(分页)
删除员工
新增员工
修改员工
准备工作
导入依赖
web(2.7.6)
mybatis
mysql驱动
lombok
准备好包结构
Controller-Servi…SpringBootWeb项目
TILAS智能学习辅助系统
需求
部门管理
查询部门列表
删除部门
新增部门
修改部门
员工管理
查询员工列表(分页)
删除员工
新增员工
修改员工
准备工作
导入依赖
web(2.7.6)
mybatis
mysql驱动
lombok
准备好包结构
Controller-Service-Mapper-Pojo
controller
Controller
public interface DeptController{
}Controller
public interface EmpController {
}mapper
Mapper
public interface DeptMapper {
}
Mapper
public interface EmpMapper {
}service/serviceImpl
Service
public interface DeptService {
}
Service
public interface EmpService {
}Slf4j
Service
public class DeptServiceImpl {
}
Slf4j
Service
public class EmpServiceImpl {
}创建数据库表和对应的实体类
Data
public class Dept {private Integer id;private String username;private String password;private String name;private Short gender;private String image;private Short job;private LocalDate entrydate;private Integer deptId;private LocalDateTime createTime;private LocalDateTime updateTime;
}Data
public class Emp {private Integer id;private String name;private LocalDateTime createTime;private LocalDateTime updateTime;
}
在配置文件中引入数据库连接
server.port8080
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locationsclasspath:mappers/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-packagecom.example.tlias.mybatis.entityspring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver
spring.datasource.urljdbc:mysql://localhost:3306/Tlias_db
spring.datasource.usernameroot
spring.datasource.passwordljymysqlpwd
#开启日志
mybatis.configuration.logimplorg.apache.ibatis.logging.stdout.StdOutImpl
#开启字段到实体类的驼峰映射
mybatis.configuration.map-underscore-to-camel-casetrue开发规范
基于前后端分离模式进行开发
定义主流的REST风格API接口进行交互
REST:(Representational State Transfer)表现形式状态转换,一种软件架构风格
url/aaa/1 GET:查询
url/aaa POST:新增
url/aaa PUT:修改
url/aaa/1 DELETE通过请求方式的不同进行不同操作
在RequestMapping()中设置method {RequestMethod.请求方式} RequestMethod是一个枚举类型
或者直接使用
GetMapping
PostMapping
PutMapping
DeleteMapping
开发流程
根据如下流程进行开发
查看页面原型明确需求
阅读接口文档
思路分析
接口开发:开发后台业务功能(功能按接口划分)
接口测试;通过Postman进行接口测试
前后端联调:和前端工程一起进行测试
功能实现
1,部门管理
查询部门
无需分页
请求路径:/depts
请求方式:GET
//控制层
GetMapping(/depts)Result getDept();AutowiredDeptService deptService;Overridepublic Result getDept() {return Result.success(deptService.selectDept());}//业务层
ListDept selectDept();Autowiredprivate DeptMapper deptMapper;Overridepublic ListDept selectDept(){ListDept deptList deptMapper.selectDept();return deptList;}//持久层
Select(select * from dept)ListDept selectDept();删除部门
前端传递ID,后端删除对应数据
请求路径:/depts/{id}
请求方式:DELETE
//控制层
//接收路径参数
DeleteMapping(/depts/{id})Result DeleteDept(PathVariable Integer id);AutowiredDeptService deptService;Overridepublic Result DeleteDept(Integer id) {deptService.deleteDept(id);return Result.success();}//业务层
void deleteDept(Integer id);Autowiredprivate DeptMapper deptMapper;Overridepublic void deleteDept(Integer id) {deptMapper.deleteDept(id);}//持久层
Delete(delete from dept where id #{id})void deleteDept(Param(id) Integer id);新增部门
前端输入部门名称,后端创建部门保存到数据库
请求路径:/depts
请求方式:POST
//控制层
//接收JSON参数,使用RequestBody进行映射
PostMapping(/depts)Result PostDept(RequestBody Dept dept);AutowiredDeptService deptService;Overridepublic Result PostDept(RequestBody Dept dept) {deptService.insertDept(dept);return Result.success();}//业务层
void insertDept(Dept dept);Autowiredprivate DeptMapper deptMapper;Overridepublic void insertDept(Dept dept) {dept.setCreateTime(LocalDateTime.now());dept.setUpdateTime(LocalDateTime.now());deptMapper.insertDept(dept);}//持久层
Insert(insert into dept (id, name, create_time, update_time) values (null,#{name},#{createTime},#{updateTime}))void insertDept(Dept dept);修改部门
先通过selectById进行数据回填,再通过updateDept进行数据修改
//控制层PutMapping(/depts)Result PutDept(RequestBody Dept dept);GetMapping(/depts/{id})Result getDeptByID(PathVariable Integer id);AutowiredDeptService deptService;Overridepublic Result PutDept(Dept dept) {deptService.putDept(dept);return Result.success();}Overridepublic Result getDeptByID(Integer id) {return Result.success(deptService.selectDeptByID(id));}//业务层void putDept(Dept dept);Dept selectDeptByID(Integer id);Autowiredprivate DeptMapper deptMapper;Overridepublic void putDept(Dept dept) {dept.setUpdateTime(LocalDateTime.now());deptMapper.putDept(dept);}Overridepublic Dept selectDeptByID(Integer id) {return deptMapper.selectDeptByID(id);}//持久层//修改部门Update(update dept set name #{name} where id #{id})void putDept(Dept dept);//查询部门数据byIDSelect(select * from dept where id #{id})Dept selectDeptByID(Integer id);员工管理
分页查询
使用sql中的LIMIT语句
前端发送查询第几页,后端根据前端返回的页码进行计算对应显示的数据
参数传递:页码page,每页展示数pageSize
后端响应:当前页展示的数据,总共的记录数,封装到对象中以json格式数据进行响应回复
//控制层
GetMapping(/emps)
Result pageSelect(RequestParam(defaultValue 1) Integer page, RequestParam(defaultValue 10) Integer num);Override
public Result pageSelect(RequestParam(defaultValue 1) Integer page, RequestParam(defaultValue 10) Integer num) {PageBean bean empService.selectPage(page,num);return Result.success(bean);
}//服务层
PageBean selectPage(Integer page, Integer num);
//返回一个pagebean对象,封装了数据总条数和数据列表Overridepublic PageBean page(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer num) {ListEmp empList empMapper.selectEmp();ListEmp empList empMapper.selectEmi(name,gender,begin,end);return new pageBean(count,emplist);//在持久层中计算总数据条数}//持久层
Select(select * from emp limit #{index},#{num})ListEmp selectPage(Param(index) Integer index, Param(num) Integer num);分页插件
PageHelper,Mybatis的一款分页插件,简化了在Mapper层手动分页的操作,直接使用列表查询,在Service层调用mapper方法设置分页参数.在查询之后解析分页结果,最后封装到PageBean对象中返回即可.
仅在服务层中不同,可以
//服务层
PageBean selectPage(Integer page, Integer num);
//返回一个pagebean对象,封装了数据总条数和数据列表Overridepublic PageBean page(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer num) {PageHelper.startPage(page,num);
// ListEmp empList empMapper.selectEmp();ListEmp empList empMapper.selectEmi(name,gender,begin,end);PageEmp p (PageEmp) empList;PageBean pageBean new PageBean(p.getTotal(),p.getResult());return pageBean;}条件分页查询
使用动态SQL
请求路径/emps
请求方式:GET
select idselectEmi resultTypecom.example.tlias.pojo.Empselect * from empwhereif testname ! null and name ! name like concat(%,#{name},%)/ifif testgender ! nulland gender #{gender}/ifif testbegin ! null and end ! nulland entrydate between #{begin} and #{end}/if/where/select删除员工
请求路径:/emps/{ids}
请求方式:DELETE
在路径参数中传递多个id,在后端springboot中对其封装到集合中,在Mybatis中通过动态sql来完成批量删除操作
//控制层DeleteMapping(/emps/{ids})Result delete(PathVariable ListInteger ids);Overridepublic Result delete(ListInteger ids) {empService.delete(ids);return Result.success();}//业务层void delete(ListInteger id);Overridepublic void delete(ListInteger id) {empMapper.delete(id);System.out.println(id);}//持久层void delete(Param(id) ListInteger id);
delete iddeletedelete from emp where id inforeach collectioniditem itemopen (separator,close)#{item}/foreach/delete遍历集合进行sql判断