mybatis中数据库字段和实体类的属性映射问题

发布时间 2023-07-11 19:25:08作者: _双林

  由于数据库中表的列名一般是按照多个单词之间用下划线隔开,而java一般是驼峰命名法,所以这两者之间存在映射不到的问题,解决方案如下:

1.给字段添加别名,如下:

 <select id="getManagerInfo" resultType="string" >
        select last_login_time lastLoginTime  from wy_manager
        where id=#{id}
    </select>

2.在mybatis中设置开启驼峰命名规则,在配置文件中配置如下:

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

3.使用ResultMap。

    <select id="getAllHouse" resultMap="houseInfo">
        SELECT *from table
    </select>
    <resultMap id="houseInfo" type="com.xx.xx.entity.House">
        <result column="house_area" property="houseArea"/>
       <result column="building_id" property="buildingId"/>
      ...
</resultMap>