项目场景:
springboot+mybatis-plus集成flowable扫描jar包下的xml文件报错,mybatisplus版本是3.5.2
报错信息如下:
Mapped Statements collection does not contain value for org.flowable.ui.modeler.domain.Model.selectModelByParameters
原因为未配置扫描flowable的mapping文件
原因:
这里会有俩个问题,
(1)、是jar包下的xml扫包没扫到.
(2)、是在xml文件还需要一些参数.
解决方案:
1. yml文件配置
#yml:
mybatis-plus:
mapper-locations: classpath:/META-INF/modeler-mybatis-mappings/*.xml
configuration-properties:
prefix:
blobType: BLOB
boolValue: TRUE
这种适用于单数据源场景,原因是在mybatisplus启动类中启动条件是单数据源匹配
@ConditionalOnSingleCandidate 注解用来判断指定类在 BeanFactory 中是否只有一个实例:
(1)如果在 BeanFactory 中存在多个实例,则匹配失败;
(2)如果在 BeanFactory 中仅仅存在一个实例,则匹配成功
2. mybatis的xml文件配置
这种方式是查资料找到的,未做测试,
(1)、spring-dao.xml增加以下红色部分:
<bean name="自己的" id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="mysqlDB"/>
<property name="mapperLocations">
<array>
<!-- 这里是自己项目的xml文件路径 -->
<value>classpath*:/mappings/**/*.xml</value>
<!-- 这里是flowavle的xml文件路径 -->
<value>classpath:/META-INF/modeler-mybatis-mappings/*.xml</value>
</array>
</property>
<property name="configLocation" value="classpath:/config/mybatis-config.xml"/>
</bean>
(2)、mybatis-config.xml增加如下部分:
<properties resource="flowable.properties">
<property name="blobType" value="BLOB"></property>
<property name="boolValue" value="TRUE"></property>
<property name="prefix" value=""></property>
</properties>
(3)、flowable.properties增加以下部分:
blobType=BLOB
boolValue=TRUE
prefix=
3. 手动SqlSessionFactory
由于项目的多数据源,所以需要手动去生成SqlSessionFactory
@Bean
@Primary
public SqlSessionFactory sqlSessionFactoryCore() throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(shardingSphereDataSourceCore());
//这里是自己项目的xml文件路径
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:com/domain/core/**/*Mapper.xml");
Resource[] resources2 = new PathMatchingResourcePatternResolver().getResources("classpath:com/domain/flowable/**/*Mapper.xml");
//这里是jar包的xml文件路径
Resource[] resources3 = new PathMatchingResourcePatternResolver().getResources("classpath:/META-INF/modeler-mybatis-mappings/*.xml");
int length1 = resources.length;
int length2 = resources2.length;
int length3 = resources3.length;
Resource[] newResources = new Resource[length1 + length2 + length3];
List<Resource> newResourceList = new ArrayList<>();
List<Resource> resourcesList1 = Arrays.asList(resources);
List<Resource> resourcesList2 = Arrays.asList(resources2);
List<Resource> resourcesList3 = Arrays.asList(resources3);
newResourceList.addAll(resourcesList3);
newResourceList.addAll(resourcesList1);
newResourceList.addAll(resourcesList2);
if(CollectionUtil.isNotEmpty(newResourceList)){
for (int i = 0; i < newResourceList.size(); i++) {
newResources[i] = newResourceList.get(i);
}
}
sqlSessionFactoryBean.setMapperLocations(newResources);
//插件,看自己需求,不用的可以注释
sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageInterceptorCem, mybatisPlusInterceptorCem});
GlobalConfig globalConfig = new GlobalConfig();
DbConfig dbConfig = new DbConfig();
dbConfig.setIdType(IdType.ASSIGN_UUID);
globalConfig.setDbConfig(dbConfig);
globalConfig.setMetaObjectHandler(customMetaObjectHandler);
//数据库一些配置
sqlSessionFactoryBean.setGlobalConfig(globalConfig);
//这里去做参数处理,MybatisConfiguration是继承mybatis的Configuration
MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
mybatisConfiguration.setMapUnderscoreToCamelCase(false);
//设置参数
Properties properties = new Properties();
properties.setProperty("boolValue", "true");
properties.setProperty("blobType", "BLOB");
properties.setProperty("prefix", "");
mybatisConfiguration.setVariables(properties);
sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
//返回SqlSessionFactory
return sqlSessionFactoryBean.getObject();
}