Unverified Commit 2da857a7 authored by Licho's avatar Licho Committed by GitHub

fix: mybatis crypt spring bean order problem (#2180)

* fix: mybatis crypt spring bean order problem Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * Spotless Apply * fix: strings library Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * fix: strings library Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * Spotless Apply --------- Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent 387ccd0a
......@@ -22,6 +22,8 @@ package org.dinky.crypto;
import org.jasypt.util.text.AES256TextEncryptor;
import org.springframework.stereotype.Component;
import com.google.common.base.Strings;
@Component
public class CryptoComponent {
......@@ -31,7 +33,7 @@ public class CryptoComponent {
public CryptoComponent(CryptoProperties cryptoProperties) {
this.enabled = cryptoProperties.getEnabled();
this.textEncryptor = new AES256TextEncryptor();
if (cryptoProperties.getEncryptionPassword() != null) {
if (!Strings.isNullOrEmpty(cryptoProperties.getEncryptionPassword())) {
this.textEncryptor.setPassword(cryptoProperties.getEncryptionPassword());
}
}
......
......@@ -19,6 +19,9 @@
package org.dinky.mybatis.crypto;
import org.dinky.context.SpringContextUtils;
import org.dinky.crypto.CryptoComponent;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultMap;
......@@ -28,6 +31,8 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
......@@ -41,6 +46,10 @@ import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
@Component
public class MybatisPlusCustomizer implements SqlSessionFactoryBeanCustomizer {
// must keep this injection, otherwise the springContextUtils and cryptoComponent will be null
@Resource private SpringContextUtils springContextUtils;
@Resource private CryptoComponent cryptoComponent;
@Override
public void customize(MybatisSqlSessionFactoryBean factoryBean) {
factoryBean.setConfiguration(
......@@ -50,49 +59,17 @@ public class MybatisPlusCustomizer implements SqlSessionFactoryBeanCustomizer {
final String ns = ms.getId().replaceAll("(.*Mapper)\\.\\w+$", "$1");
final MapperBuilderAssistant builderAssistant =
new MapperBuilderAssistant(ms.getConfiguration(), "");
builderAssistant.setCurrentNamespace(ns);
boolean[] changes = new boolean[] {false};
final List<ResultMap> collect =
ms.getResultMaps().stream()
.map(
m -> {
// 仅ResultType为Table且包含TypeHandler时,替换ResultMap为MybatisPlus生成的
TableName table =
m.getType()
.getAnnotation(TableName.class);
if (table != null) {
TableInfo tableInfo =
TableInfoHelper.getTableInfo(
m.getType());
if (tableInfo == null) {
tableInfo =
TableInfoHelper.initTableInfo(
builderAssistant,
m.getType());
}
if (tableInfo != null
&& tableInfo.getResultMap() != null
&& tableInfo.getFieldList().stream()
.anyMatch(
f ->
f
.getTypeHandler()
!= null)) {
ResultMap resultMap =
ms.getConfiguration()
.getResultMap(
tableInfo
.getResultMap());
if (resultMap != null
&& resultMap != m) {
changes[0] = true;
return resultMap;
}
}
}
return m;
})
m ->
MybatisPlusCustomizer.getResultMap(
ms, builderAssistant, changes, m))
.collect(Collectors.toList());
if (changes[0]) {
final Field field =
ReflectionUtils.findField(ms.getClass(), "resultMaps");
......@@ -106,4 +83,37 @@ public class MybatisPlusCustomizer implements SqlSessionFactoryBeanCustomizer {
}
});
}
private static ResultMap getResultMap(
MappedStatement ms,
MapperBuilderAssistant builderAssistant,
boolean[] changes,
ResultMap m) {
// 仅ResultType为Table且包含TypeHandler时,替换ResultMap为MybatisPlus生成的
if (m.getType().getAnnotation(TableName.class) == null) {
return m;
}
TableInfo tableInfo = TableInfoHelper.getTableInfo(m.getType());
if (tableInfo == null) {
tableInfo = TableInfoHelper.initTableInfo(builderAssistant, m.getType());
}
if (tableInfo == null || tableInfo.getResultMap() == null) {
return m;
}
if (tableInfo.getFieldList().stream().anyMatch(f -> f.getTypeHandler() != null)) {
ResultMap resultMap = ms.getConfiguration().getResultMap(tableInfo.getResultMap());
if (resultMap == null || resultMap == m) {
return m;
}
changes[0] = true;
return resultMap;
}
return m;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment