springboot sqlSessionFactoryBean自定义
今夕是何年? 人气:3springboot sqlSessionFactoryBean自定义
1.新建一个配置类,加上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);//标准输出日志 configuration.setLogImpl(NoLoggingImpl.class);// 不输出日志() configuration.setMapUnderscoreToCamelCase(true);// 开启驼峰命名 configuration.setCallSettersOnNulls(true);// 开启在属性为null也调用setter方法 sqlSessionFactoryBean.setConfiguration(configuration); sqlSessionFactoryBean.setDataSource(dataSource); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocations));// 设置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启动报找不到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
原因是这个电脑 ,这个项目第一次启动,项目链接的是Oracle的数据库,Oracle没把自己jar包放在maven库,要自己安装
在maven仓库目录下 放置Oracle的jar包
再在cmd中切换到 这个目录下运行命令:
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=ojdbc14.jar
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
加载全部内容