關(guān)于springboot中對sqlSessionFactoryBean的自定義
springboot sqlSessionFactoryBean自定義
1.新建一個(gè)配置類,加上configuration注解
2.定制化SqlSessionFactoryBean,然后交給容器管理
代碼如下
@Configuration public class MybatisConfig { @Value("${mybatis.mapper-locations}") private String mapperLocations; @Bean public SqlSessionFactoryBean configSqlSessionFactoryBean(DataSource dataSource) throws IOException { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); // configuration.setLogImpl(StdOutImpl.class);//標(biāo)準(zhǔn)輸出日志 configuration.setLogImpl(NoLoggingImpl.class);// 不輸出日志() configuration.setMapUnderscoreToCamelCase(true);// 開啟駝峰命名 configuration.setCallSettersOnNulls(true);// 開啟在屬性為null也調(diào)用setter方法 sqlSessionFactoryBean.setConfiguration(configuration); sqlSessionFactoryBean.setDataSource(dataSource); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocations));// 設(shè)置mapper文件掃描路徑 return sqlSessionFactoryBean; }
以上配置也可以通過properties文件配置
如:
mybatis.mapper-locations=classpath:mapper/*.xml mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis.configuration.mapUnderscoreToCamelCase=true mybatis.configuration.call-setters-on-nulls=true
springboot啟動報(bào)找不到sqlSessionFactory
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/spring/boot/starter/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method ‘sqlSessionFactory' threw exception; nested exception is com.baomidou.mybatisplus.exceptions.MybatisPlusException: Error: GlobalConfigUtils setMetaData Fail ! Cause:java.sql.SQLException: oracle.jdbc.OracleDriver
原因是這個(gè)電腦 ,這個(gè)項(xiàng)目第一次啟動,項(xiàng)目鏈接的是Oracle的數(shù)據(jù)庫,Oracle沒把自己jar包放在maven庫,要自己安裝
在maven倉庫目錄下 放置Oracle的jar包
再在cmd中切換到 這個(gè)目錄下運(yùn)行命令:
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=ojdbc14.jar
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(47)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-08-08Spring Cloud Feign請求添加headers的實(shí)現(xiàn)方式
這篇文章主要介紹了Spring Cloud Feign請求添加headers的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04java常用工具類 Reflect反射工具類、String字符串工具類
這篇文章主要為大家詳細(xì)介紹了java常用工具類,包括Reflect反射工具類、String字符串工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05基于Java實(shí)現(xiàn)計(jì)數(shù)排序,桶排序和基數(shù)排序
這篇文章主要為大家詳細(xì)介紹了計(jì)數(shù)排序,桶排序和基數(shù)排序的多種語言的實(shí)現(xiàn)(JavaScript、Python、Go語言、Java),感興趣的小伙伴可以了解一下2022-12-12springboot3整合遠(yuǎn)程調(diào)用的過程解析
遠(yuǎn)程過程調(diào)用主要分為:服務(wù)提供者,服務(wù)消費(fèi)者,通過連接對方服務(wù)器進(jìn)行請求交互,來實(shí)現(xiàn)調(diào)用效果,這篇文章主要介紹了springboot3整合遠(yuǎn)程調(diào)用,需要的朋友可以參考下2023-06-06使用profiles進(jìn)行多環(huán)境配置的代碼實(shí)現(xiàn)
在項(xiàng)目開發(fā)的過程中會用到多個(gè)環(huán)境,為了便于開發(fā)使用,通常需要使用profiles進(jìn)行多環(huán)境配置,所以本文給大家介紹了使用profiles進(jìn)行多環(huán)境配置的代碼實(shí)現(xiàn),需要的朋友可以參考下2024-02-02