update 修改MybatisPlus的配置:

1. 使用自定义主键生成策略
  2. 新增自动填充字段,createTime,updateTime,createUserId,updateUserId,createUsername,updateUsername,loginIp
master
管理员 1 year ago
parent 7835a5d6de
commit 6cb2f3c153

@ -1,12 +1,15 @@
package com.ruoyi.framework.config;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.ruoyi.common.utils.IdUtils;
import com.ruoyi.framework.handler.CreateAndUpdateMetaObjectHandler;
import com.ruoyi.framework.interceptor.PlusDataPermissionInterceptor;
import org.mybatis.spring.annotation.MapperScan;
@ -74,11 +77,40 @@ public class MybatisPlusConfig {
* 使
* ID
*/
// @Bean
// public IdentifierGenerator idGenerator() {
// return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
// }
/**
* 使id
* @return
*/
@Bean
public IdentifierGenerator idGenerator() {
return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
return new IdentifierGenerator() {
@Override
public boolean assignId(Object idValue) {
if (idValue instanceof CharSequence) {
return StrUtil.isBlank((CharSequence) idValue);
}
return ObjUtil.isNull(idValue);
}
@Override
public Number nextId(Object entity) {
return IdUtils.nextDateIdLong(entity.getClass().getSimpleName(), 17);
}
@Override
public String nextUUID(Object entity) {
return IdUtils.nextDateId(entity.getClass().getSimpleName(), 17);
}
};
}
/**
* PaginationInnerInterceptor
* https://baomidou.com/pages/97710a/

@ -1,5 +1,7 @@
package com.ruoyi.framework.handler;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
@ -11,6 +13,7 @@ import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import java.beans.PropertyDescriptor;
import java.util.Date;
/**
@ -38,11 +41,25 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
// 当前已登录 且 更新人为空 则填充
baseEntity.setUpdateBy(username);
}
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
try {
LoginUser loginUser = LoginHelper.getLoginUser();
this.strictInsertFill(metaObject, "createUserId", Long.class, loginUser.getUserId());
this.strictInsertFill(metaObject, "createUsername", String.class, loginUser.getUsername());
this.strictInsertFill(metaObject, "loginIp", String.class, loginUser.getIpaddr());
} catch (Exception e) {
log.debug("自动注入警告 => 用户未登录");
}
} catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
}
}
@Override
public void updateFill(MetaObject metaObject) {
try {
@ -57,6 +74,17 @@ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
baseEntity.setUpdateBy(username);
}
}
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
try {
LoginUser loginUser = LoginHelper.getLoginUser();
this.strictUpdateFill(metaObject, "updateUserId", Long.class, loginUser.getUserId());
this.strictUpdateFill(metaObject, "updateUsername", String.class, loginUser.getUsername());
} catch (Exception e) {
log.debug("自动注入警告 => 用户未登录");
}
} catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
}

Loading…
Cancel
Save