diff --git a/pom.xml b/pom.xml index 90e8c1e..0c7da64 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,7 @@ 2021.0.9 2021.0.6.1 2.7.16 + 3.5.9 1.27.2 2.3.3 2.0.0 @@ -85,6 +86,18 @@ import + + + com.baomidou + mybatis-plus-boot-starter + ${mybatis-plus.version} + + + com.baomidou + mybatis-plus-generator + ${mybatis-plus.version} + + ch.qos.logback @@ -271,6 +284,13 @@ ${wfc.version} + + + org.wfc + wfc-common-mybatis + ${wfc.version} + + org.wfc diff --git a/wfc-common/pom.xml b/wfc-common/pom.xml index 7bc9ce6..94b52ad 100644 --- a/wfc-common/pom.xml +++ b/wfc-common/pom.xml @@ -19,6 +19,7 @@ wfc-common-datascope wfc-common-datasource wfc-common-mail + wfc-common-mybatis wfc-common diff --git a/wfc-common/wfc-common-mybatis/pom.xml b/wfc-common/wfc-common-mybatis/pom.xml new file mode 100644 index 0000000..991cfa5 --- /dev/null +++ b/wfc-common/wfc-common-mybatis/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + + org.wfc + wfc-common + 1.0.2 + + + wfc-common-mybatis + + wfc-common-mybatis服务 + + + + + + org.wfc + wfc-common-security + + + com.baomidou + mybatis-plus-boot-starter + + + diff --git a/wfc-common/wfc-common-mybatis/src/main/java/org/wfc/common/mybatis/domain/BaseData.java b/wfc-common/wfc-common-mybatis/src/main/java/org/wfc/common/mybatis/domain/BaseData.java new file mode 100644 index 0000000..97c4897 --- /dev/null +++ b/wfc-common/wfc-common-mybatis/src/main/java/org/wfc/common/mybatis/domain/BaseData.java @@ -0,0 +1,39 @@ +package org.wfc.common.mybatis.domain; + +import com.baomidou.mybatisplus.annotation.FieldFill; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +/** + * @description: 基类 + * @author: cyc + * @since: 2024-12-09 + */ +@Data +public class BaseData implements Serializable { + + private static final long serialVersionUID = 1L; + + @TableId(value = "id", type = IdType.AUTO) + private Long id; + + private Boolean delFlag; + + @TableField(fill = FieldFill.INSERT) + private Long createBy; + + @TableField(fill = FieldFill.INSERT) + private Date createTime; + + @TableField(fill = FieldFill.INSERT_UPDATE) + private Long updateBy; + + @TableField(fill = FieldFill.INSERT_UPDATE) + private Date updateTime; + +} diff --git a/wfc-common/wfc-common-mybatis/src/main/java/org/wfc/common/mybatis/handler/InjectionMetaObjectHandler.java b/wfc-common/wfc-common-mybatis/src/main/java/org/wfc/common/mybatis/handler/InjectionMetaObjectHandler.java new file mode 100644 index 0000000..958af74 --- /dev/null +++ b/wfc-common/wfc-common-mybatis/src/main/java/org/wfc/common/mybatis/handler/InjectionMetaObjectHandler.java @@ -0,0 +1,98 @@ +package org.wfc.common.mybatis.handler; + +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.http.HttpStatus; +import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.reflection.MetaObject; +import org.springframework.stereotype.Component; +import org.wfc.common.core.domain.LoginUser; +import org.wfc.common.core.exception.ServiceException; +import org.wfc.common.mybatis.domain.BaseData; +import org.wfc.common.security.utils.SecurityUtils; + +import java.util.Date; + +@Slf4j +@Component +public class InjectionMetaObjectHandler implements MetaObjectHandler { + + /** + * 插入填充方法,用于在插入数据时自动填充实体对象中的创建时间、更新时间、创建人、更新人等信息 + * + * @param metaObject 元对象,用于获取原始对象并进行填充 + */ + @Override + public void insertFill(MetaObject metaObject) { + try { + if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseData) { + BaseData baseData = (BaseData) metaObject.getOriginalObject(); + // 获取当前时间作为创建时间和更新时间,如果创建时间不为空,则使用创建时间,否则使用当前时间 + Date current = ObjectUtil.isNotNull(baseData.getCreateTime()) + ? baseData.getCreateTime() : new Date(); + baseData.setCreateTime(current); + baseData.setUpdateTime(current); + + // 如果创建人为空,则填充当前登录用户的信息 + if (ObjectUtil.isNull(baseData.getCreateBy())) { + LoginUser loginUser = getLoginUser(); + if (ObjectUtil.isNotNull(loginUser)) { + Long userId = loginUser.getUserid(); + // 填充创建人、更新人和创建部门信息 + baseData.setCreateBy(userId); + baseData.setUpdateBy(userId); + } + } + } else { + Date date = new Date(); + this.strictInsertFill(metaObject, "createTime", Date.class, date); + this.strictInsertFill(metaObject, "updateTime", Date.class, date); + } + } catch (Exception e) { + throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED); + } + } + + /** + * 更新填充方法,用于在更新数据时自动填充实体对象中的更新时间和更新人信息 + * + * @param metaObject 元对象,用于获取原始对象并进行填充 + */ + @Override + public void updateFill(MetaObject metaObject) { + try { + if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseData) { + BaseData baseData = (BaseData) metaObject.getOriginalObject(); + // 获取当前时间作为更新时间,无论原始对象中的更新时间是否为空都填充 + Date current = new Date(); + baseData.setUpdateTime(current); + + // 获取当前登录用户的ID,并填充更新人信息 + LoginUser loginUser = getLoginUser(); + if (ObjectUtil.isNotNull(loginUser)) { + baseData.setUpdateBy(loginUser.getUserid()); + } + } else { + this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date()); + } + } catch (Exception e) { + throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED); + } + } + + /** + * 获取当前登录用户信息 + * + * @return 当前登录用户的信息,如果用户未登录则返回 null + */ + private LoginUser getLoginUser() { + LoginUser loginUser; + try { + loginUser = SecurityUtils.getLoginUser(); + } catch (Exception e) { + log.warn("自动注入警告 => 用户未登录"); + return null; + } + return loginUser; + } +} \ No newline at end of file diff --git a/wfc-common/wfc-common-mybatis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/wfc-common/wfc-common-mybatis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..8a0d6ca --- /dev/null +++ b/wfc-common/wfc-common-mybatis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +org.wfc.common.mybatis.handler.InjectionMetaObjectHandler \ No newline at end of file diff --git a/wfc-modules/wfc-modules-user/pom.xml b/wfc-modules/wfc-modules-user/pom.xml index 97d5ad3..753997c 100644 --- a/wfc-modules/wfc-modules-user/pom.xml +++ b/wfc-modules/wfc-modules-user/pom.xml @@ -75,6 +75,13 @@ org.wfc wfc-api-omada + + + + org.wfc + wfc-common-mybatis + + diff --git a/wfc-modules/wfc-modules-user/src/main/resources/application.yml b/wfc-modules/wfc-modules-user/src/main/resources/application.yml index 71acad5..91d8cda 100644 --- a/wfc-modules/wfc-modules-user/src/main/resources/application.yml +++ b/wfc-modules/wfc-modules-user/src/main/resources/application.yml @@ -42,12 +42,12 @@ spring: # url: # driver-class-name: -# mybatis配置 -mybatis: +# mybatis-plus配置 +mybatis-plus: # 搜索指定包别名 - typeAliasesPackage: org.wfc.user + type-aliases-package: org.wfc.user # 配置mapper的扫描,找到所有的mapper.xml映射文件 - mapperLocations: classpath:mapper/**/*.xml + mapper-locations: classpath:mapper/**/*.xml # swagger配置 swagger: @@ -61,3 +61,4 @@ omada: omadac-id: 'f3aa6e479b94222581523710cc2c2a9d' client-id: '5036e77c81a74008821c694a715fe2b8' client-secret: '29faa06fb7f244b094377b48eb3083a7' + diff --git a/wfc-modules/wfc-system/pom.xml b/wfc-modules/wfc-system/pom.xml index 6bc5380..4e046a2 100644 --- a/wfc-modules/wfc-system/pom.xml +++ b/wfc-modules/wfc-system/pom.xml @@ -75,6 +75,15 @@ org.wfc wfc-common-mail + + org.wfc + wfc-api-omada + + + + org.wfc + wfc-common-mybatis + diff --git a/wfc-modules/wfc-system/src/main/resources/application.yml b/wfc-modules/wfc-system/src/main/resources/application.yml index 7460c4d..36b2336 100644 --- a/wfc-modules/wfc-system/src/main/resources/application.yml +++ b/wfc-modules/wfc-system/src/main/resources/application.yml @@ -42,12 +42,12 @@ spring: # url: # driver-class-name: -# mybatis配置 -mybatis: +# mybatis-plus配置 +mybatis-plus: # 搜索指定包别名 - typeAliasesPackage: org.wfc.system + type-aliases-package: org.wfc.system # 配置mapper的扫描,找到所有的mapper.xml映射文件 - mapperLocations: classpath:mapper/**/*.xml + mapper-locations: classpath:mapper/**/*.xml # swagger配置 swagger: