zl程序教程

您现在的位置是:首页 >  后端

当前栏目

SpringBoot开启驼峰命名规范自动映射

SpringBoot自动映射 开启 规范 命名 驼峰
2023-09-14 09:04:53 时间

mybatis在默认情况下,属性名和数据库字段名是一一对应的。

例如:若数据库字段名为:USER_ID,则java bean中属性字段对应因为:user_id(可不区分大小写)

但是java代码中实际上却不这样命名,java中一般采用驼峰命名:数据库字段名:user_name,对应java实体bean:userName,而默认mybatis不会将二者自动映射,如果想要成功映射,则需要开启mybatis的驼峰命名规范自动映射。

看图:数据库中brand_name和company_name有下划线,而在java类中没有下划线

 

在Springboot中,可以通过设置map-underscore-to-camel-case属性为true来开启驼峰功能。

application.yml配置文件中:

#开启驼峰命名规范自动映射
configuration:
    map-underscore-to-camel-case: true

application.properties配置文件中:

#开启驼峰命名规范自动映射
mybatis.configuration.map-underscore-to-camel-case=true