您的位置:首页 > 编程语言 > Java开发

数据库与JavaBean字段名不一致处理

2017-11-20 10:43 423 查看
当数据库字段与
JavaBean
字段不一致时将导致封装结果失败,一般数据库字段命名用下划线如:
first_name
JavaBean
采用驼峰命名法如:
firstName


解决方法有三种:

1.取别名

sql
语句取别名与
JavaBean
字段对应

<select id="getList" resultType="com.mybatis.entity.Employee">
select e.employee_id id,e.first_name firstName,e.last_name lastName,e.email email
from employees e
</select>


2.开启驼峰命名自动转换

MyBatis
配置文件中将
mapUnderscoreToCamelCase
设置为
true


<settings>
<setting name="mapUnderscoreToCamelCase " value="true"/>
</settings>


注意: 使用此项需注意命名规范

3.使用
ResultMap


在映射文件中使用
ResultMap
自定义映射

<resultMap id="myMap" type="com.mybatis.entity.Employee">

<id column="employee_id" property="id"/>
<result column="first_name" property="firstName"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>

</resultMap>

<select id="getList" resultMap="myMap">
select e.employee_id,e.first_name,e.last_name,e.email
from employees e
</select>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: