My problem is simple. I have a column name product_name in my product table in my mysql database but in my Product class (java), camelcase is used in productName. MyBatis is not mapping product_name to productName. Any solution for this? I had no problem in Hibernate before but right now I need to use mybatis for development
4 Answers
I know this is old but for those that may come across this, MyBatis supports mapping underscores to camel case
config.xml
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
...
</configuration>
Then your sql can look like:
select product_name, product_description, ...
from products
or even just
select *
from products

- 1,163
- 3
- 16
- 27
Underscore to camel case mapping can be enabled in spring-based configuration through a customizable SqlSessionFactory, like that:
@Bean
@Primary
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactory factory = sessionFactoryBuilder().build();
factory.getConfiguration().setMapUnderscoreToCamelCase(true);
// other configurations
return factory;
}

- 21,561
- 9
- 74
- 114
You have to use <resultMap>
tag in MyBatis to return the result. For example:
<resultMap id="result" type="userModel">
<result property="id" column="USER_ID"/>
</resultMap>
In the above code, in type="userModel"
userModel is defined in a config file where there is a mapping of userModel with a model java class which will have the corresponding setter/getter method for id.
For more info on this, refer the following Doc:

- 479
- 3
- 13
I had a very simple solution for this, but I do not consider it proper solution.
I added as
to match with a property of my class.
select product_name as productName from products;

- 111
- 2
- 14