MyBatis: underline not tied to stone
My problem is simple. I have a column name product_name in my product table in my mysql database, but in my Product (java) class, camelcase is used in productName. MyBatis does not match product_name with productName. Any solution for this? I had no problem in Hibernate before, but now I need to use mybatis for development
source to share
I know this is old, but for those who might run into this, MyBatis supports displaying underscores for camel case
config.xml
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
...
</configuration>
Then your sql might look like:
select product_name, product_description, ...
from products
or even just
select *
from products
source to share
Camel body binding underscore can be enabled in spring based configurations using a custom SqlSessionFactory like:
@Bean
@Primary
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactory factory = sessionFactoryBuilder().build();
factory.getConfiguration().setMapUnderscoreToCamelCase(true);
// other configurations
return factory;
}
source to share
You must use a tag <resultMap>
in MyBatis to return the result. For example:
<resultMap id="result" type="userModel">
<result property="id" column="USER_ID"/>
</resultMap>
In the above code, the type="userModel"
userModel is defined in the config file where there is a mapping from userModel to a java class that will have a corresponding setter / getter method for id. For more information on this refer to the following document:
source to share