解決mybatis+springboot+flowable6.4.0遇到的問題
問題
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
寫錯對應(yīng)關(guān)系 namespace等騷操作造成的就不說了。動動腦子。下面主要說沖突導致的這個問題。
源碼查看
1 報錯位置
MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
configuration);
if (ms == null) {
if (method.getAnnotation(Flush.class) != null) {
name = null;
type = SqlCommandType.FLUSH;
} else {
throw new BindingException("Invalid bound statement (not found): "
+ mapperInterface.getName() + "." + methodName);
}
...
2 ms為啥為空 因為 mappedStatements里沒有(MapperLocations配置沒生效)
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
Class<?> declaringClass, Configuration configuration) {
String statementId = mapperInterface.getName() + "." + methodName;
if (configuration.hasStatement(statementId)) {
return configuration.getMappedStatement(statementId);
} else if (mapperInterface.equals(declaringClass)) {
return null;
}
public boolean hasStatement(String statementName) {
return hasStatement(statementName, true);
}
public boolean hasStatement(String statementName, boolean validateIncompleteStatements) {
if (validateIncompleteStatements) {
buildAllStatements();
}
return mappedStatements.containsKey(statementName);
}
3 真正原因! @ConditionalOnMissingBean !?。。。喝绻萜髦袥]有,就注入,如果有就不注入.mybatis的sqlsession被flowable的覆蓋了
mybatis的:org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration里
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
...
factory.setMapperLocations(this.properties.resolveMapperLocations());
...
}
flowable的:org.flowable.ui.modeler.conf.DatabaseConfiguration里
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
String databaseType = this.initDatabaseType(dataSource);
if (databaseType == null) {
throw new FlowableException("couldn't deduct database type");
} else {
try {
Properties properties = new Properties();
properties.put("prefix", this.modelerAppProperties.getDataSourcePrefix());
properties.put("blobType", "BLOB");
properties.put("boolValue", "TRUE");
properties.load(this.getClass().getClassLoader().getResourceAsStream("org/flowable/db/properties/" + databaseType + ".properties"));
sqlSessionFactoryBean.setConfigurationProperties(properties);
sqlSessionFactoryBean.setMapperLocations(ResourcePatternUtils.getResourcePatternResolver(this.resourceLoader).getResources("classpath:/META-INF/modeler-mybatis-mappings/*.xml"));
sqlSessionFactoryBean.afterPropertiesSet();
return sqlSessionFactoryBean.getObject();
} catch (Exception var5) {
throw new FlowableException("Could not create sqlSessionFactory", var5);
}
}
}
4 很明顯,mybatis的配置被覆蓋了。吐槽一下flowable-ui的源碼!
解決辦法
就是兼顧 flowable和mybatis的配置
1 第一步 排除flowable的sqlsession注入:
在@ComponentScan里加入這樣的話。 @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = DatabaseConfiguration.class)
2 第二步 增加flowable的屬性配置等。application.properties里添加如下配置。加modeler-mybatis-mapping那個
mybatis.mapperLocations=classpath*:mapper/*/*.xml,classpath:/META-INF/modeler-mybatis-mappings/*.xml #這里就是配個空。或者配置數(shù)據(jù)庫用戶名 username也行,不要瞎改哈(這個是查詢前綴-flowable) mybatis.configuration-properties.prefix = mybatis.configuration-properties.blobType = BLOB mybatis.configuration-properties.boolValue = TRUE
3 第三步,做到這直接啟動項目可能會報 act_de_model表找不到等錯誤。再一個@Configuration配置里加入以下bean的注入
@Bean
public Liquibase liquibase(DataSource dataSource) {
Liquibase liquibase = null;
Liquibase var5;
try {
DatabaseConnection connection = new JdbcConnection(dataSource.getConnection());
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
database.setDatabaseChangeLogTableName("ACT_DE_" + database.getDatabaseChangeLogTableName());
database.setDatabaseChangeLogLockTableName("ACT_DE_" + database.getDatabaseChangeLogLockTableName());
liquibase = new Liquibase("META-INF/liquibase/flowable-modeler-app-db-changelog.xml", new ClassLoaderResourceAccessor(), database);
liquibase.update("flowable");
var5 = liquibase;
} catch (Exception var9) {
throw new InternalServerErrorException("Error creating liquibase database", var9);
} finally {
}
return var5;
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java框架基礎(chǔ)之SPI機制實現(xiàn)及源碼解析
這篇文章主要為大家介紹了java框架基礎(chǔ)之SPI機制實現(xiàn)及源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
SpringBoot?整合?Quartz?定時任務(wù)框架詳解
這篇文章主要介紹了SpringBoot整合Quartz定時任務(wù)框架詳解,Quartz是一個完全由Java編寫的開源作業(yè)調(diào)度框架,為在Java應(yīng)用程序中進行作業(yè)調(diào)度提供了簡單卻強大的機制2022-08-08
SpringBoot利用自定義注解實現(xiàn)多數(shù)據(jù)源
這篇文章主要為大家詳細介紹了SpringBoot如何利用自定義注解實現(xiàn)多數(shù)據(jù)源效果,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以了解一下2022-10-10
idea社區(qū)版如何設(shè)置vm?options
這篇文章主要介紹了idea社區(qū)版如何設(shè)置vm?options問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

