SpringBoot+MyBatis(通用Mapper+PageHelper+Generator)+Activiti集成及问题解决

随便一说

        因为需要搭建公司新的框架,使用了SpringBoot+MyBatis,加入了MyBatis的通用Mapper、分布插件PageHelper、MyBatis Generator,因为后面做的项目需要用到工作流,所以又需要把工作流(选用了Activiti)加进来。在没有加入Activiti之前,框架搭建的还算比较顺利,包括加入thymeleaf、lombok、swagger等,虽然遇到了问题,但解决起来都算比较顺利。但是,加入了工作流,则是遇到了大麻烦!!!


开发环境

        这里假设已经有了一个框架,是由SpringBoot+MyBatis+druid+lombok+swagger+thymeleaf(这些集成会在后续的文章中进行说明),在这个框架的基础上集成Activiti。下面对开发环境及框架版本等信息进行说明:

  • 开发工具:IDEA 2017.2
  • JDK:1.8
  • mysql:5.1.44
  • SpringBoot:2.0.1
  • MyBatis:3.4.5(Mapper:3.4.4,PageHelper:5.1.2,Generator对应的Maven插件:1.3.5)
  • druid:1.0.19
  • thymeleaf:同SpringBoot一致为2.0.1
  • swagger:2.2.2
  • lombok:1.16.8
  • Activiti:5.22.0

    

集成Activiti

       集成中遇到的问题,在文章的后面会给出解释说明,这里先说明怎么集成工作流。

  1. 添加Activiti的依赖;
  2. 生成Activiti的25张表。

添加依赖

xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0modelVersion>

   <groupId>com.woxin.itsmgroupId>
   <artifactId>itsmartifactId>
   <version>0.0.1-SNAPSHOTversion>
   
   <packaging>warpackaging>

   <name>itsmname>
   <description>itsm projectdescription>

   <parent>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-parentartifactId>
      <version>2.0.1.RELEASEversion>
      <relativePath/> 
   parent>

   <properties>
      <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
      <java.version>1.8java.version>
      <activiti.version>5.22.0activiti.version>
      <spring.security.version>5.0.4.RELEASEspring.security.version>
   properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-webartifactId>
      dependency>

      
      <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-thymeleafartifactId>
      dependency>

      
      <dependency>
         <groupId>io.springfoxgroupId>
         <artifactId>springfox-swagger2artifactId>
         <version>2.2.2version>
      dependency>
      <dependency>
         <groupId>io.springfoxgroupId>
         <artifactId>springfox-swagger-uiartifactId>
         <version>2.2.2version>
      dependency>

      
      <dependency>
         <groupId>org.projectlombokgroupId>
         <artifactId>lombokartifactId>
         <version>1.16.8version>
      dependency>

      
      <dependency>
         <groupId>com.alibabagroupId>
         <artifactId>druidartifactId>
         <version>1.0.19version>
      dependency>

      
      <dependency>
         <groupId>org.mybatis.spring.bootgroupId>
         <artifactId>mybatis-spring-boot-starterartifactId>
         <version>1.3.1version>
      dependency>

      
      <dependency>
         <groupId>tk.mybatisgroupId>
         <artifactId>mapper-spring-boot-starterartifactId>
         <version>1.1.5version>
      dependency>

      
      <dependency>
         <groupId>com.github.pagehelpergroupId>
         <artifactId>pagehelper-spring-boot-starterartifactId>
         <version>1.2.3version>
         
            
               
               
            
         
      dependency>

      <dependency>
         <groupId>mysqlgroupId>
         <artifactId>mysql-connector-javaartifactId>
      dependency>

      <dependency>
         <groupId>com.alibabagroupId>
         <artifactId>fastjsonartifactId>
         <version>1.2.16version>
      dependency>

      
      <dependency>
         <groupId>org.activitigroupId>
         <artifactId>activiti-springartifactId>
         <version>${activiti.version}version>
         <exclusions>
            <exclusion>
               <groupId>org.codehaus.jacksongroupId>
               <artifactId>jackson-core-aslartifactId>
            exclusion>
            <exclusion>
               <groupId>org.slf4jgroupId>
               <artifactId>slf4j-log4j12artifactId>
            exclusion>
            <exclusion>
               <artifactId>spring-contextartifactId>
               <groupId>org.springframeworkgroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-jdbcartifactId>
               <groupId>org.springframeworkgroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-txartifactId>
               <groupId>org.springframeworkgroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-ormartifactId>
               <groupId>org.springframeworkgroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-beansartifactId>
               <groupId>org.springframeworkgroupId>
            exclusion>
            <exclusion>
               <artifactId>mybatisartifactId>
               <groupId>org.mybatisgroupId>
            exclusion>
            <exclusion>
               <artifactId>activationartifactId>
               <groupId>javax.activationgroupId>
            exclusion>
         exclusions>
      dependency>

      <dependency>
         <groupId>org.activitigroupId>
         <artifactId>activiti-modelerartifactId>
         <version>${activiti.version}version>
         <exclusions>
            <exclusion>
               <artifactId>spring-beansartifactId>
               <groupId>org.springframeworkgroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-contextartifactId>
               <groupId>org.springframeworkgroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-coreartifactId>
               <groupId>org.springframeworkgroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-txartifactId>
               <groupId>org.springframeworkgroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-webartifactId>
               <groupId>org.springframeworkgroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-security-configartifactId>
               <groupId>org.springframework.securitygroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-security-coreartifactId>
               <groupId>org.springframework.securitygroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-security-cryptoartifactId>
               <groupId>org.springframework.securitygroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-security-webartifactId>
               <groupId>org.springframework.securitygroupId>
            exclusion>
            <exclusion>
               <artifactId>spring-webmvcartifactId>
               <groupId>org.springframeworkgroupId>
            exclusion>
            <exclusion>
               <artifactId>activationartifactId>
               <groupId>javax.activationgroupId>
            exclusion>
            <exclusion>
               <artifactId>commons-ioartifactId>
               <groupId>commons-iogroupId>
            exclusion>
         exclusions>
      dependency>

      <dependency>
         <groupId>org.activitigroupId>
         <artifactId>activiti-restartifactId>
         <version>${activiti.version}version>
      dependency>
      <dependency>
         <groupId>org.activitigroupId>
         <artifactId>activiti-explorerartifactId>
         <version>${activiti.version}version>
      dependency>
      <dependency>
         <groupId>org.activitigroupId>
         <artifactId>activiti-webapp-explorer2artifactId>
         <version>${activiti.version}version>
      dependency>
      <dependency>
         <groupId>org.activitigroupId>
         <artifactId>activiti-diagram-restartifactId>
         <version>${activiti.version}version>
      dependency>
      <dependency>
         <groupId>org.activitigroupId>
         <artifactId>activiti-simple-workflowartifactId>
         <version>${activiti.version}version>
      dependency>
      

      <dependency>
         <groupId>org.apache.shirogroupId>
         <artifactId>shiro-springartifactId>
         <version>1.4.0version>
      dependency>

      <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-testartifactId>
         <scope>testscope>
      dependency>
   dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
         plugin>
         
         <plugin>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-maven-pluginartifactId>
            <version>1.3.5version> <dependencies>
            
            <dependency>
               <groupId>mysqlgroupId>
               <artifactId>mysql-connector-javaartifactId>
               <version>5.1.44version>
            dependency>
            <dependency>
               <groupId>tk.mybatisgroupId>
               <artifactId>mapperartifactId>
               <version>3.4.0version>
            dependency>
         dependencies>
            <executions>
               <execution>
                  <id>Generate MyBatis Artifactsid>
                  <phase>packagephase>
                  <goals> <goal>generategoal>
                  goals>
               execution>
            executions>
            <configuration>
               
               <verbose>trueverbose>
               
               <overwrite>trueoverwrite>
               
               <configurationFile>src/main/resources/mybatis-generator-cfg.xmlconfigurationFile>
            configuration>
         plugin>
      plugins>
   build>
project>

通过测试方法生成表

        在添加依赖后,在SpringBoot的测试类ApplicationTests中写一个方法,如下:

       /**
	 * 测试生成Activiti的表结构
	 */
	@Test
	public void createTable(){
		//流程引擎配置
		ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();

		configuration.setJdbcDriver("com.mysql.jdbc.Driver");
		configuration.setJdbcUrl("jdbc:mysql://localhost:3306/itsm?characterEncoding=UTF-8&useUnicode=true");
		configuration.setJdbcUsername("root");
		configuration.setJdbcPassword("root");

		//DB_SCHEMA_UPDATE_FALSE = "false";//不能自动创建表,需要表存在
		//DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";//先删除表再创建表
		//DB_SCHEMA_UPDATE_TRUE = "true";//如果表不存在,自动创建表
		configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
		//根据引擎配置构建引擎
		ProcessEngine engine = configuration.buildProcessEngine();
	}

        运行此方法后会生成Activiti的25表,如下:

SpringBoot+MyBatis(通用Mapper+PageHelper+Generator)+Activiti集成及问题解决_第1张图片

通过配置文件生成表

        官网的配置文件如下:

SpringBoot+MyBatis(通用Mapper+PageHelper+Generator)+Activiti集成及问题解决_第2张图片

        官网的文档链接:Activiti5.x用户指导文档

        在项目的resources目录下创建activiti.cfg.xml配置文件【Activiti默认配置文件的名称就是activiti.cfg.xml】,官网上给的配置示例中使用的是h2数据库,把数据库的连接信息改成自己项目的数据库信息,然后在测试类写一个方法,生成数据库表。activiti.cfg.xml文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

    
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">

        <property name="databaseType" value="mysql"/>
        
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itsm?characterEncoding=UTF-8&useUnicode=true" />
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUsername" value="root" />
        <property name="jdbcPassword" value="root" />

        

        
        
        
        <property name="databaseSchemaUpdate" value="true" />

        
        <property name="history" value="full" />

        

        
        <property name="jobExecutorActivate" value="false" />
        
        
        
        
        <property name="dbIdentityUsed" value="false"/>

        
        
        

        
        <property name="activityFontName" value="宋体"/>
        <property name="labelFontName" value="宋体"/>
        <property name="annotationFontName" value="宋体"/>
    bean>

    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
    bean>

    
    
    <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
    
    <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
    
    <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
    
    <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
    
    <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
    
    <bean id="IdentityService" factory-bean="processEngine" factory-method="getIdentityService" />

beans>

        测试方法代码:

/**
	 * 测试生成Activiti的表结构
	 */
	@Test
	public void createTable(){
//		//流程引擎配置
//		ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
//
//		configuration.setJdbcDriver("com.mysql.jdbc.Driver");
//		configuration.setJdbcUrl("jdbc:mysql://localhost:3306/itsm?characterEncoding=UTF-8&useUnicode=true");
//		configuration.setJdbcUsername("root");
//		configuration.setJdbcPassword("root");
//
//		//DB_SCHEMA_UPDATE_FALSE = "false";//不能自动创建表,需要表存在
//		//DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";//先删除表再创建表
//		//DB_SCHEMA_UPDATE_TRUE = "true";//如果表不存在,自动创建表
//		configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP);
//		//根据引擎配置构建引擎
//		ProcessEngine engine = configuration.buildProcessEngine();

		ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
		ProcessEngine engine = configuration.buildProcessEngine();
	}
        

集成中遇到的问题及解决

        其实我在集成Activiti的时候,引入的依赖包和排除的依赖包比较包,原来是可以更简单的——引入activiti的starter,如下


    org.activiti
    activiti-spring-boot-starter-basic
    ${activiti.version}

但这样却引发了包冲突,排除冲突的架包之后,项目根本启动不了,很多异常,找了网上说的解决办法,比如在SpringBoot的应用入口类的注解@SpringBootApplication排除Activiti的一个安全配置类,还有在MyBatis及插件的依赖中排除特殊的架包,但依然启动不成功,没办法,只能是暂时换回这种比较原始的方法来集成。





你可能感兴趣的:(Spring,Boot,工作流)