SpringBoot整合Activiti學習(一)

回首往昔發表於2020-11-29

前言

只有學到手的才是自己的,加油,boy!!!

環境

JDK版本JAVA version "1.8.0_212"
IDEA2020.1
SpringBoot2.3.2.RELEASE
activiti-spring-boot-starter7.1.0.M5

搭建

pom結構如下

 

<?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.0</modelVersion>

    <groupId>com.code</groupId>
    <artifactId>activiti_demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <description>
        本節主要在於學習activiti, 話不多說, 肝就完事
    </description>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <spring.boot.version>2.3.2.RELEASE</spring.boot.version>
        <activiti.version>7.1.0.M1</activiti.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter</artifactId>
            <version>${activiti.version}</version>
        </dependency>

        <!--web操作-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--驅動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--測試-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!--mybatis-->
        <!--<dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

yml配置

server:
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql:///activiti?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456

  # activiti配置
  activiti:
    history-level: full
    db-history-used: true
    database-schema-update: true

注意: 因為activiti整合了security,因此需要加上部分程式碼,這段程式碼可以在官方例子程式碼中找到

例子連結

@Component
public class SecurityUtil {

    private Logger logger = LoggerFactory.getLogger(SecurityUtil.class);

    @Autowired
    private UserDetailsService userDetailsService;

    public void logInAs(final String username) {

        final UserDetails user = userDetailsService.loadUserByUsername(username);
        if (user == null) {
            throw new IllegalStateException("User " + username + " doesn't exist, please provide a valid user");
        }
        logger.info("> Logged in as: " + username);
        SecurityContextHolder.setContext(new SecurityContextImpl(new Authentication() {
            public Collection<? extends GrantedAuthority> getAuthorities() {
                return user.getAuthorities();
            }

            public Object getCredentials() {
                return user.getPassword();
            }

            public Object getDetails() {
                return user;
            }

            public Object getPrincipal() {
                return user;
            }

            public boolean isAuthenticated() {
                return true;
            }

            public void setAuthenticated(boolean b) throws IllegalArgumentException {

            }

            public String getName() {
                return user.getUsername();
            }
        }));
        org.activiti.engine.impl.identity.Authentication.setAuthenticatedUserId(username);
    }
}

到此基本算是搭建完成...

執行專案,會生成如下25張表

act_evt_log -- 估計是日誌一類的

act_ge_* -- 是儲存部署的資源的,下一篇我們就可以看到

act_hi_* --是歷史一類的,在yml裡面配置之後才會生成歷史相關的表

act_ru_* -- 不用說了,例項執行一類的相關記錄表

相關文章