zoukankan      html  css  js  c++  java
  • mybaties返回值对对象,字段名与属性名不匹配问题

    字段名与属性名不一致,会造成属性值为null

    1、默认行为根据名称相同自动匹配
    When auto-mapping results MyBatis will get the column name and look for a** property** with the same name ignoring case.
    2、一般开启驼峰命名
    Usually database columns are named using uppercase letters and underscores between words and java properties often follow the camelcase naming covention. To enable the auto-mapping between them set the setting mapUnderscoreToCamelCase to true.

    例如
    class User{
    private String userName;
    getset方法;
    }
    user表中字段名为:user_name
    解决思路:
    1、select user_name AS userName,起别名。
    优点:最简单,易理解
    缺点:如果有N个查询,每个查询写一遍别名,需要N遍,有耐心你就写,等到字段名有修改,你在一处一处去修改,加班开始哈哈哈。
    2、添加类属性名与数据库字段名映射关系。(column为表中字段名)



    优点:映射关系可以复用
    缺点:不能一眼看出对应的实体类,需要先看resultMap,在找到其type属性

  • 相关阅读:
    java栈的实现复习
    条件判断语句--linux
    转义符&脚本参数--linux
    linux备份脚本-模板
    github的搜索使用技巧
    sonarqube7.9安装部署(linux)
    openjdk与oraclejdk下载
    gitlab重写历史,清理历史大文件
    nexus私服搭建过程
    nexus私服关联本地maven配置
  • 原文地址:https://www.cnblogs.com/xingrui/p/14279817.html
Copyright © 2011-2022 走看看