springboot開啟mybatis駝峰命名自動映射的三種方式
方式一:通過springboot的配置文件application.yml
mybatis: configuration: map-underscore-to-camel-case: true
此方式是最簡單的,但是要注意,通過springboot的配置文件配置mybatis的設(shè)置,則不能夠再使用mybatis的配置文件
例如:下邊代碼中的classpath:mybatis/mybatis-config.xml和map-underscore-to-camel-case: true兩個設(shè)置不能同時存在
要么使用config-location指定mybatis的配置文件,在通過mybatis的配置文件配置相關(guān)設(shè)置,要么通過springboot配置文件的mybatis.configuration進(jìn)行相關(guān)設(shè)置,二者只能選其一,否則會報錯。
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配置文件中進(jìn)行設(shè)置
<?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中進(jìn)行設(shè)置(沒試過)
@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駝峰命名自動映射的三種方式的詳細(xì)內(nèi)容,更多關(guān)于springboot mybatis自動映射的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
集群環(huán)境中使用ehcache_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了集群環(huán)境中使用ehcache的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Java實(shí)現(xiàn)Timer的定時調(diào)度函數(shù)schedule的四種用法
本文主要介紹了Java實(shí)現(xiàn)Timer的定時調(diào)度函數(shù)schedule的四種用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04hibernate關(guān)于session的關(guān)閉實(shí)例解析
這篇文章主要介紹了hibernate關(guān)于session的關(guān)閉實(shí)例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02對Mybatis?Plus中@TableField的使用正解
這篇文章主要介紹了對Mybatis?Plus中@TableField的使用正解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01