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

3 answers

I know this is old, but for those who might run into this, MyBatis supports displaying underscores for camel case


        <setting name="mapUnderscoreToCamelCase" value="true"/>


Then your sql might look like:

select product_name, product_description, ...
from products


or even just

select *
from products




Camel body binding underscore can be enabled in spring based configurations using a custom SqlSessionFactory like:

public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactory factory = sessionFactoryBuilder().build();
    // other configurations
    return factory;




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"/>


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:

MyBatis Doc



All Articles