当前位置:网站首页>黑马瑞吉外卖之公共字段自动填充
黑马瑞吉外卖之公共字段自动填充
2022-08-11 10:59:00 【兰舟千帆】
黑马瑞吉外卖之公共字段自动填充
公共字段填充的必要性就是当我们在我们执行一些具体的操作的时候,我们需要更新字段,比如具体执行相应操作的时间,以及可能需要的更新或者修改者的id。这些我们可能需要每次都需要去填写,但是我们可以让其统一的去执行,比如我们执行修改或者插入类似的操作的时候,那么时间就会自动获取到当前时间,然后填进去,依次类似。
首先怎么做呢?先取一个实体类。对需要做公共填充的字段属性添加响应的注解。
比如这张用户表的实体类。

我们需要去使用到mybatisplus的一个接口。
MetaObjectHandler接口是mybatisPlus为我们提供的的一个扩展接口,我们可以利用这个接口在我们插入或者更新数据的时候,为一些字段指定默认值。实现这个需求的方法不止一种,在sql层面也可以做到,在建表的时候也可以指定默认值。
我们需要写一个类去实现这个接口,将类做成bean,交给spring管理
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());
}
}
基本一看就明白了,值得注意的是我们在设置uodateUser的时候,我们一同到了BaseContext.getCurrentId()。
这个BaseContext其实就是一个工具类。里面封装了 threadLocal 。
ThreadLocal 叫做本地线程变量,意思是说,ThreadLocal 中填充的的是当前线程的变量,该变量对其他线程而言是封闭且隔离的,ThreadLocal 为变量在每个线程中创建了一个副本,这样每个线程都可以访问自己内部的副本变量。

我们这样去写这个类
package com.jgdabc.common;
//基于ThreadLocal封装的工具类,用于保存获取用户id
public class BaseContext {
private static ThreadLocal<Long> threadLocal = new ThreadLocal<>();
public static void setCurrentId(Long id)
{
threadLocal.set(id);
}
public static Long getCurrentId()
{
return threadLocal.get();
}
}
我们在登录校验的的时候其实就有用到这个方法,设置了用户的id。
首先其实是我们的进行登录功能的时候,用session将id放到了session里面,首先是这样。

然后登录校验这里有对id的一个存储。

然后后面我们就可以按照这个对id进行设置。
这就是整个字段填充的过程。
边栏推荐
- VMWare中安装的win10,新增其他盘符,如:E盘,F盘等
- Latex引用图片 发现 显示的图片标号不对
- 大疆2022秋招笔试 —— 最小时间差、数组的最小偏移量
- mySQL transaction and its characteristic analysis
- php获取微信小程序码并存储到oss
- LeetCode·每日一题·1417.重新格式化字符串·模拟
- 论文笔记:《Time Series Generative Adversrial Networks》(TimeGAN,时间序列GAN)
- 本地开发好的 SAP UI5 应用部署到 ABAP 服务器时,中文字符变成乱码的原因分析和解决方案
- 二、第二章变量
- SDUT数据库 SQL语句练习(MySQL)
猜你喜欢

a sequence of consecutive positive numbers with sum s

1. 类与对象——什么是对象

【应用SLAM技术建立二维栅格化地图】

人是怎么废掉的?人是怎么变强的?

How to explain to my girlfriend what is cache penetration, cache breakdown, cache avalanche?

那些不用写代码也能做游戏的工具

和为s的连续正数序列

a-upload上传图片去掉预览icon

B端产品需求分析与优先级判断

Revelations!The former Huawei microservice expert wrote 500 pages of practical notes on the landing architecture, which has been open sourced
随机推荐
chrome设置为深色模式(包括整个网页)
Jetpack Compose学习(9)——Compose中的列表控件(LazyRow和LazyColumn)
独家采访 | 智能源于自发产生而非计划:进化论拥趸,前OpenAI研究经理、UBC大学副教授Jeff Clune
form-making notes on climbing pits (jeecg project replaces form designer)
Neural network visualization has 3 d version of the, the United States to fall!(open source)
困扰所有SAP顾问多年的问题终于解决了
【Study Notes】Unused graph theory knowledge
What areas of the deep neural network are related to the human brain neural network?
LeetCode每日一题(1754. Largest Merge Of Two Strings)
【2022】【论文笔记】基于激光直写氧化石墨烯纸的超薄THz偏转——
Flexmonster 数据透视表和图表组件
09什么是继承
Install nodejs
【应用SLAM技术建立二维栅格化地图】
安装nodejs
解决 Pocess finished with exit code 1 Class not found 和 Command line is too long. Shorten the command
漫画手绘之临摹篇
爆料!前华为微服务专家纯手打500页落地架构实战笔记,已开源
a-upload上传图片去掉预览icon
1.TCP/IP基础知识