日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

解決mybatis中因數據庫列名和實體類屬性名不同而獲取不到數據的問題

作者:徐明宇x 更新時間: 2024-07-13 編程語言

1.在查詢的時候對列名起別名

<mapper namespace="com.mingyu.mapper.UserMapper">
    <select id="selectAll" resultType="com.mingyu.pojo.student">
        select student_name as studentName,age from student
    </select>
</mapper>

2.sql片段

在sql標簽中書寫sql語句,id屬性用來聲明該標簽的名字,在使用的時候將sql片段的名字寫在include標簽的refid的屬性中即可。

缺點:不夠靈活

<sql id="student_column">
        student_name as studentName,age
    </sql>
    <select id="selectAll" resultType="com.mingyu.pojo.student">
        select  <include refid="student_column"></include>
        from student
    </select>

3.resultMap映射

通過使用resultMap映射來完成對指定關鍵字的映射。

select標簽中需要添加resultMap屬性才能完成映射。

<resultMap id="selectAllMap" type="com.mingyu.pojo.student">
        <result column="student_name" property="studentName"></result>
    </resultMap>
    <select id="selectAll" resultMap="selectAllMap">
        select *
        from student
    </select>

原文鏈接:https://blog.csdn.net/m0_72903413/article/details/140301004

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新