java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > mybatis+springboot+flowable6.4.0问题

解决mybatis+springboot+flowable6.4.0遇到的问题

作者:ToBeYourBaBa

文章主要介绍了MyBatis和Flowable在Spring Boot项目中的冲突问题,以及如何通过排除Flowable的SQLSession注入、增加Flowable属性配置和处理数据库表错误来解决这个问题

问题

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

写错对应关系 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
#这里就是配个空。或者配置数据库用户名 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;
  }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文