1、在sql查询语句中,给字段起别名保持和实体类中的属性名一致
<select id="getAllEmpOld" resultType="Emp">
select eid,emp_name empName,age,sex,email from t_emp
</select>
2、设置全局配置,将_下划线自动映射为驼峰(在mybatis-config.xml中进行配置)
设置mybatis中的全局配置
<settings>
<!--mapUnderscoreToCamelCase:将_自动映射为驼峰:emp_name -> empName-->
<!--这样就可以解决字段名和实体类属性名字不一致的问题-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
3、使用resultMap:设置自定义映射关系
<resultMap id="resultEmpMap" type="Emp">
<!--
resultMap:设置自定义映射关系
type:设置映射关系中的实体类类型
id 设置主键的映射关系 result普通字段的映射关系
property:type设置中的实体类中属性的名字
column:sql查询出来的字段名字
-->
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"