SpringBoot 1.5.2中MyBatis驼峰命名转换失效问题求助
Hey there, let's figure out why your Map results aren't getting underscore-to-camel-case conversion despite setting mybatis.configuration.map-underscore-to-camel-case=true in your Spring Boot 1.5.2 + mybatis-spring-boot-starter 1.3.2 setup. Here are the most common fixes and checks to try:
1. 确认配置是否真的生效
First, let's verify if the configuration is being loaded correctly—sometimes typos or context issues can prevent the setting from taking effect:
- Inject the
SqlSessionFactoryinto a test or component and check the configuration value directly:
If@Autowired private SqlSessionFactory sqlSessionFactory; public void checkMyBatisConfig() { Configuration config = sqlSessionFactory.getConfiguration(); System.out.println("mapUnderscoreToCamelCase: " + config.isMapUnderscoreToCamelCase()); System.out.println("useActualColumnNames: " + config.isUseActualColumnNames()); }mapUnderscoreToCamelCasereturnsfalse, double-check your config file:- For
.properties: Ensure the line is exactlymybatis.configuration.map-underscore-to-camel-case=true - For
.yml: Indentation matters! Make sure it's nested under the correctmybatisblock:mybatis: configuration: map-underscore-to-camel-case: true
- For
2. 检查useActualColumnNames配置
In MyBatis 3.4.x (which ships with your starter version), the useActualColumnNames setting can override camel case conversion for Map results. If this is set to true, MyBatis will use raw database column names instead of converting them. Add this to your config to disable it:
- Properties:
mybatis.configuration.use-actual-column-names=false - YAML:
mybatis: configuration: use-actual-column-names: false
3. 确保Mapper方法返回的是正确的Map类型
If your Mapper method returns Map<String, Object>, avoid using @MapKey in a way that overrides conversion. Also, check if your SQL query uses hardcoded column aliases that bypass the config—though the setting should handle raw underscore column names automatically.
If you're using an explicit result map, don't hardcode non-camel-case property names for Map types. For example, skip entries like this:
<resultMap id="userMap" type="java.util.Map"> <result column="user_name" property="user_name"/> <!-- This skips camel case conversion --> </resultMap>
Let MyBatis handle the conversion by omitting explicit result entries for Map returns.
4. 升级MyBatis Starter版本(可选)
The 1.3.2 version of mybatis-spring-boot-starter is quite outdated. If the above fixes don't work, consider upgrading to a newer compatible version (e.g., 1.3.5, the latest in the 1.x line for Spring Boot 1.5.x). Newer versions often include bug fixes related to configuration propagation.
5. 手动转换(临时 workaround)
If you need a quick fix while debugging, you can manually convert Map keys to camel case in your service layer. Here's a utility method to do that:
public static Map<String, Object> convertToCamelCase(Map<String, Object> underscoreMap) { Map<String, Object> camelCaseMap = new HashMap<>(); for (Map.Entry<String, Object> entry : underscoreMap.entrySet()) { String[] keyParts = entry.getKey().split("_"); StringBuilder camelCaseKey = new StringBuilder(); for (int i = 0; i < keyParts.length; i++) { String part = keyParts[i]; if (i == 0) { camelCaseKey.append(part.toLowerCase()); } else { camelCaseKey.append(part.substring(0, 1).toUpperCase()) .append(part.substring(1).toLowerCase()); } } camelCaseMap.put(camelCaseKey.toString(), entry.getValue()); } return camelCaseMap; }
Hopefully one of these solutions gets your camel case conversion working for Map results!
内容的提问来源于stack exchange,提问作者贺小五

