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駝峰命名自動映射的三種方式的詳細內(nèi)容,更多關于springboot mybatis自動映射的資料請關注腳本之家其它相關文章!
相關文章
集群環(huán)境中使用ehcache_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了集群環(huán)境中使用ehcache的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Java實現(xiàn)Timer的定時調度函數(shù)schedule的四種用法
本文主要介紹了Java實現(xiàn)Timer的定時調度函數(shù)schedule的四種用法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04對Mybatis?Plus中@TableField的使用正解
這篇文章主要介紹了對Mybatis?Plus中@TableField的使用正解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01