Introducing new features. closed #I44EYC 使用mybatis-plus 的自动填充策略重构 审计字段

This commit is contained in:
wjie 2021-08-09 15:51:37 +08:00
parent d879b41461
commit 1c8a41506b
29 changed files with 196 additions and 140 deletions

View File

@ -52,6 +52,8 @@ public class PigAuthenticationFailureEvenHandler extends AbstractAuthenticationF
Long startTime = System.currentTimeMillis();
Long endTime = System.currentTimeMillis();
logVo.setTime(endTime - startTime);
logVo.setCreateBy(authentication.getName());
logVo.setUpdateBy(authentication.getName());
SpringContextHolder.publishEvent(new SysLogEvent(logVo));
}

View File

@ -48,6 +48,8 @@ public class PigAuthenticationSuccessEventHandler extends AbstractAuthentication
Long startTime = System.currentTimeMillis();
Long endTime = System.currentTimeMillis();
logVo.setTime(endTime - startTime);
logVo.setCreateBy(authentication.getName());
logVo.setUpdateBy(authentication.getName());
SpringContextHolder.publishEvent(new SysLogEvent(logVo));
}

View File

@ -57,6 +57,8 @@ public class PigLogoutSuccessEventHandler extends AbstractLogoutSuccessEventHand
OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication;
logVo.setServiceId(auth2Authentication.getOAuth2Request().getClientId());
}
logVo.setCreateBy(authentication.getName());
logVo.setUpdateBy(authentication.getName());
SpringContextHolder.publishEvent(new SysLogEvent(logVo));
}

View File

@ -47,12 +47,6 @@
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!--mybatis plus extension,包含了mybatis plus core-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!--hibernate-validator-->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -16,13 +16,10 @@
package com.pig4cloud.pig.common.feign.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import java.lang.annotation.*;
/**
* @author lengleng

View File

@ -16,11 +16,6 @@
package com.pig4cloud.pig.common.feign.ext;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
import com.alibaba.cloud.sentinel.feign.SentinelContractHolder;
import feign.Contract;
import feign.Feign;
@ -36,6 +31,11 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
/**
* 支持自动降级注入 重写 {@link com.alibaba.cloud.sentinel.feign.SentinelFeign}
*

View File

@ -46,6 +46,7 @@ public class SysLogUtils {
.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
SysLog sysLog = new SysLog();
sysLog.setCreateBy(Objects.requireNonNull(getUsername()));
sysLog.setUpdateBy(Objects.requireNonNull(getUsername()));
sysLog.setType(LogTypeEnum.NORMAL.getType());
sysLog.setRemoteAddr(ServletUtil.getClientIP(request));
sysLog.setRequestUri(URLUtil.getPath(request.getRequestURI()));
@ -89,4 +90,4 @@ public class SysLogUtils {
return authentication.getName();
}
}
}

View File

@ -58,5 +58,10 @@
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!--security 依赖-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -17,8 +17,10 @@
package com.pig4cloud.pig.common.mybatis;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.pig4cloud.pig.common.mybatis.config.MybatisPlusMetaObjectHandler;
import com.pig4cloud.pig.common.mybatis.resolver.SqlFilterArgumentResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -55,4 +57,13 @@ public class MybatisAutoConfiguration implements WebMvcConfigurer {
return interceptor;
}
/**
* 审计字段自动填充
* @return {@link MetaObjectHandler}
*/
@Bean
public MybatisPlusMetaObjectHandler mybatisPlusMetaObjectHandler() {
return new MybatisPlusMetaObjectHandler();
}
}

View File

@ -0,0 +1,45 @@
package com.pig4cloud.pig.common.mybatis.base;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 抽象实体
*
* @author lengleng
* @date 2021/8/9
*/
@Getter
@Setter
public class BaseEntity implements Serializable {
/**
* 创建者
*/
@TableField(fill = FieldFill.INSERT)
private String createBy;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 更新者
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private String updateBy;
/**
* 更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
}

View File

@ -0,0 +1,77 @@
package com.pig4cloud.pig.common.mybatis.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.ClassUtils;
import java.time.LocalDateTime;
import java.util.Optional;
/**
* MybatisPlus 自动填充配置
*
* @author L.cm
*/
@Slf4j
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
log.debug("mybatis plus start insert fill ....");
LocalDateTime now = LocalDateTime.now();
fillValIfNullByName("createTime", now, metaObject, false);
fillValIfNullByName("updateTime", now, metaObject, false);
fillValIfNullByName("createBy", getUserName(), metaObject, false);
fillValIfNullByName("updateBy", getUserName(), metaObject, false);
}
@Override
public void updateFill(MetaObject metaObject) {
log.debug("mybatis plus start update fill ....");
fillValIfNullByName("updateTime", LocalDateTime.now(), metaObject, true);
fillValIfNullByName("updateBy", getUserName(), metaObject, true);
}
/**
* 填充值先判断是否有手动设置优先手动设置的值例如job必须手动设置
*
* @param fieldName 属性名
* @param fieldVal 属性值
* @param metaObject MetaObject
* @param isCover 是否覆盖原有值,避免更新操作手动入参
*/
private static void fillValIfNullByName(String fieldName, Object fieldVal, MetaObject metaObject, boolean isCover) {
// 1. 没有 get 方法
if (!metaObject.hasSetter(fieldName)) {
return;
}
// 2. 如果用户有手动设置的值
Object userSetValue = metaObject.getValue(fieldName);
if (userSetValue != null && !isCover) {
return;
}
// 3. field 类型相同时设置
Class<?> getterType = metaObject.getGetterType(fieldName);
if (ClassUtils.isAssignableValue(getterType, fieldVal)) {
metaObject.setValue(fieldName, fieldVal);
}
}
/**
* 获取 spring security 当前的用户名
*
* @return 当前用户名
*/
private String getUserName() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (Optional.ofNullable(authentication).isPresent()) {
return authentication.getName();
}
return null;
}
}

View File

@ -18,7 +18,6 @@ package com.pig4cloud.pig.common.security.handler;
import cn.hutool.core.collection.CollUtil;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.authentication.event.LogoutSuccessEvent;
import org.springframework.security.core.Authentication;

View File

@ -24,7 +24,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @author lengleng

View File

@ -46,5 +46,10 @@
<groupId>com.pig4cloud</groupId>
<artifactId>pig-common-feign</artifactId>
</dependency>
<!--mybatis 依赖-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pig-common-mybatis</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -19,13 +19,12 @@ package com.pig4cloud.pig.admin.api.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.pig4cloud.pig.common.mybatis.base.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
/**
* <p>
@ -37,7 +36,7 @@ import java.time.LocalDateTime;
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class SysDept extends Model<SysDept> {
public class SysDept extends BaseEntity {
private static final long serialVersionUID = 1L;
@ -58,18 +57,6 @@ public class SysDept extends Model<SysDept> {
@ApiModelProperty(value = "排序值")
private Integer sort;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间")
private LocalDateTime createTime;
/**
* 修改时间
*/
@ApiModelProperty(value = "修改时间")
private LocalDateTime updateTime;
/**
* 父级部门id
*/

View File

@ -18,14 +18,12 @@ package com.pig4cloud.pig.admin.api.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.pig4cloud.pig.common.mybatis.base.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.time.LocalDateTime;
/**
* 字典表
*
@ -35,7 +33,7 @@ import java.time.LocalDateTime;
@Data
@ApiModel(value = "字典类型")
@EqualsAndHashCode(callSuper = true)
public class SysDict extends Model<SysDict> {
public class SysDict extends BaseEntity {
private static final long serialVersionUID = 1L;
@ -58,18 +56,6 @@ public class SysDict extends Model<SysDict> {
@ApiModelProperty(value = "字典描述")
private String description;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间")
private LocalDateTime createTime;
/**
* 更新时间
*/
@ApiModelProperty(value = "更新时间")
private LocalDateTime updateTime;
/**
* 是否是系统内置
*/

View File

@ -17,14 +17,12 @@ package com.pig4cloud.pig.admin.api.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.pig4cloud.pig.common.mybatis.base.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.time.LocalDateTime;
/**
* 字典项
*
@ -34,7 +32,7 @@ import java.time.LocalDateTime;
@Data
@ApiModel(value = "字典项")
@EqualsAndHashCode(callSuper = true)
public class SysDictItem extends Model<SysDictItem> {
public class SysDictItem extends BaseEntity {
private static final long serialVersionUID = 1L;
@ -81,18 +79,6 @@ public class SysDictItem extends Model<SysDictItem> {
@ApiModelProperty(value = "排序值,默认升序")
private Integer sort;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间")
private LocalDateTime createTime;
/**
* 更新时间
*/
@ApiModelProperty(value = "更新时间")
private LocalDateTime updateTime;
/**
* 备注信息
*/

View File

@ -21,12 +21,11 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.pig4cloud.pig.common.mybatis.base.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* <p>
@ -37,7 +36,7 @@ import java.time.LocalDateTime;
* @since 2019/2/1
*/
@Data
public class SysLog implements Serializable {
public class SysLog extends BaseEntity {
private static final long serialVersionUID = 1L;
@ -63,24 +62,6 @@ public class SysLog implements Serializable {
@ApiModelProperty(value = "日志标题")
private String title;
/**
* 创建者
*/
@ApiModelProperty(value = "创建人")
private String createBy;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间")
private LocalDateTime createTime;
/**
* 更新时间
*/
@ApiModelProperty(value = "更新时间")
private LocalDateTime updateTime;
/**
* 操作IP地址
*/

View File

@ -19,14 +19,13 @@ package com.pig4cloud.pig.admin.api.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.pig4cloud.pig.common.mybatis.base.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
/**
* <p>
@ -38,7 +37,7 @@ import java.time.LocalDateTime;
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class SysMenu extends Model<SysMenu> {
public class SysMenu extends BaseEntity {
private static final long serialVersionUID = 1L;
@ -99,18 +98,6 @@ public class SysMenu extends Model<SysMenu> {
@ApiModelProperty(value = "路由缓冲")
private String keepAlive;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间")
private LocalDateTime createTime;
/**
* 更新时间
*/
@ApiModelProperty(value = "更新时间")
private LocalDateTime updateTime;
/**
* 0--正常 1--删除
*/

View File

@ -18,7 +18,7 @@ package com.pig4cloud.pig.admin.api.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.pig4cloud.pig.common.mybatis.base.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -35,7 +35,7 @@ import javax.validation.constraints.NotBlank;
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class SysOauthClientDetails extends Model<SysOauthClientDetails> {
public class SysOauthClientDetails extends BaseEntity {
private static final long serialVersionUID = 1L;

View File

@ -19,13 +19,12 @@ package com.pig4cloud.pig.admin.api.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.pig4cloud.pig.common.mybatis.base.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
/**
* <p>
@ -37,7 +36,7 @@ import java.time.LocalDateTime;
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class SysRole extends Model<SysRole> {
public class SysRole extends BaseEntity {
private static final long serialVersionUID = 1L;
@ -57,12 +56,6 @@ public class SysRole extends Model<SysRole> {
@ApiModelProperty(value = "角色描述")
private String roleDesc;
@ApiModelProperty(value = "创建时间")
private LocalDateTime createTime;
@ApiModelProperty(value = "修改时间")
private LocalDateTime updateTime;
/**
* 删除标识0-正常,1-删除
*/

View File

@ -20,12 +20,10 @@ import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.pig4cloud.pig.common.mybatis.base.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* <p>
* 用户表
@ -35,7 +33,7 @@ import java.time.LocalDateTime;
* @since 2019/2/1
*/
@Data
public class SysUser implements Serializable {
public class SysUser extends BaseEntity {
private static final long serialVersionUID = 1L;
@ -65,18 +63,6 @@ public class SysUser implements Serializable {
@ApiModelProperty(value = "随机盐")
private String salt;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间")
private LocalDateTime createTime;
/**
* 修改时间
*/
@ApiModelProperty(value = "修改时间")
private LocalDateTime updateTime;
/**
* 锁定标记
*/

View File

@ -23,7 +23,6 @@ import com.pig4cloud.pig.admin.api.dto.UserDTO;
import com.pig4cloud.pig.admin.api.dto.UserInfo;
import com.pig4cloud.pig.admin.api.entity.SysUser;
import com.pig4cloud.pig.admin.api.vo.UserVO;
import com.pig4cloud.pig.common.core.util.R;
import java.util.List;

View File

@ -147,9 +147,17 @@ public class CodeGenKits {
tableEntity.setLowerClassName(StringUtils.uncapitalize(className));
// 获取需要在swagger文档中隐藏的属性字段
List<Object> hiddenColumns = config.getList("hiddenColumn");
// 排除不需要处理的字段
List<Object> excludeColumns = config.getList("excludeColumn");
// 列信息
List<ColumnEntity> columnList = new ArrayList<>();
for (Map<String, String> column : columns) {
// 判断是否处理
String columnName = column.get("columnName");
if (excludeColumns.contains(columnName)) {
continue;
}
ColumnEntity columnEntity = new ColumnEntity();
columnEntity.setColumnName(column.get("columnName"));
columnEntity.setDataType(column.get("dataType"));

View File

@ -26,6 +26,9 @@ author=pig code generator
#\u8868\u524D\u7F00(\u7C7B\u540D\u4E0D\u4F1A\u5305\u542B\u8868\u524D\u7F00)
tablePrefix=table_
#\u6392\u9664\u4E0D\u751F\u6210\u7684\u5B57\u6BB5
excludeColumns=createTime,updateTime,createBy,updateBy
#\u7C7B\u578B\u8F6C\u6362\uFF0C\u914D\u7F6E\u4FE1\u606F
tinyint=Integer
smallint=Integer

View File

@ -19,7 +19,7 @@ package ${package}.${moduleName}.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.pig4cloud.pig.common.mybatis.base.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ -40,7 +40,7 @@ import java.time.LocalDateTime;
@TableName("${tableName}")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "${comments}")
public class ${className} extends Model<${className}> {
public class ${className} extends BaseEntity {
private static final long serialVersionUID = 1L;
#foreach ($column in $columns)

View File

@ -37,8 +37,8 @@
</template>
<script>
import {fetchList, getObj, addObj, putObj, delObj} from '@/api/${pathName}'
import {tableOption} from '@/const/crud/${pathName}'
import {addObj, delObj, fetchList, putObj} from '@/api/'
import {tableOption} from '@/const/crud/'
import {mapGetters} from 'vuex'
export default {

View File

@ -20,7 +20,7 @@
</template>
<script>
import {getObj, addObj, putObj} from '@/api/${pathName}'
import {addObj, getObj, putObj} from '@/api/'
export default {
data () {

View File

@ -51,10 +51,11 @@
</template>
<script>
import {fetchList, delObj} from '@/api/${pathName}'
import TableForm from './${pathName}-form'
import {mapGetters} from 'vuex'
export default {
import {delObj, fetchList} from '@/api/'
import TableForm from './'
import {mapGetters} from 'vuex'
export default {
data () {
return {
dataForm: {