MyBatisPlus 反引号
时间: 2023-08-14 18:22:15 浏览: 66
MyBatisPlus 中的反引号是用来包裹 SQL 查询语句中的表名、列名等标识符的。它的作用是将标识符转义,以避免与 SQL 关键字或其他特殊字符冲突,从而保证 SQL 查询语句的正确性。在 MyBatisPlus 中,可以通过在实体类的属性上使用 @TableField 注解,来指定数据库表中对应的字段名,并且自动添加反引号。例如:
```
@TableField(value = "`user_id`")
private Long userId;
```
这样,当 MyBatisPlus 自动生成 SQL 语句时,就会将 user_id 包裹在反引号中,保证 SQL 语句的正确性。
相关问题
mybatisplus 添加反引号
在 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;
}
}
```
如何自动给mybatisplus 生成的语句添加反引号
MyBatisPlus 默认会自动给表名和字段名添加反引号,以避免与 SQL 关键字冲突。如果你想要关闭该功能,可以在配置文件中设置:
```
mybatis-plus:
global-config:
db-config:
column-format: %s
```
这样设置之后,MyBatisPlus 就不会自动添加反引号了,而是使用 `%s` 占位符来表示列名和表名。
如果你想要自定义反引号的格式,可以在配置文件中设置:
```
mybatis-plus:
global-config:
db-config:
column-format: "`%s`"
table-format: "`%s`"
```
这样设置之后,MyBatisPlus 会将列名和表名都使用反引号包裹起来。你可以根据自己的需要,自定义反引号的格式。