总结springboot开启mybatis驼峰命名自动映射的三种方式

方式一:通过springboot的配置文件application.yml

mybatis:
  configuration:
    map-underscore-to-camel-case: true

此方式是最简单的,但是要注意,通过springboot的配置文件配置mybatis的设置,则不能够再使用mybatis的配置文件,
例如:下边代码中的classpath:mybatis/mybatis-config.xml和map-underscore-to-camel-case: true两个设置不能同时存在,
要么使用config-location指定mybatis的配置文件,在通过mybatis的配置文件配置相关设置,
要么通过springboot配置文件的mybatis.configuration进行相关设置,二者只能选其一,否则会报错。

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true

方式二:通过mybatis的配置文件

首先需要在springboot的配置文件application.yml中指定mybatis配置文件的位置。

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

然后在mybatis配置文件中进行设置

<?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE configuration
         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 <configuration>
   <settings>
       <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

方式三:通过@Comfiguration注解和@Bean注解,向容器中添加ConfigurationCustomizer类型的组件,在ConfigurationCustomizer中进行设置(没试过)

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

链接: 总结springboot开启mybatis驼峰命名自动映射的三种方式

  • 12
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值