关于springboot中对sqlSessionFactoryBean的自定义
作者:今夕是何年? 时间:2022-12-09 06:05:09
springboot 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
来源:https://www.cnblogs.com/fallmwu/p/13863141.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用EasyPoi轻松导入导出Excel文档的方法示例
![](https://img.aspxhome.com/file/2023/8/92638_0s.png)
C#Winform窗口移动方法
详解Spring MVC3返回JSON数据中文乱码问题解决
![](https://img.aspxhome.com/file/2023/7/77107_0s.jpg)
Java反射 Field类的使用全方位解析
java对象序列化与反序列化的默认格式和json格式使用示例
Java的封装类和装箱拆箱详解
android 更改TextView中任意位置字体大小和颜色的方法
![](https://img.aspxhome.com/file/2023/0/138730_0s.jpg)
Android进程运行中权限被收回导致关闭的问题解决
![](https://img.aspxhome.com/file/2023/1/89531_0s.png)
SprinBoot整合Quart实现定时调度的示例代码
解析Mybatis SqlSessionFactory初始化原理
![](https://img.aspxhome.com/file/2023/4/62084_0s.jpg)
c#调用qq邮箱smtp发送邮件修改版代码分享
android学习笔记之View的滑动
![](https://img.aspxhome.com/file/2023/4/130304_0s.gif)
javaweb实现在线支付功能
![](https://img.aspxhome.com/file/2023/8/93348_0s.jpg)
Java算法设计与分析分治算法
![](https://img.aspxhome.com/file/2023/9/92549_0s.png)
Spring Boot配置接口WebMvcConfigurer的实现
![](https://img.aspxhome.com/file/2023/3/60523_0s.png)
常用Hash算法(C语言的简单实现)
Java接口中尽量避免使用数组
![](https://img.aspxhome.com/file/2023/0/69930_0s.png)
C#实现基于加减按钮形式控制系统音量及静音的方法
C# 实现视频监控系统(附源码)
![](https://img.aspxhome.com/file/2023/8/122658_0s.png)
使用cmd根据WSDL网址生成java客户端代码的实现
![](https://img.aspxhome.com/file/2023/1/99871_0s.jpg)