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的實現(xiàn)
這篇文章主要介紹了SpringBoot Security整合JWT授權RestAPI的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman
HTTP Client 是 IDEA 自帶的一款簡潔輕量級的接口調用插件,通過它,我們能在 IDEA 上開發(fā),調試,測試 RESTful Web 服務,接下來通過本文給大家分享IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman的知識,感興趣的朋友一起看看吧2021-05-05
java日期格式化SimpleDateFormat的使用詳解
這篇文章主要介紹了java SimpleDateFormat使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05
JDK的一個Bug監(jiān)聽文件變更的初步實現(xiàn)思路
這篇文章主要介紹了JDK的一個Bug監(jiān)聽文件變更要小心了,本篇文章就帶大家簡單實現(xiàn)一個對應的功能,并分析一下對應的Bug和優(yōu)缺點,需要的朋友可以參考下2022-05-05

