Mybatis中实体类中的属性名和表中的字段名不一样怎么办?

解决方案:

1)写sql语句时起别名,例如:

更改last_name为lastName

更改dept_id为deptId

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

2)在MyBatis的全局配置文件中开启驼峰命名规则

<configuration>

​		<settings>

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

​		</settings>

​		.......

</configuration>

3)在Mapper映射文件中使用resultMap来自定义映射规则

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

猜你喜欢

转载自blog.csdn.net/di_ko/article/details/114921809
今日推荐