按照后端的逻辑的话,我们其实是可以直接先去运行这个项目,因为我们之前完成了的登录退出功能。只要登录进来器是你就可以看到一个这样的界面。
其实刚进来这个页面你是看不到数据的,因为这样进来台管理页面它其实会做一个自动的分页查询,但是这个后端的功能我们还没有做。
我们在网页检查这个登录的动作就可以。
我们点开这个链接
可以看到它的请求链接,和携带参数。通过这个链接我们就知道如何去写Controller和接收处理数据。不过还是去看看前端的界面,明白一个大概的流程会更加方便我们去操作。
我们的后台管理界面是在index.html这里。所以我们去这个界面来找到这个请求。
index的这个界面,我们找到这个界面。
然后这个页面在member的list的list.html这里。
跟进到这里
这些都是关联的前端跟进。 ElementUI组件展示,就是前端的这些。
vue的data
展示在这里
实体类
package com.jgdabc.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.io.File;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String username;
private String name;
private String password;
private String phone;
private String sex;
private String idNumber;
private Integer status;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)
private Long createUser;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
}
@TableField 功能是让这些字段自动填充,具体在什么时候自动化填充,可以指定。
具体的填充信息 但是单单填这样的一个注解还不行,我们需要去做一个数据处理类
package com.jgdabc.common;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.mysql.cj.log.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
//自定义的源数据处理器
@Component
@Slf4j
public class MyMetaObjextHander implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
log.info("公共字段自动填充[insert]....");
log.info(metaObject.toString());
metaObject.setValue("createTime", LocalDateTime.now());
metaObject.setValue("updateTime", LocalDateTime.now());
metaObject.setValue("createUser",BaseContext.getCurrentId());
metaObject.setValue("updateUser",BaseContext.getCurrentId());
}
@Override
public void updateFill(MetaObject metaObject) {
log.info("公共字段自动填充[insert]");
log.info(metaObject.toString());
metaObject.setValue("updateTime", LocalDateTime.now());
metaObject.setValue("updateUser",BaseContext.getCurrentId());
// long id = Thread.currentThread().getId();
// log.info("当前线程的id为{}",id);
}
}
BaseContext放在后面说。
实体类定义完后,我们来看dao层,也就是mapper类。
package com.jgdabc.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jgdabc.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
// Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
@Mapper
public interface EmployMapper extends BaseMapper<Employee> {
}
service层
package com.jgdabc.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jgdabc.entity.Employee;
import org.springframework.stereotype.Service;
/**
* 除了 BaseMapper 接口,MyBatis Plus 还提供了 IService 接口,该接口对应 Service 层。MyBatis Plus 的通用 Service CRUD 实现了 IService 接口,
* 进一步封装 CRUD。为了避免与 BaseMapper 中定义的方法混淆,该接口使用 get(查询单行)、remove(删除)、list(查询集合)和 page(分页)前缀命名的方式进行区别。
*/
public interface EmployService extends IService<Employee> {
}
service的实现层
package com.jgdabc.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jgdabc.entity.Employee;
import com.jgdabc.mapper.EmployMapper;
import com.jgdabc.service.EmployService;
import org.springframework.stereotype.Service;
/*
MyBatis Plus 使用 ServiceImpl 类实现 IService 接口
*/
@Service
public class EmployServiceImpl extends ServiceImpl<EmployMapper, Employee> implements EmployService {
}
这几个都是固定的逻辑。
现在我们来看Controller的实现方法
员工信息查询
@RequestMapping("/page")
public R_<Page> page(int page,int pageSize,String name)
{
log.info("page = {},pagesize ={},name={}",page,pageSize,name);
Page pageInfo = new Page(page, pageSize);//前端传过来分页的当前码和分页的每一页的大小
log.info("pageinfo:{}",pageInfo);
// 构造分页构造器
// 条件构造器
LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();
// 添加过滤条件
lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
// 添加排序条件
lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
employService.page(pageInfo,lambdaQueryWrapper);
return R_.success(pageInfo);
需要一个分页拦截器的类
package com.jgdabc.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//Mybatis拦截器设计的一个初衷就是为了供用户在某些时候可以实现自己的逻辑而不必去动Mybatis固有的逻辑。
//配置mp的分页插件
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor()
{
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
可以注意到这个方法的泛型是Page,也就是我们最后返回的也是Page类型的pageInfo。为什么要这样返回,我们可以先看一下前端需要的数据。
records,total,我们的实体类并没有这样的字典属性。但是我们的pageInfo有。我们可以点进去看。我们点进去Page这个类。
你看这些字段就是我们需要的,所以我们要用Page这个泛型。最终的数据全部会自动封装在pageInfo里面,所以我们无需其他的处理。