Mybatis中数据库中字段名与属性名不一致怎么办?

方法1:起别名

<select id="selectAll" resultType="brand">
    select
    id, brand_name as brandName, company_name as companyName, ordered, description, status
    from tb_brand;
</select>

如上图,通过as关键字取别名,取名同字段对应的属性名一致

方法2:<resultMap>标签

注意:里面只需要定义 字段名 和 属性名 不一样的映射,而一样的则不需要专门定义出来。

<resultMap id="brandResultMap" type="brand">
    <result column="brand_name" property="brandName"/>
    <result column="company_name" property="companyName"/>
</resultMap>

关键字解释:

id:完成主键字段的映射
column:表的列名
property:实体类的属性名
result:完成一般字段的映射
column:表的列名
property:实体类的属性名

sql标签正常写即可(将select标签中添加resultMap属性,对应的value值为上述resultMap的id:

<select id="selectAll" resultMap="brandResultMap">
    select *
    from tb_brand;
</select>