Spring boot + activiti有两个数据源

时间:2016-01-09 13:15:17

标签: java spring-boot datasource activiti

我需要将我的数据模型与activiti分开。 Activiti的表是在主数据源中正确创建的,但我的另一个数据库是空的。我该怎么办?

的pom.xml

CREATE TABLE my_course
(my_course_firstyear VARCHAR2(2),
my_course_secondyear NUMBER(10) NOT NULL,
my_course_final VARCHAR2(10)); 

ALTER TABLE my_course
modify
my_course_final VARCHAR2(20),
my_course_secondyear || mycoursefinal);

我的主数据源应用程序

<?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.multipledb.activiti</groupId>
    <artifactId>demo-miltibledb-activiti</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo-miltibledb-activiti</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <activiti.version>5.19.0</activiti.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-basic</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-rest-api</artifactId>
            <!--<artifactId>spring-boot-starter-rest-api</artifactId> -->
            <version>${activiti.version}</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-actuator</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-jpa</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0.2206.100</version>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

配置第二个数据源

package com.multipledb.activiti;

import javax.sql.DataSource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

//@SpringBootApplication
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class DemoMiltibledbActivitiApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoMiltibledbActivitiApplication.class, args);
    }

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource database() {
        return DataSourceBuilder.create().build();
    }
}

application.properties

package com.multipledb.activiti;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@ComponentScan
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "dmEntityManagerFactory", transactionManagerRef = "dmTransactionManager", basePackages = { "com.multipledb.activiti.repo" })
public class DatamodelConfig {
    @Bean(name = "dmDataSource")
    @ConfigurationProperties(prefix = "dm.datasource")
    public DataSource dmDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "dmEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean dmEntityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("dmDataSource") DataSource barDataSource) {
        return builder.dataSource(dmDataSource())
                .packages("com.multipledb.activiti.domain")
                .persistenceUnit("datamodel").build();
    }

    @Bean(name = "dmTransactionManager")
    public PlatformTransactionManager dmTransactionManager(
            @Qualifier("dmEntityManagerFactory") EntityManagerFactory dmEntityManagerFactory) {
        return new JpaTransactionManager(dmEntityManagerFactory);
    }
}

实体申请人

spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=activiti
spring.datasource.username=activiti
spring.datasource.password= activiti
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

dm.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=data
dm.datasource.username=activiti
dm.datasource.password= activiti
dm.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

申请人资料库

package com.multipledb.activiti.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


@Entity
public class Applicant {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private String email;

    private String phoneNumber;

    public Applicant() {

    }

    public Applicant(String name, String email, String phoneNumber) {
        this.name = name;
        this.email = email;
        this.phoneNumber = phoneNumber;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}

由于

1 个答案:

答案 0 :(得分:0)

您需要配置Activiti以使用辅助数据源(dm)。 这是通过自定义SpringProcessEngineConfiguration

完成的

我已就此相关问题提供了一个示例:https://stackoverflow.com/a/35956539/2408961