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 会将列名和表名都使用反引号包裹起来。你可以根据自己的需要,自定义反引号的格式。

相关推荐

最新推荐

recommend-type

MybatisPlus使用手册.docx

MybatisPlus3.0使用,快速上手搭建一个Demo,附带SQL脚本,快速上手CRUD,每个方法都有例子参考,自己写的一些学习心得,仅供参考,如需了解更多功能,请参考官网
recommend-type

SpringBoot整合MyBatisPlus配置动态数据源的方法

主要介绍了SpringBoot整合MyBatisPlus配置动态数据源的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
recommend-type

SpringBoot集成MybatisPlus报错的解决方案

主要介绍了SpringBoot集成MybatisPlus报错的解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

MybatisPlus自定义Sql实现多表查询的示例

主要介绍了MybatisPlus自定义Sql实现多表查询的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

SpringBoot整合MybatisPlus的简单教程实现(简单整合)

主要介绍了SpringBoot整合MybatisPlus的简单教程实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。