mybatisplus 添加反引号
时间: 2023-11-08 11:03:01 浏览: 135
在 MybatisPlus 中添加反引号可以通过设置全局的 SQL 解析处理器实现,示例如下:
```
public class CustomSqlInjector extends DefaultSqlInjector {
@Override
public SqlNode inject(SqlNode sqlNode) {
return new MybatisPlusResolveSqlInjector(sqlNode);
}
public static class MybatisPlusResolveSqlInjector extends AbstractWrapper {
public MybatisPlusResolveSqlInjector(SqlNode sqlNode) {
super(sqlNode);
}
@Override
public void processWrapper(MappedStatement ms, Class<?> mapperClass, Method method) {
SqlSource sqlSource = ms.getSqlSource();
if (sqlSource instanceof DynamicSqlSource) {
DynamicSqlSource dynamicSqlSource = (DynamicSqlSource) sqlSource;
Field field = ReflectionUtils.findField(dynamicSqlSource.getClass(), "rootSqlNode");
ReflectionUtils.makeAccessible(field);
SqlNode rootSqlNode = (SqlNode) ReflectionUtils.getField(field, dynamicSqlSource);
this.processDynamicSql(rootSqlNode);
} else if (sqlSource instanceof RawSqlSource) {
RawSqlSource rawSqlSource = (RawSqlSource) sqlSource;
Field field = ReflectionUtils.findField(rawSqlSource.getClass(), "sqlSource");
ReflectionUtils.makeAccessible(field);
SqlNode rootSqlNode = (SqlNode) ReflectionUtils.getField(field, rawSqlSource);
this.processDynamicSql(rootSqlNode);
} else if (sqlSource instanceof ProviderSqlSource) {
ProviderSqlSource providerSqlSource = (ProviderSqlSource) sqlSource;
Field field = ReflectionUtils.findField(providerSqlSource.getClass(), "sqlProviderMethod");
ReflectionUtils.makeAccessible(field);
Method sqlProviderMethod = (Method) ReflectionUtils.getField(field, providerSqlSource);
SqlNode rootSqlNode = this.parseProviderMethod(sqlProviderMethod, mapperClass);
this.processDynamicSql(rootSqlNode);
}
}
private void processDynamicSql(SqlNode sqlNode) {
if (sqlNode instanceof TextSqlNode) {
Field field = ReflectionUtils.findField(sqlNode.getClass(), "text");
ReflectionUtils.makeAccessible(field);
String text = (String) ReflectionUtils.getField(field, sqlNode);
if (!text.contains("`")) {
ReflectionUtils.setField(field, sqlNode, "`" + text + "`");
}
} else if (sqlNode instanceof MixedSqlNode) {
MixedSqlNode mixedSqlNode = (MixedSqlNode) sqlNode;
List<SqlNode> contents = mixedSqlNode.getContents();
for (int i = 0; i < contents.size(); i++) {
SqlNode item = contents.get(i);
if (item instanceof TextSqlNode) {
Field field = ReflectionUtils.findField(item.getClass(), "text");
ReflectionUtils.makeAccessible(field);
String text = (String) ReflectionUtils.getField(field, item);
if (!text.contains("`")) {
ReflectionUtils.setField(field, item, "`" + text + "`");
}
} else if (item instanceof IfSqlNode) {
Field field = ReflectionUtils.findField(item.getClass(), "contents");
ReflectionUtils.makeAccessible(field);
SqlNode contentsNode = ((SqlNode) ReflectionUtils.getField(field, item));
this.processDynamicSql(contentsNode);
}
}
} else if (sqlNode instanceof IfSqlNode) {
Field field = ReflectionUtils.findField(sqlNode.getClass(), "contents");
ReflectionUtils.makeAccessible(field);
SqlNode contentsNode = ((SqlNode) ReflectionUtils.getField(field, sqlNode));
this.processDynamicSql(contentsNode);
} else if (sqlNode instanceof WhereSqlNode) {
Field field = ReflectionUtils.findField(sqlNode.getClass(), "contents");
ReflectionUtils.makeAccessible(field);
SqlNode contentsNode = ((SqlNode) ReflectionUtils.getField(field, sqlNode));
this.processDynamicSql(contentsNode);
}
}
}
}
```
然后在 MybatisPlus 的配置类中将此处理器设置为全局的 SQL 解析处理器即可完成添加反引号的操作,示例如下:
```
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusResolveSqlInjector mybatisPlusResolveSqlInjector() {
return new MybatisPlusResolveSqlInjector();
}
@Bean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(@Autowired DataSource dataSource,
@Autowired MybatisPlusResolveSqlInjector mybatisPlusResolveSqlInjector) {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setMapUnderscoreToCamelCase(true);
configuration.setCacheEnabled(false);
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setSqlInjector(mybatisPlusResolveSqlInjector);
bean.setGlobalConfig(globalConfig);
bean.setConfiguration(configuration);
return bean;
}
}
```
阅读全文