美文网首页
使用Mybatis时实体类属性名和表中的字段名不一致导致结果无法

使用Mybatis时实体类属性名和表中的字段名不一致导致结果无法

作者: 三招六式一把铁斧 | 来源:发表于2018-02-23 14:30 被阅读0次

问题描述

java中的属性的命名规则为驼峰式命名,mysql中则使用下划线将各个不同的单词分开(feedback_time),这使得数据库中的表单字段与java实体类的属性名不一致,导致mybatis无法映射到java实体类

解决方法

  1. 方法一:通过在查询的SQL语句中定义字段名的别名的方式,让字段名的别名和实体类中的属性名一致,这样就可以实现实体类属性和表字段一一对应。(通过在SQL语句中定义别名的方法实现)【个人不推荐】
    示例:
<select id="queryCertificationInfoByCerNumber" parameterType="string" resultMap="certificationResultMap">  
    SELECT cer_number cerNumber FROM, cer_time cerTime, cer_type cerType t_diamond_allinfo_gia WHERE cer_number = #{cerNumber}  
</select>  

注:cer_number 是数据库中表单的字段名,cerNumber是java实体类中属性名

  1. 通过<resultMap>来映射字段名和实体类属性名的一一对应关系。(使用Mybatis提供的解决方法)


    image.png
<resultMap type="com.shebao.base.model.OrderFlow" id="orderFlow">
        <id column="flow_id" property="flowId" />
        <result column="work_order_id" property="workOrderId" />
        <result column="receive_time" property="receiveTime" />
        <result column="state" property="state" />
        <result column="operate_type" property="operateType" />
        <result column="robot_id" property="robotId" />
        <result column="send_time" property="sendTime" />
        <result column="feedback_time" property="feedbackTime" />
        <result column="rpa_order_id" property="RPAOrderId" />
        <result column="result_id" property="resultId" />
</resultMap>

相关文章

网友评论

      本文标题:使用Mybatis时实体类属性名和表中的字段名不一致导致结果无法

      本文链接:https://www.haomeiwen.com/subject/nblxxftx.html