111
This commit is contained in:
1 parent
7cd697187c
commit
6c73388fdd
25 files changed
+454
-33
No files matched your search
@@ -0,0 +1,80 @@
|
||||
package com.ruoyi.framework.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.reflection.DefaultReflectorFactory;
|
||||
import org.apache.ibatis.reflection.ReflectorFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class CustomKeyGenerator implements IdentifierGenerator {
|
||||
|
||||
private final ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
|
||||
|
||||
private final AtomicLong atomicLong = new AtomicLong(System.currentTimeMillis());
|
||||
|
||||
@Override
|
||||
public Number nextId(Object entity) {
|
||||
IdType idType = getIdTypeFromEntity(entity);
|
||||
|
||||
if (idType == null || idType == IdType.AUTO) {
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (idType) {
|
||||
case ASSIGN_ID:
|
||||
return generateSnowflakeId();
|
||||
case ASSIGN_UUID:
|
||||
return null;
|
||||
case INPUT:
|
||||
return null;
|
||||
case ID_WORKER:
|
||||
return generateSnowflakeId();
|
||||
case ID_WORKER_STR:
|
||||
return generateSnowflakeId();
|
||||
case UUID:
|
||||
return null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String nextUUID(Object entity) {
|
||||
IdType idType = getIdTypeFromEntity(entity);
|
||||
|
||||
if (idType == IdType.ASSIGN_UUID || idType == IdType.UUID) {
|
||||
return java.util.UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private IdType getIdTypeFromEntity(Object entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Class<?> clazz = entity.getClass();
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
|
||||
for (Field field : fields) {
|
||||
TableId tableId = field.getAnnotation(TableId.class);
|
||||
if (tableId != null) {
|
||||
return tableId.type();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Long generateSnowflakeId() {
|
||||
return atomicLong.incrementAndGet();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.ruoyi.framework.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
||||
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
|
||||
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
|
||||
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import org.apache.ibatis.io.VFS;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
@@ -25,16 +28,14 @@ import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Mybatis支持*匹配扫描包
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Configuration
|
||||
public class MyBatisConfig {
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Autowired
|
||||
private MybatisPlusProperties mybatisPlusProperties;
|
||||
|
||||
static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
|
||||
|
||||
public static String setTypeAliasesPackage(String typeAliasesPackage) {
|
||||
@@ -45,7 +46,8 @@ public class MyBatisConfig {
|
||||
for (String aliasesPackage : typeAliasesPackage.split(",")) {
|
||||
List<String> result = new ArrayList<String>();
|
||||
aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
|
||||
+ ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN;
|
||||
+ ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/"
|
||||
+ DEFAULT_RESOURCE_PATTERN;
|
||||
Resource[] resources = resolver.getResources(aliasesPackage);
|
||||
if (resources != null && resources.length > 0) {
|
||||
MetadataReader metadataReader = null;
|
||||
@@ -53,7 +55,8 @@ public class MyBatisConfig {
|
||||
if (resource.isReadable()) {
|
||||
metadataReader = metadataReaderFactory.getMetadataReader(resource);
|
||||
try {
|
||||
result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());
|
||||
result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage()
|
||||
.getName());
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -68,7 +71,8 @@ public class MyBatisConfig {
|
||||
if (allResult.size() > 0) {
|
||||
typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0]));
|
||||
} else {
|
||||
throw new RuntimeException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");
|
||||
throw new RuntimeException(
|
||||
"mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -93,7 +97,8 @@ public class MyBatisConfig {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, MybatisPlusInterceptor paginationInterceptor) throws Exception {
|
||||
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, MybatisPlusInterceptor paginationInterceptor,
|
||||
CustomKeyGenerator customKeyGenerator) throws Exception {
|
||||
String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
|
||||
String mapperLocations = env.getProperty("mybatis.mapperLocations");
|
||||
String configLocation = env.getProperty("mybatis.configLocation");
|
||||
@@ -106,6 +111,15 @@ public class MyBatisConfig {
|
||||
sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
|
||||
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
|
||||
sessionFactory.setPlugins(paginationInterceptor);
|
||||
return sessionFactory.getObject();
|
||||
|
||||
GlobalConfig globalConfig = mybatisPlusProperties.getGlobalConfig();
|
||||
if (globalConfig == null) {
|
||||
globalConfig = new GlobalConfig();
|
||||
}
|
||||
globalConfig.setIdentifierGenerator(customKeyGenerator);
|
||||
sessionFactory.setGlobalConfig(globalConfig);
|
||||
|
||||
SqlSessionFactory sqlSessionFactory = sessionFactory.getObject();
|
||||
return sqlSessionFactory;
|
||||
}
|
||||
}
|
||||
@@ -128,7 +128,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
.antMatchers("/life/housingEstate/all-by-letter").permitAll()
|
||||
.antMatchers("/system/config/getVerifyStatus").permitAll()
|
||||
.antMatchers("/system/config/setVerifyStatus").permitAll()
|
||||
// 除上面外的所有请求全部需要鉴权认证
|
||||
.antMatchers("/ec/device-api/**").anonymous()
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.headers().frameOptions().disable();
|
||||
|
||||
Reference in new issue
Block a user