What should I do if the attribute name in the entity class in Mybatis is different from the field name in the table?

solution:

1) Alias ​​when writing SQL statements, for example:

Change last_name to lastName

Change dept_id to deptId

select id,last_name lastName,email,salary,dept_id deptId 
from ...
where ...

 

2) Open the camel case naming rule in the global configuration file of MyBatis

<configuration>

​		<settings>

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

​		</settings>

​		.......

</configuration>

 

3) Use resultMap in the Mapper mapping file to customize the mapping rules

<select id="getEmployeeById" resultMap="myMap">


</select>

<resultMap type="com.diko.Employee" id="myMap">

​		<id column="id" property="id">

​		<result column="last_name" property="lastName">

​							......

</resultMap>

 

Guess you like

Origin blog.csdn.net/di_ko/article/details/114921809