mybatis驼峰命名属性功能与配置

2019年1月13日 18点热度 0条评论 来源: zhangbeizhen18

1.本例环境
    springboot + mybatis + IntelliJ IDEA 
2.功能:
       数据库字段,一般都用下划线分隔例如employee表的姓名字段last_name;但是Model等实体对象中的属性,一般用驼峰命名,例如lastName;使用mybatis驼峰命名属性后,表字段last_name能自动映射到表对应实体对象的lastName.
3.springboot集成mybatis后可以如下配置
方式一:
  在application.yml配置

mybatis:
  # 指定sql映射文件位置
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:
    #开启驼峰命名
    map-underscore-to-camel-case: true

 方式二:
  1>.在application.yml配置

mybatis:
# 指定全局配置文件位置
  config-location: classpath:mybatis/mybatis-config.xml
# 指定sql映射文件位置
  mapper-locations: classpath:mybatis/mapper/*.xml

2>.在mybatis-config.xml配置

<settings>
   <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

方式三:
  1>.在在application.yml配置

mybatis:
  # 指定sql映射文件位置
  mapper-locations: classpath:mybatis/mapper/*.xml

 2>.添加自动自动配置类

@Configuration
public class MybatisConfig {
    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer(){
            @Override
            public void customize(org.apache.ibatis.session.Configuration configuration) {
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}

4.表结构

drop table if exists employee;
CREATE TABLE `employee` (
  `id` INT(16) NOT NULL AUTO_INCREMENT COMMENT '编号',
  `last_name` VARCHAR(128) DEFAULT NULL COMMENT '姓名',
  `email` VARCHAR(128) DEFAULT NULL COMMENT '邮件',
  `gender` INT(2) DEFAULT NULL COMMENT '性别',
  `card_number` VARCHAR(64) DEFAULT NULL COMMENT '胸卡编号',
  `post` VARCHAR(128) DEFAULT NULL COMMENT '岗位',
  `department_id` INT(11) DEFAULT NULL COMMENT '部门编号',
  PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COMMENT='员工表' ;

5.实体对象

public class Employee implements Serializable {
    private Integer id;
    private String lastName;
    private String email;
    private Integer gender;
    private String cardNumber;
    private String post;
    /** 数据库字段: department_id 开启驼峰命名后可以自动匹配*/
    private Integer departmentId;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }
    public String getCardNumber() {
        return cardNumber;
    }
    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }
    public String getPost() {
        return post;
    }
    public void setPost(String post) {
        this.post = post;
    }
    public Integer getDepartmentId() {
        return departmentId;
    }
    public void setDepartmentId(Integer departmentId) {
        this.departmentId = departmentId;
    }
}

以上,TKS.

    原文作者:zhangbeizhen18
    原文地址: https://blog.csdn.net/zhangbeizhen18/article/details/88555832
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。