学无先后,达者为师

网站首页 编程语言 正文

解决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

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新