feat: add mybatis plus
This commit is contained in:
20
pom.xml
20
pom.xml
@@ -20,6 +20,7 @@
|
||||
<spring-cloud.version>2021.0.9</spring-cloud.version>
|
||||
<spring-cloud-alibaba.version>2021.0.6.1</spring-cloud-alibaba.version>
|
||||
<spring-boot-admin.version>2.7.16</spring-boot-admin.version>
|
||||
<mybatis-plus.version>3.5.9</mybatis-plus.version>
|
||||
<tobato.version>1.27.2</tobato.version>
|
||||
<kaptcha.version>2.3.3</kaptcha.version>
|
||||
<pagehelper.boot.version>2.0.0</pagehelper.boot.version>
|
||||
@@ -85,6 +86,18 @@
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Mybatis-Plus 配置 -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>${mybatis-plus.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-generator</artifactId>
|
||||
<version>${mybatis-plus.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 覆盖logback的依赖配置-->
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
@@ -271,6 +284,13 @@
|
||||
<version>${wfc.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatis服务 -->
|
||||
<dependency>
|
||||
<groupId>org.wfc</groupId>
|
||||
<artifactId>wfc-common-mybatis</artifactId>
|
||||
<version>${wfc.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 系统接口 -->
|
||||
<dependency>
|
||||
<groupId>org.wfc</groupId>
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
<module>wfc-common-datascope</module>
|
||||
<module>wfc-common-datasource</module>
|
||||
<module>wfc-common-mail</module>
|
||||
<module>wfc-common-mybatis</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>wfc-common</artifactId>
|
||||
|
||||
26
wfc-common/wfc-common-mybatis/pom.xml
Normal file
26
wfc-common/wfc-common-mybatis/pom.xml
Normal file
@@ -0,0 +1,26 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.wfc</groupId>
|
||||
<artifactId>wfc-common</artifactId>
|
||||
<version>1.0.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>wfc-common-mybatis</artifactId>
|
||||
<description>
|
||||
wfc-common-mybatis服务
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.wfc</groupId>
|
||||
<artifactId>wfc-common-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.wfc.common.mybatis.handler.InjectionMetaObjectHandler
|
||||
@@ -75,6 +75,13 @@
|
||||
<groupId>org.wfc</groupId>
|
||||
<artifactId>wfc-api-omada</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MybatisPlus -->
|
||||
<dependency>
|
||||
<groupId>org.wfc</groupId>
|
||||
<artifactId>wfc-common-mybatis</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -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'
|
||||
|
||||
|
||||
@@ -75,6 +75,15 @@
|
||||
<groupId>org.wfc</groupId>
|
||||
<artifactId>wfc-common-mail</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wfc</groupId>
|
||||
<artifactId>wfc-api-omada</artifactId>
|
||||
</dependency>
|
||||
<!-- MybatisPlus -->
|
||||
<dependency>
|
||||
<groupId>org.wfc</groupId>
|
||||
<artifactId>wfc-common-mybatis</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user