mybatis中bean和数据库字段名字不一致解决方式

1、使用内置ConfigurationCustomizer

编写配置类,修改Configuration中的 mapUnderscoreToCamelCase 属性为 true

此时数据库字段和实体类字段不一致问题可以解决

@Configuration
public class MybatisConfig {

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

 

2、使用字段映射

在mybatis配置文件中可以使用 resultMap 字段进行映射

在springboot 中,可以在yml 配置中配置mybatis  在yml中 配置  mybatis: configuration: map-underscore-to-camel-case: true

mybatis:
  # 加载所有的xml文件
  mapper-locations: classpath:mapper/*.xml
  # 类型别名
  type-aliases-package: sboot.dao
  # 将数据库下划线字段与实体类的驼峰字段映射
  configuration:
    map-underscore-to-camel-case: true

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值