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

第一种在sql语句中起别名:

如:

<select id="getEmployeeById" resultMap="myMap">
select id id,q_name qName  from employees where id = #{id}
</select>

 

第二种驼峰命名法:

<!-- <settings> -->
<!-- 开启驼峰命名规则 ,可以将数据库中的下划线映射为驼峰命名
例如:last_name可以映射为lastName
-->
<!-- <setting name="mapUnderscoreToCamelCase" value="true"/> -->
<!-- </settings> -->

 

第三种自定义映射:

<!-- 自定义高级映射 -->
<resultMap type="com.atguigu.mybatis.entities.Employee" id="myMap">
<!-- 映射主键 -->
<id column="id" property="id"/>
<!-- 映射其他列 -->
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="salary" property="salary"/>
<result column="dept_id" property="deptId"/>
</resultMap>

posted @ 2021-01-03 11:28  十二lq  阅读(220)  评论(0)    收藏  举报