1.起别名解决
在写sql语句时给字段(表列名)起别名,将别名定义成和实体类中属性名一致即可
<select id="selectAll" resultType="brand">
select
id, brand_name as brandName, company_name as companyName, ordered, description, status
from tb_brand;
</select>
2.在映射配置文件Mapper.xml中使用resultMap解决
如下:
<resultMap id="brandResultMap" type="brand">
<!--
id:完成主键字段的映射
result:完成一般字段的映射
column:表的列名
property:实体类的属性名
-->
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/>
</resultMap>
值得注意的是,配置好映射文件后SQL语句中resultType="brand"应改为resultMap="brandResultMap",如下:
<select id="selectAll" resultMap="brandResultMap">
select *
from tb_brand;
</select>
或者在mapper接口的查询方法前面加上注解:
@ResultMap("brandResultMap")
3.在mybatis-config.xml文件中配置开启驼峰命名规则自动转换
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--开启驼峰命名规则自动转换-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>