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
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
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;
}
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:
MyBatis Doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With