我是靠谱客的博主 迷路香氛,这篇文章主要介绍关于springboot中对sqlSessionFactoryBean的自定义,现在分享给大家,希望可以做个参考。

springboot sqlSessionFactoryBean自定义

1.新建一个配置类,加上configuration注解

2.定制化SqlSessionFactoryBean,然后交给容器管理

代码如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@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文件配置

如:

复制代码
1
2
3
4
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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持靠谱客。

最后

以上就是迷路香氛最近收集整理的关于关于springboot中对sqlSessionFactoryBean的自定义的全部内容,更多相关关于springboot中对sqlSessionFactoryBean内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(375)

评论列表共有 0 条评论

立即
投稿
返回
顶部