feat: 初始化
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>febs-common</artifactId>
|
||||
<groupId>com.yida.data</groupId>
|
||||
<version>2.2-RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<name>FEBS-Common-DataSource-Starter</name>
|
||||
<description>通用数据库模块</description>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>febs-common-datasource-starter</artifactId>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yida.data</groupId>
|
||||
<artifactId>febs-common-core</artifactId>
|
||||
<version>${febs-cloud.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
|
||||
<version>${dynamic-datasource.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>p6spy</groupId>
|
||||
<artifactId>p6spy</artifactId>
|
||||
<version>${p6spy.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package cc.mrbird.febs.common.datasource.starter.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface DataPermission {
|
||||
|
||||
/**
|
||||
* mapper层需要数据权限过滤的方法名集合
|
||||
*
|
||||
* @return 方法名数组
|
||||
*/
|
||||
String[] methods() default {};
|
||||
|
||||
/**
|
||||
* mapper层需要数据过滤的方法名前缀,
|
||||
* 比如指定为find,表示所有以find开头的方法
|
||||
* 都会进行数据权限过滤
|
||||
*
|
||||
* @return 方法名前缀
|
||||
*/
|
||||
String methodPrefix() default "";
|
||||
|
||||
/**
|
||||
* 数据权限关联字段
|
||||
* 目前系统数据权限通过dept_id关联
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
String field() default "dept_id";
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package cc.mrbird.febs.common.datasource.starter.configure;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
@Configuration
|
||||
public class FebsDataSourceAutoConfigure {
|
||||
|
||||
/**
|
||||
* 注册数据权限
|
||||
*/
|
||||
//@Bean
|
||||
//@Order(-1)
|
||||
//public DataPermissionInterceptor dataPermissionInterceptor() {
|
||||
// return new DataPermissionInterceptor();
|
||||
//}
|
||||
|
||||
/**
|
||||
* 注册分页插件
|
||||
*/
|
||||
@Bean
|
||||
@Order(-2)
|
||||
public MybatisPlusInterceptor paginationInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
//向Mybatis过滤器链中添加分页拦截器
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
//还可以添加i他的拦截器
|
||||
return interceptor;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package cc.mrbird.febs.common.datasource.starter.configure;
|
||||
|
||||
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
|
||||
import com.yida.data.common.core.utils.DateUtil;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 自定义 p6spy sql输出格式
|
||||
*
|
||||
* @author MrBird
|
||||
*/
|
||||
public class P6spySqlFormatConfigure implements MessageFormattingStrategy {
|
||||
|
||||
@Override
|
||||
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
|
||||
return StringUtils.isNotBlank(sql) ? DateUtil.formatFullTime(LocalDateTime.now(), DateUtil.FULL_TIME_SPLIT_PATTERN)
|
||||
+ " | 耗时 " + elapsed + " ms | SQL 语句:" + StringUtils.LF + sql.replaceAll("[\\s]+", StringUtils.SPACE) + ";" : StringUtils.EMPTY;
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package cc.mrbird.febs.common.datasource.starter.handler;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.yida.data.common.core.entity.CurrentUser;
|
||||
import com.yida.data.common.core.utils.FebsUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 自动填充插入更新时间
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class FebsMetaObjectHandler implements MetaObjectHandler {
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
boolean hasCreateDate = metaObject.hasSetter("createDate");
|
||||
boolean hasCreateId = metaObject.hasSetter("createId");
|
||||
boolean hasCreateName = metaObject.hasSetter("createName");
|
||||
boolean hasCreateTime = metaObject.hasSetter("createTime");
|
||||
|
||||
Boolean isLogin = FebsUtil.isLogin();
|
||||
if (hasCreateDate) {
|
||||
Object createDate = this.getFieldValByName("createDate", metaObject);
|
||||
if (createDate == null) {
|
||||
this.setFieldValByName("createDate", LocalDateTime.now(), metaObject);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasCreateTime) {
|
||||
Object createTime = this.getFieldValByName("createTime", metaObject);
|
||||
if (createTime == null) {
|
||||
this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasCreateId && isLogin) {
|
||||
Object createId = this.getFieldValByName("createId", metaObject);
|
||||
CurrentUser currentUser = FebsUtil.getCurrentUser();
|
||||
if (createId == null && currentUser != null) {
|
||||
this.setFieldValByName("createId", currentUser.getUserId(), metaObject);
|
||||
}
|
||||
}
|
||||
if (hasCreateName && isLogin) {
|
||||
Object createName = this.getFieldValByName("createName", metaObject);
|
||||
CurrentUser currentUser = FebsUtil.getCurrentUser();
|
||||
if (createName == null && currentUser != null) {
|
||||
this.setFieldValByName("createName", currentUser.getNickname(), metaObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
boolean hasUpdateDate = metaObject.hasSetter("updateDate");
|
||||
boolean hasUpdateTime = metaObject.hasSetter("updateTime");
|
||||
boolean hasUpdateId = metaObject.hasSetter("updateId");
|
||||
Boolean isLogin = FebsUtil.isLogin();
|
||||
if (hasUpdateDate) {
|
||||
Object updateDate = this.getFieldValByName("updateDate", metaObject);
|
||||
if (updateDate == null) {
|
||||
this.setFieldValByName("updateDate", LocalDateTime.now(), metaObject);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasUpdateTime) {
|
||||
Object updateTime = this.getFieldValByName("updateTime", metaObject);
|
||||
if (updateTime == null) {
|
||||
this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasUpdateId && isLogin) {
|
||||
Object updateId = this.getFieldValByName("updateId", metaObject);
|
||||
CurrentUser currentUser = FebsUtil.getCurrentUser();
|
||||
if (updateId == null && currentUser != null) {
|
||||
this.setFieldValByName("updateId", currentUser.getUserId(), metaObject);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
# Auto Configure
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cc.mrbird.febs.common.datasource.starter.configure.FebsDataSourceAutoConfigure,\
|
||||
cc.mrbird.febs.common.datasource.starter.handler.FebsMetaObjectHandler
|
||||
Reference in New Issue
Block a user