0


Spring Boot+Mybatis配置双数据源&整合Activiti工作流报错整理

目录

1. 首先切记!!:

mybatis多数据源的原理是根据不同包,调用不同的数据源,只需要把mapper.java和mapper.xml写在某个package中,springboot自动帮你实现数据源切换,这个不要弄错

2 配置双数据源报错

2.1.1 问题描述

org.springframework.beans.factory.BeanDefinitionStoreException:Failedtoprocessimportcandidatesfor configuration class[com.telecom.SpBookActApplication]; 
nested exception is java.io.FileNotFoundException:class path resource [org/springframework/security/config/annotation/authentication/configurers/GlobalAuthenticationConfigurerAdapter.class] cannot be opened because it does not exist

2.1.2 解决方法

启动类中添加:

@SpringBootApplication(exclude ={org.activiti.spring.boot.SecurityAutoConfiguration.class,org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class})

2.2.1 问题描述

Failedtoinstantiate[org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]:Factory method 'entityManagerFactory' threw exception;
nested exception is java.lang.IllegalArgumentException: jdbcUrl is required withdriverClassName.

2.2.2 解决方法

将配置文件Application.yml中的 url:jdbc:mysql… 改为:jdbc-url: jdbc:mysql…

2.3.1 问题描述

Error creating bean withname 'bookInfoController':Unsatisfied dependency expressed through field 'iBookInfoService'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean withname 'bookInfoServiceImpl':Unsatisfied dependency expressed through field 'baseMapper';

2.3.2 解决方法

可能性1:实现类(XxxServiceImpl)没有加@Service注解,没有的话把这个注解加上。
可能性2:mapper类没有注入进去,可以在项目启动类上面加个全盘扫描或者指定扫描其下的任何包,注解使用它即可@MapperScan(“path”),其中path为扫描路径。

2.4.1 问题描述

NoSuchBeanDefinitionException:
No qualifying bean of type ‘com.telecom.dao.BookDao’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

2.4.2 解决方法

在写了mapper层的情况下还忘记删掉之前写的的dao层,导致重复了

2.5.1 问题描述

***************************
APPLICATION FAILED TO START
***************************Description:An attempt was made tocall a method that does not exist. The attempt was made from the following location:org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:564)The following method did not exist:

'voidorg.apache.ibatis.session.Configuration.setDefaultEnumTypeHandler(java.lang.Class)'

2.5.2 解决方法

这种一般都是由于版本依赖冲突引起的问题,用maven-helper检查一下依赖冲突,exclude一下,但是发现还是不行,但是删了本地重复的版本文件也不行。

2.6.1 问题描述

jar:file:/D:/programs/apache-maven-3.6.1/mvn_resp/org/mybatis/mybatis-spring/2.0.6/mybatis-spring-2.0.6.jar!/org/mybatis/spring/SqlSessionFactoryBean.class

The called method’s class, org.apache.ibatis.session.Configuration, is available from the following locations:

jar:file:/D:/programs/apache-maven-3.6.1/mvn_resp/org/mybatis/mybatis/3.4.2/mybatis-3.4.2.jar!/org/apache/ibatis/session/Configuration.class

The called method’s class hierarchy was loaded from the following locations:

org.apache.ibatis.session.Configuration: file:/D:/programs/apache-maven-3.6.1/mvn_resp/org/mybatis/mybatis/3.4.2/mybatis-3.4.2.jar
Action:

Correct the classpath of your application so that it contains compatible versions of the classes org.mybatis.spring.SqlSessionFactoryBean and org.apache.ibatis.session.Configuration

2.6.2 解决方法

这是因为多个版本的依赖,找到mybatis-spring这个2.0.6依赖删除即可

2.7.1 问题描述

java.lang.IllegalStateException:
Failed to introspect Class [com.telecom.config.PrimaryDataSourceConfig] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@2f0e140b]
Caused by: java.lang.NoClassDefFoundError: org/apache/ibatis/session/SqlSessionFactory
Caused by: java.lang.ClassNotFoundException: org.apache.ibatis.session.SqlSessionFactory

2.7.2 解决方法

这个报错其实就是没有找到要加载的类,一般在启动的时候报错。
加载不到的类有两种情况,一种是直接找不到要加载的依赖包,或者添加了依赖包后没有加载进入,需要看看是不是 maven 的设置问题,重新刷新加载一下 maven 的依赖,或者清一下 idea 的缓存;
另外一种是导入了依赖包,但是依赖包中又引入了其他的依赖包中的类,这个类找不到。

2.8.1 问题描述

An attempt was made to call a method that does not exist. The attempt was made from the following location:

com.baomidou.mybatisplus.core.MybatisMapperAnnotationBuilder.getLanguageDriver(MybatisMapperAnnotationBuilder.java:384)

2.8.2 解决方法

com.baomidou.mybatisplus.core.MybatisMapperAnnotationBuilder.getLanguageDriver方法不存在
原因:activiti包中的 org.mybatis 包 和 mybatisPlus中的org.mybatis 包冲突。
需要排除掉activiti包中的 org.mybatis 包。

<dependency><groupId>org.activiti</groupId><artifactId>activiti-spring-boot-starter-basic</artifactId><version>6.0.0</version><exclusions><exclusion><artifactId>mybatis</artifactId><groupId>org.mybatis</groupId></exclusion></exclusions></dependency>

2.9.1 问题描述

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘deployApplications’ defined in class path resource [org/activiti/application/conf/ApplicationAutoConfiguration.class]:
Unsatisfied dependency expressed through method ‘deployApplications’ parameter 2;

nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘processEntryDeployer’ defined in class path resource [org/activiti/application/conf/ApplicationProcessAutoConfiguration.class]: Unsatisfied dependency expressed through method ‘processEntryDeployer’ parameter 0;

nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘repositoryServiceBean’ defined in class path resource [org/activiti/spring/boot/JpaProcessEngineAutoConfiguration/JpaConfiguration.class]: Unsatisfied dependency expressed through method ‘repositoryServiceBean’ parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘processEngine’ defined in class path resource [org/activiti/spring/boot/JpaProcessEngineAutoConfiguration$JpaConfiguration.class]: Unsatisfied dependency expressed through method ‘processEngine’ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘springProcessEngineConfiguration’ defined in class path resource [org/activiti/spring/boot/JpaProcessEngineAutoConfig

2.9.2 解决方法

在创建

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

针对mybatis;若是项目中使用了mybatis,则需要替换成:

MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); 

否则会报错:找不到baseMapper中的方法;

3 整合Activiti工作流报错

3.1.1 问题描述

Parameter1 of method springProcessEngineConfiguration in org.activiti.spring.boot.JpaProcessEngineAutoConfiguration$JpaConfiguration required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.

3.1.2 解决方法

// 第一种方式 指定工作流单独的数据源@ConfigurationpublicclassActivitiDatasourceConfigextendsAbstractProcessEngineAutoConfiguration{@Bean@Primary@ConfigurationProperties(prefix ="spring.datasource.activiti")publicDataSourceactivitiDataSource(){returnDataSourceBuilder.create().build();}@BeanpublicSpringProcessEngineConfigurationspringProcessEngineConfiguration(PlatformTransactionManager transactionManager,SpringAsyncExecutor springAsyncExecutor)throwsIOException{returnbaseSpringProcessEngineConfiguration(activitiDataSource(), transactionManager, springAsyncExecutor);}}// 第二种方式 使用已有的数据源@ConfigurationpublicclassActivitiConfigextendsAbstractProcessEngineAutoConfiguration{staticfinalString NAME ="master";//注入数据源和事务管理器@BeanpublicSpringProcessEngineConfigurationspringProcessEngineConfiguration(SpringAsyncExecutor springAsyncExecutor)throwsIOException{@Qualifier(NAME +"DataSource")DataSource dataSource,@Qualifier(NAME +"TransactionManager")PlatformTransactionManager transactionManager,returnthis.baseSpringProcessEngineConfiguration(dataSource, transactionManager, springAsyncExecutor);}}

此问题参考博客: https://blog.csdn.net/weixin_29300375/article/details/81181333

3.2.1 问题描述

***************************
APPLICATION FAILED TO START
***************************Description:Failedtoconfigure a DataSource:'url' attribute is not specified and no embedded datasource could be configured.

Reason:Failedtodetermine a suitable driver class

3.2.2 解决方法

八成是application.yml文件中配置数据源啥的有问题
我是因为改动了配置数据源的路径

spring.datasource.dynamic.datasource

后,但是忘记在项目的其他地方改一下这个配置文件中的路径
也可能是其他问题引起的:https://blog.csdn.net/qq_45498460/article/details/112860809

3.3.1 问题描述

Could not update Activiti database schema: unknown version from database: '7.0.0.0'

3.3.2 解决方法

这个可能是activiti下activiti-spring-boot-starter-basic包版本异常引起的数据库错误,需要如下图检查一下ProcessEngine.java里和数据库里的act_ge_property表里版本号是否一致,不一致的话将数据库中act_ge_property表中的版本号中的的版本和和pom.xml中activiti-spring-boot-starter-basic包下db.create下sql 文件中的版本对应。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这个问题也是苦苦寻找好久解决方法,最后参考的这个文章找到的这个解决办法https://blog.csdn.net/qq_41169544/article/details/122810066
,瞬间柳暗花明又一村了。


本文转载自: https://blog.csdn.net/zmh15927652085/article/details/127102974
版权归原作者 Miss 她扶 所有, 如有侵权,请联系我们删除。

“Spring Boot+Mybatis配置双数据源&整合Activiti工作流报错整理”的评论:

还没有评论