init
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package com.newyetai.mybatisplus.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.newyetai.common.util.UserContextHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Slf4j
|
||||
//@Component
|
||||
public class AutoMetaObjectHandler implements MetaObjectHandler {
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
this.strictInsertFill(metaObject, "deleted", Boolean.class, Boolean.FALSE);
|
||||
this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
|
||||
this.strictInsertFill(metaObject, "createBy", Long.class, UserContextHolder.getLoginUserId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
this.strictUpdateFill(metaObject, "updateBy", Long.class, UserContextHolder.getLoginUserId());
|
||||
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.newyetai.mybatisplus.config;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@MapperScan(basePackages = {"com.newyetai.**.dao.**"})
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
@Bean
|
||||
public PaginationInterceptor paginationInterceptor() {
|
||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
||||
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
|
||||
// paginationInterceptor.setOverflow(false);
|
||||
// 设置最大单页限制数量,默认 500 条,-1 不受限制
|
||||
// paginationInterceptor.setLimit(500);
|
||||
// 开启 count 的 join 优化,只针对部分 left join
|
||||
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
|
||||
return paginationInterceptor;
|
||||
}
|
||||
}
|
||||
46
src/main/java/com/newyetai/mybatisplus/injector/Exists.java
Normal file
46
src/main/java/com/newyetai/mybatisplus/injector/Exists.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.newyetai.mybatisplus.injector;
|
||||
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.mapping.SqlSource;
|
||||
|
||||
import com.baomidou.mybatisplus.core.enums.SqlMethod;
|
||||
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
|
||||
public class Exists extends AbstractMethod {
|
||||
|
||||
@Override
|
||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass,
|
||||
TableInfo tableInfo) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_COUNT;
|
||||
String sql =
|
||||
String.format("<script> SELECT CASE WHEN COUNT(1) > 0 THEN 1 ELSE 0 end FROM %s %s \n</script>", tableInfo.getTableName(),
|
||||
sqlWhereEntity(true, tableInfo));
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, getMethod(sqlMethod), sqlSource, Boolean.class);
|
||||
}
|
||||
|
||||
protected String sqlWhereEntity(boolean newLine, TableInfo table) {
|
||||
|
||||
String sqlScript = table.getAllSqlWhere(false, false, ENTITY_DOT);
|
||||
|
||||
String newKeyProperty = ENTITY_DOT + table.getKeyProperty();
|
||||
String keySqlScript =
|
||||
table.getKeyColumn() + HTML_LT + HTML_GT + SqlScriptUtils.safeParam(newKeyProperty);
|
||||
sqlScript =
|
||||
SqlScriptUtils.convertIf(keySqlScript, String.format("%s != null", newKeyProperty), false)
|
||||
+ NEWLINE + sqlScript;
|
||||
|
||||
sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", ENTITY), true);
|
||||
sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE;
|
||||
return newLine ? NEWLINE + sqlScript : sqlScript;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethod(SqlMethod sqlMethod) {
|
||||
// 自定义 mapper 方法名
|
||||
return "existsEntity";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.newyetai.mybatisplus.injector;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
|
||||
import com.baomidou.mybatisplus.extension.injector.methods.AlwaysUpdateSomeColumnById;
|
||||
|
||||
@Component
|
||||
public class MySqlInjector extends DefaultSqlInjector {
|
||||
@Override
|
||||
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
|
||||
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
|
||||
/**
|
||||
* 把两个扩展内置扩展SQL注入器注入
|
||||
*/
|
||||
// methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
|
||||
methodList.add(new AlwaysUpdateSomeColumnById(i -> i.getFieldFill() != FieldFill.INSERT));
|
||||
methodList.add(new Exists());
|
||||
return methodList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.newyetai.mybatisplus.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.EnumValue;
|
||||
|
||||
public class MybatisPlusUtils {
|
||||
|
||||
/**
|
||||
* 处理枚举字段
|
||||
* @param obj
|
||||
* @return
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IllegalAccessException
|
||||
* @author sr on 2022-02-21
|
||||
*/
|
||||
public static Object dealEnumField(Object obj) throws IllegalArgumentException, IllegalAccessException {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
Class<?> clazz = obj.getClass();
|
||||
if (!clazz.isEnum()) {
|
||||
return obj;
|
||||
}
|
||||
Field f = Arrays.stream(clazz.getDeclaredFields())
|
||||
.filter(field -> field.isAnnotationPresent(EnumValue.class)).findFirst().get();
|
||||
f.setAccessible(true);
|
||||
return f.get(obj);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user