人生到处知何似,应似飞鸿踏雪泥。|

wshuanglin

园龄:2年11个月粉丝:0关注:2

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

  由于数据库中表的列名一般是按照多个单词之间用下划线隔开,而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>
复制代码

 

本文作者:wshuanglin

本文链接:https://www.cnblogs.com/hectorlin/p/mave_mybatis_problem01.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   wshuanglin  阅读(176)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起