MyBatis系列二  之   数据库列名于程序实体类中字段名称不一致

情景:当数据库中的列名与我们程序实体类中的字段名称不一致

        使用ResultMap节点配置信息

 在映射文件中  mapper根节点下配置ResultMap节点信息

<resultMap type="Student" id="studentMapper">
        <result column="sname" property="stuname"/>
</resultMap>

解析: type为程序中实体类的全类名(此处我使用了别名配置 故直接用Student) 

         id为我们自定义的命名  供下文使用

         result子节点为非主键的字段  column为数据库中的列名  property为程序中实体类的字段名

  在查询的节点上将ResultType替换为ResultMap   并且ResultType和ResultMap不能并存

<!--查询所有学生  -->
    <select id="getAll" resultMap="studentMapper" >
        select * from Student
    </select>

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-04
  • 2021-12-25
  • 2021-05-25
  • 2022-12-23
  • 2021-12-26
  • 2021-04-17
猜你喜欢
  • 2022-01-20
  • 2021-06-28
  • 2021-11-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案