[feat] 集成mybatis-plus
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package com.ruoyi.web.core.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 注入公共字段自动填充,任选注入方式即可
|
||||
* <h2>
|
||||
* 继承使用
|
||||
* </h2>
|
||||
* <p>
|
||||
* 继承后<i>重写 {@link #getOperator()}</i> 和 <i>重新注入 Bean</i>
|
||||
* </p>
|
||||
* <pre>
|
||||
* @author yexuejc
|
||||
* @Bean
|
||||
* public MyMetaObjectHandler metaObjectHandler() {
|
||||
* return new MyMetaObjectHandler();
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public class MplusMetaObjectHandler implements MetaObjectHandler {
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
protected String crtTimeColumn = "createTime";
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
protected String crtByColumn = "createBy";
|
||||
/**
|
||||
* 最后操作时间
|
||||
*/
|
||||
protected String updateTimeColumn = "updateTime";
|
||||
/**
|
||||
* 最后操作人
|
||||
*/
|
||||
protected String updateByColumn = "updateBy";
|
||||
|
||||
/**
|
||||
* 设置操作者
|
||||
* <p>
|
||||
* 应设置登录账号为操作者,这里由每个工程具体实现
|
||||
* </p>
|
||||
* <p>
|
||||
* 返回null时,会查找consumerId作为操作者,找不到会设置默认值
|
||||
* <br/>
|
||||
* 返回"" 时,会存""
|
||||
* </p>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected String getOperator() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
Object crtTime = metaObject.getValue(crtTimeColumn);
|
||||
if (crtTime == null) {
|
||||
this.strictInsertFill(metaObject, crtTimeColumn, Date.class, new Date());
|
||||
}
|
||||
|
||||
Object crtBy = metaObject.getValue(crtByColumn);
|
||||
if (crtBy == null) {
|
||||
this.strictInsertFill(metaObject, crtByColumn, String.class, getOperator());
|
||||
}
|
||||
|
||||
Object mdfyTime = metaObject.getValue(updateTimeColumn);
|
||||
if (mdfyTime == null) {
|
||||
this.strictInsertFill(metaObject, updateTimeColumn, Date.class, new Date());
|
||||
}
|
||||
|
||||
Object mdfyBy = metaObject.getValue(updateByColumn);
|
||||
if (mdfyBy == null) {
|
||||
this.strictInsertFill(metaObject, updateByColumn, String.class, getOperator());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
Object mdfyTime = metaObject.getValue(updateTimeColumn);
|
||||
if (mdfyTime == null) {
|
||||
this.strictInsertFill(metaObject, updateTimeColumn, Date.class, new Date());
|
||||
}
|
||||
|
||||
Object mdfyBy = metaObject.getValue(updateByColumn);
|
||||
if (mdfyBy == null) {
|
||||
this.strictInsertFill(metaObject, updateByColumn, String.class, getOperator());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.ruoyi.web.core.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* MybatisPlus 配置文件
|
||||
*
|
||||
* @author maxf
|
||||
* @version 1.0
|
||||
* @ClassName MybatisPlusConfig
|
||||
* @Description
|
||||
* @date 2018/12/13 11:34
|
||||
*/
|
||||
//Spring boot方式
|
||||
@EnableTransactionManagement
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
/**
|
||||
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
//新的分页插件
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
//乐观锁插件
|
||||
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MetaObjectHandler metaObjectHandler() {
|
||||
return new MplusMetaObjectHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入sql注入器
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public DefaultSqlInjector sqlInjector() {
|
||||
return new DefaultSqlInjector();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.ruoyi.web.core.config;
|
||||
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
|
||||
/**
|
||||
* @author: yexuejc
|
||||
* @date: 2020-10-13 15:25:28
|
||||
*/
|
||||
public class RuoyiMetaObjectHandler extends MplusMetaObjectHandler {
|
||||
@Override
|
||||
protected String getOperator() {
|
||||
return SecurityUtils.getUsername();
|
||||
}
|
||||
}
|
||||
@@ -97,8 +97,8 @@ token:
|
||||
# 令牌有效期(默认30分钟)
|
||||
expireTime: 30
|
||||
|
||||
# MyBatis配置
|
||||
mybatis:
|
||||
# MyBatis配置(plus)
|
||||
mybatis-plus:
|
||||
# 搜索指定包别名
|
||||
typeAliasesPackage: com.ruoyi.**.domain
|
||||
# 配置mapper的扫描,找到所有的mapper.xml映射文件
|
||||
|
||||
Reference in New Issue
Block a user