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注解
通过@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驼峰命名自动映射的三种方式的详细内容,更多关于springboot mybatis自动映射的资料请关注脚本之家其它相关文章!
相关文章
SpringBoot Security整合JWT授权RestAPI的实现
这篇文章主要介绍了SpringBoot Security整合JWT授权RestAPI的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-11-11
详解IntelliJ IDEA 自带的 HTTP Client 接口调用插件吊打 Postman
HTTP Client 是 IDEA 自带的一款简洁轻量级的接口调用插件,通过它,我们能在 IDEA 上开发,调试,测试 RESTful Web 服务,接下来通过本文给大家分享IntelliJ IDEA 自带的 HTTP Client 接口调用插件吊打 Postman的知识,感兴趣的朋友一起看看吧2021-05-05
java日期格式化SimpleDateFormat的使用详解
这篇文章主要介绍了java SimpleDateFormat使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-05-05


最新评论