Skip to content

Commit

Permalink
style: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
KouShenhai committed Jul 21, 2024
1 parent 06feee6 commit 15b15a6
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
public class SpringContextUtil implements ApplicationContextAware, DisposableBean {

private final Environment environment;

@Getter
private ApplicationContext applicationContext;

/**
* 获取工厂.
*
* @return 工厂
*/
public DefaultListableBeanFactory getFactory() {
Expand All @@ -60,7 +60,6 @@ public DefaultListableBeanFactory getFactory() {

/**
* 根据名称获取Bean.
*
* @param name 名称
* @return Bean
*/
Expand All @@ -70,7 +69,6 @@ public Object getBean(String name) {

/**
* 根据名称判断Bean.
*
* @param name 名称
* @return 判断结果
*/
Expand All @@ -80,7 +78,6 @@ public boolean containsBean(String name) {

/**
* 根据名称判断单例.
*
* @param name 名称
* @return 判断结果
*/
Expand All @@ -90,9 +87,8 @@ public boolean isSingleton(String name) {

/**
* 根据类型获取Bean.
*
* @param requiredType 类型
* @param <T> 泛型
* @param <T> 泛型
* @return Bean
*/
public <T> T getBean(Class<T> requiredType) {
Expand All @@ -101,10 +97,9 @@ public <T> T getBean(Class<T> requiredType) {

/**
* 根据名称和类型获取Bean.
*
* @param name 名称
* @param name 名称
* @param requiredType 类型
* @param <T> 泛型
* @param <T> 泛型
* @return Bean
*/
public <T> T getBean(String name, Class<T> requiredType) {
Expand All @@ -113,7 +108,6 @@ public <T> T getBean(String name, Class<T> requiredType) {

/**
* 根据名称获取类.
*
* @param name 名称
* @return 类
*/
Expand All @@ -123,9 +117,8 @@ public Class<?> getType(String name) {

/**
* 根据类型获取类.
*
* @param requiredType 类型
* @param <T> 泛型
* @param <T> 泛型
* @return 类
*/
public <T> Map<String, T> getType(Class<T> requiredType) {
Expand All @@ -134,10 +127,9 @@ public <T> Map<String, T> getType(Class<T> requiredType) {

/**
* 注册Bean.
*
* @param clazz 类
* @param clazz 类
* @param beanName 名称
* @param <T> 泛型
* @param <T> 泛型
*/
public <T> void registerBean(Class<T> clazz, String beanName) {
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
Expand All @@ -146,7 +138,6 @@ public <T> void registerBean(Class<T> clazz, String beanName) {

/**
* 注销Bean.
*
* @param beanName 名称
*/
public void removeBean(String beanName) {
Expand All @@ -158,7 +149,6 @@ public void removeBean(String beanName) {

/**
* 推送事件.
*
* @param event 事件
*/
public void publishEvent(ApplicationEvent event) {
Expand All @@ -170,7 +160,8 @@ public void publishEvent(ApplicationEvent event) {
public String getAppName() {
try {
return ObjectUtil.requireNotNull(environment.getProperty("spring.application.name"));
} catch (Exception e) {
}
catch (Exception e) {
return "application";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public <TDocument> CompletableFuture<Boolean> asyncCreateIndex(String name, Stri
if (response.acknowledged()) {
log.info("索引:{} -> 创建索引成功", name);
return Boolean.TRUE;
} else {
}
else {
log.error("索引:{} -> 创建索引失败", name);
return Boolean.FALSE;
}
Expand All @@ -109,7 +110,8 @@ public <TDocument> void createIndex(String name, String alias, Class<TDocument>
boolean acknowledged = createIndexResponse.acknowledged();
if (acknowledged) {
log.info("索引:{} -> 创建索引成功", name);
} else {
}
else {
log.error("索引:{} -> 创建索引失败", name);
}
}
Expand All @@ -124,7 +126,8 @@ public void deleteIndex(List<String> names) {
boolean acknowledged = deleteIndexResponse.acknowledged();
if (acknowledged) {
log.info("索引:{} -> 删除索引成功", StringUtil.collectionToDelimitedString(names, COMMA));
} else {
}
else {
log.error("索引:{} -> 删除索引失败", StringUtil.collectionToDelimitedString(names, COMMA));
}
}
Expand All @@ -140,7 +143,8 @@ public void createDocument(String index, String id, Object obj) {
.index(idx -> idx.index(index).refresh(Refresh.True).id(id).document(obj));
if (StringUtil.isNotEmpty(response.result().jsonValue())) {
log.info("索引:{} -> 同步索引成功", index);
} else {
}
else {
log.error("索引:{} -> 同步索引失败", index);
}
}
Expand All @@ -152,7 +156,8 @@ public CompletableFuture<Boolean> asyncCreateDocument(String index, String id, O
if (StringUtil.isNotEmpty(resp.result().jsonValue())) {
log.info("索引:{} -> 异步同步索引成功", index);
return Boolean.TRUE;
} else {
}
else {
log.error("索引:{} -> 异步同步索引失败", index);
return Boolean.FALSE;
}
Expand All @@ -166,7 +171,8 @@ public void bulkCreateDocument(String index, Map<String, Object> map) {
.errors();
if (errors) {
log.error("索引:{} -> 批量同步索引失败", index);
} else {
}
else {
log.info("索引:{} -> 批量同步索引成功", index);
}
}
Expand All @@ -179,7 +185,8 @@ public CompletableFuture<Boolean> asyncBulkCreateDocument(String index, Map<Stri
if (resp.errors()) {
log.error("索引:{} -> 异步批量同步索引失败", index);
return Boolean.FALSE;
} else {
}
else {
log.info("索引:{} -> 异步批量同步索引成功", index);
return Boolean.TRUE;
}
Expand Down Expand Up @@ -220,7 +227,8 @@ private <R> void setHighlightFields(R source, Map<String, List<String>> map) {
Field field = clazz.getDeclaredField(k);
field.setAccessible(true);
ReflectionUtils.setField(field, source, v.getFirst());
} catch (NoSuchFieldException e) {
}
catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
});
Expand All @@ -231,7 +239,8 @@ private <R> void setId(R source, String id) {
Field field = source.getClass().getDeclaredField(PRIMARY_KEY);
field.setAccessible(true);
ReflectionUtils.setField(field, source, id);
} catch (Exception e) {
}
catch (Exception e) {
log.error("ID赋值失败,错误信息:{}", e.getMessage(), e);
}
}
Expand Down Expand Up @@ -276,7 +285,7 @@ private co.elastic.clients.elasticsearch.core.search.Highlight getHighlight(Sear
}

private Map<String, co.elastic.clients.elasticsearch.core.search.HighlightField> getHighlightFieldMap(
List<Search.HighlightField> fields) {
List<Search.HighlightField> fields) {
return fields.stream().collect(Collectors.toMap(Search.HighlightField::getName, j -> {
co.elastic.clients.elasticsearch.core.search.HighlightField.Builder builder = new co.elastic.clients.elasticsearch.core.search.HighlightField.Builder();
builder.fragmentSize(j.getFragmentSize());
Expand Down Expand Up @@ -426,11 +435,11 @@ private void setProperties(TypeMapping.Builder mappingBuilder, Document.Mapping
boolean eagerGlobalOrdinals = mapping.isEagerGlobalOrdinals();
switch (type) {
case TEXT -> mappingBuilder.properties(field,
fn -> fn.text(t -> t.index(true)
.fielddata(fielddata)
.eagerGlobalOrdinals(eagerGlobalOrdinals)
.searchAnalyzer(searchAnalyzer)
.analyzer(analyzer)));
fn -> fn.text(t -> t.index(true)
.fielddata(fielddata)
.eagerGlobalOrdinals(eagerGlobalOrdinals)
.searchAnalyzer(searchAnalyzer)
.analyzer(analyzer)));
case KEYWORD ->
mappingBuilder.properties(field, fn -> fn.keyword(t -> t.eagerGlobalOrdinals(eagerGlobalOrdinals)));
case LONG -> mappingBuilder.properties(field, fn -> fn.long_(t -> t));
Expand Down Expand Up @@ -484,12 +493,12 @@ private <S> Search.Field getSearchField(Field field, SearchField searchField, S
field.setAccessible(true);
String value = String.valueOf(ReflectionUtils.getField(field, obj));
return new Search.Field(Arrays.asList(names), value, searchField.type(), searchField.query(),
searchField.condition());
searchField.condition());
}

private Search.Highlight getHighlight(Highlight highlight) {
return new Search.Highlight(Arrays.asList(highlight.preTags()), Arrays.asList(highlight.postTags()),
highlight.requireFieldMatch(), getHighlightField(highlight.fields()));
highlight.requireFieldMatch(), getHighlightField(highlight.fields()));
}

private List<Search.HighlightField> getHighlightField(HighlightField[] fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
* @author laokou
*/
@AutoConfiguration
@ConditionalOnClass({DataSource.class})
@ConditionalOnClass({ DataSource.class })
@MapperScan("org.laokou.common.mybatisplus.mapper")
public class MybatisPlusAutoConfig {

Expand Down Expand Up @@ -90,14 +90,14 @@ public ConfigurationCustomizer slowSqlConfigurationCustomizer(SpringContextUtil
@Bean
@ConditionalOnMissingBean(MybatisPlusInterceptor.class)
public MybatisPlusInterceptor mybatisPlusInterceptor(MybatisPlusExtProperties mybatisPlusExtProperties,
DataSource dataSource, Executor workStealingPoolExecutor) {
DataSource dataSource, Executor workStealingPoolExecutor) {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 数据权限插件
interceptor.addInnerInterceptor(new DataFilterInterceptor());
// 多租户插件
if (mybatisPlusExtProperties.getTenant().isEnabled()) {
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(
new GlobalTenantLineHandler(mybatisPlusExtProperties.getTenant().getIgnoreTables())));
new GlobalTenantLineHandler(mybatisPlusExtProperties.getTenant().getIgnoreTables())));
}
// 动态表名插件
DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
Expand Down Expand Up @@ -145,12 +145,12 @@ public TransactionTemplate transactionTemplate(PlatformTransactionManager transa
* 异步分页. 解除每页500条限制.
*/
private AsyncPaginationInnerInterceptor asyncPaginationInnerInterceptor(DataSource dataSource,
Executor workStealingPoolExecutor) {
Executor workStealingPoolExecutor) {
// 使用postgresql,如果使用其他数据库,需要修改DbType
// 使用postgresql,如果使用其他数据库,需要修改DbType
// 使用postgresql,如果使用其他数据库,需要修改DbType
AsyncPaginationInnerInterceptor asyncPaginationInnerInterceptor = new AsyncPaginationInnerInterceptor(
DbType.POSTGRE_SQL, dataSource, workStealingPoolExecutor);
DbType.POSTGRE_SQL, dataSource, workStealingPoolExecutor);
// -1表示不受限制
asyncPaginationInnerInterceptor.setMaxLimit(-1L);
// 溢出总页数后是进行处理,查看源码就知道是干啥的
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
*/
@Slf4j
@RequiredArgsConstructor
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })
public class SqlMonitorInterceptor implements Interceptor {

private final SpringContextUtil springContextUtil;
Expand All @@ -61,7 +62,8 @@ public Object intercept(Invocation invocation) throws Throwable {
if (target instanceof StatementHandler statementHandler) {
// 替换空格、制表符、换页符
String sql = getSql(invocation, statementHandler).replaceAll("\\s+", SPACE);
springContextUtil.publishEvent(new SqlLogEvent("SQL日志", springContextUtil.getAppName(), sql, time, DateUtil.now()));
springContextUtil
.publishEvent(new SqlLogEvent("SQL日志", springContextUtil.getAppName(), sql, time, DateUtil.now()));
log.info("\nConsume Time:{} ms \nExecute SQL:{}\n", time, sql);
}
return obj;
Expand Down
Loading

0 comments on commit 15b15a6

Please sign in to comment.