MyBatis配置文件开启驼峰命名映射
mybatis支持属性使用驼峰的命名,用属性是这样的
1 mapUnderscoreToCamelCase:true/false 2 <!--是否启用下划线与驼峰式命名规则的映射(如first_name => firstName)-->
我们一般在数据库中字段名使用 '_
'连接,而在实体类中使用驼峰命名。但是这样查询之后使用的驼峰命名法的是映射不到实体类上的 。
要解决这个问题只需要在mybatis配置文件中添加以下配置
配置是这样的
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <settings> 7 <setting name="mapUnderscoreToCamelCase" value="true" /> 8 </settings> 9 </configuration>
在setting中设置mapUnderscoreToCamelCase为true
,就可以实现驼峰转换了, 这个的默认是false
;
在SpringBoot 项目中没有mybatis.xml文件,可以在application.properties中,加入下面的配置项:
1 mybatis.configuration.mapUnderscoreToCamelCase=true 2 或 3 mybatis.configuration.map-underscore-to-camel-case=true
设为true表示开启驼峰转换。两种方式实验证明都可以使用。但如果同时配置的话,前者mybatis.configuration.mapUnderscoreToCamelCase的优先级更高。
SpringBoot中还可以使用自定义配置类的方式配置;给容器中添加一个ConfigurationCustomizer;
1 @Configuration 2 public class MyBatisConfig { 3 4 @Bean 5 public ConfigurationCustomizer configurationCustomizer() { 6 return new ConfigurationCustomizer() { 7 8 @Override 9 public void customize(org.apache.ibatis.session.Configuration configuration) { 10 configuration.setMapUnderscoreToCamelCase(true); 11 } 12 }; 13 } 14 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)