Mybatis underscore variable problem

When mybatis obtains information from the database and maps it to the entity, it often encounters the case where the variable name is underlined. If it is not handled well, it is easy to produce "oaisAutoMappingUnknownColumnBehavior : Unknown column is detected on '*****' auto-mapping. Mapping parameters are [columnName=***_***,propertyName=***_***,propertyType=null]", the variable with underscore cannot be mapped.

The solution is as follows:

1. Set <setting name="mapUnderscoreToCamelCase" value="true"/> in the mybatis setting file to start the hump rule, so that the underlined column in the map file will be automatically mapped to the entity attribute variable according to the hump naming rule.

E.g:

map文件中 select user_id, user_name from user_info where user_id=#{id}

The corresponding variable name in the bean corresponding to the resultType should use userId and userName or write the set method according to the camel case rule

 

2. Disable the camel case rule <setting name="mapUnderscoreToCamelCase" value="false"/>, so that the underlined columnName in the map file will correspond one-to-one to the variable of the same name in the entity class.

E.g:

map文件中 select user_id, user_name from user_info where user_id=#{id}

The corresponding variable names user_id and user_name in the bean corresponding to resultType

 

3. If you don't want to disable the hump rule or modify the entity class, use resultMap in the map file. The columnName in the resultMap is not affected by the hump rule, which indirectly has the effect of disabling the hump rule.

E.g:

<resultMap id="userResultMap" type="cn.**.User">

<result property="user_id" column="user_id"/>

<result property="user_name" column="user_name"/>  

</resultMap>

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326098954&siteId=291194637