二、Activiti-自定義用戶表

在很多項(xiàng)目中都不會(huì)使用activiti提供的用戶信息表,因?yàn)橐话愎径紩?huì)有獨(dú)立的用戶系統(tǒng),所以重構(gòu)activiti的用戶表是十分棘手且重要的事情。activiti中用戶表是已a(bǔ)ct_id_*開頭的四張表,分別是用戶信息表、分組(角色)表、用戶角色關(guān)聯(lián)表、用戶擴(kuò)展信息表。具體數(shù)據(jù)表字段這里不展開敘述,大家有需要可以去網(wǎng)上查閱,接下來(lái)闡述怎么棄用activiti流程引擎中自帶的4張用戶表,構(gòu)建自己的用戶表。在重構(gòu)的時(shí)候著實(shí)費(fèi)了一番功夫,記錄下來(lái)以備以后翻閱,也為大家提供一個(gè)參考。

1.POM文件依賴引入

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--activiti-->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-basic</artifactId>
            <version>5.22.0</version>
        </dependency>
        <!-- shiro -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.4.0</version>
        </dependency>
        <!--mybatis start-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!--mybatis generator-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!--mybatis end-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--json-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
    </dependencies>

2.配置文件

server.port=8080
#數(shù)據(jù)庫(kù)配置
spring.datasource.url = jdbc:mysql://localhost:3306/roc_process?characterEncoding=utf8&useSSL=false
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#關(guān)閉activiti自動(dòng)部署掃描
spring.activiti.check-process-definitions=false
#上傳文件大小配置
spring.servlet.multipart.max-file-size=5MB
#請(qǐng)求最大限制
spring.servlet.multipart.max-request-size=20MB
##指向mapper的xml文件位置
mybatis.mapper-locations=classpath:mapper/*.xml

3.自定義session工廠

重點(diǎn)來(lái)了:

Activiti的每一張表都有一個(gè)對(duì)應(yīng)的實(shí)體管理器,在引擎初始化時(shí)會(huì)初始化所有表的實(shí)體管理器(提供CRUD等功能),每一個(gè)實(shí)體類都有一個(gè)對(duì)應(yīng)的實(shí)體管理類及實(shí)體管理工廠類。實(shí)體管理工廠類實(shí)現(xiàn)SeesionFactory接口。

開始前先看一下我項(xiàng)目中的用戶數(shù)據(jù)表字段。

用戶信息表:

//用戶編號(hào)
 private String id;
//用戶昵稱,用于在界面顯示
 private String nickname;
//用戶名
 private String username;
//密碼
 private String password;
//加密鹽值
 private String salt;
//狀態(tài)信息
 private Integer status;

角色表

    private String id;
    //角色名稱
    private String role;
    //描述
    private String description;
    //是否可用
    private Integer enable;
    //權(quán)限信息
    private String permissions;

用戶、角色關(guān)聯(lián)表

 private Integer id;
 //用戶id
 private String userId;
 //角色id
 private String roleId;

其他數(shù)據(jù)表這里不做展示,關(guān)鍵的就是這三張表。

1).編寫User的實(shí)體管理類

/**
 * 自定義用戶管理類,管理用戶方法
 * 添加其他方法
 */
@Component
public class CustomUserEntityManager extends UserEntityManager{
    @Autowired
    private RocIdUserMapper rocIdUserMapper;
    @Autowired
    private RocIdRoleMapper rocIdRoleMapper;
    @Autowired
    private RocIdUserRoleMapper rocIdUserRoleMapper;
    @Override
    public User findUserById(String userId){
        User userEntity=new UserEntity();
        RocIdUser rocIdUser=rocIdUserMapper.selectByPrimaryKey(userId);
        //將自定義的user轉(zhuǎn)化為activiti的類
        userEntity=ActivitiUserUtils.toActivitiUser(rocIdUser);
        //返回activiti的實(shí)體類
        return userEntity;
    }

    @Override
    public List<Group> findGroupsByUser(final String userId) {
        if(userId==null){
            return null;
        }
        List<RocIdRole> roleList=new ArrayList<RocIdRole>();
        List<RocIdUserRole> userRoleList=rocIdUserRoleMapper.selectByUserId(userId);
        for (RocIdUserRole userrole:userRoleList
             ) {
            String roleId=userrole.getRoleId();
            RocIdRole role=rocIdRoleMapper.selectByPrimaryKey(roleId);
            roleList.add(role);
        }
        List<Group> gs=null;
        gs=ActivitiUserUtils.toActivitiGroups(roleList);
        return gs;
    }
}

2).編寫Group的實(shí)體管理類

/**
 * 自定義角色管理
 * 具體方法進(jìn)入GroupEntityManager中查看
 */
@Component
public class CustomGroupEntityManager extends GroupEntityManager{
    @Autowired
    private RocIdUserMapper rocIdUserMapper;
    @Autowired
    private RocIdUserRoleMapper rocIdUserRoleMapper;
    @Autowired
    private RocIdRoleMapper rocIdRoleMapper;

    @Override
    public Group createNewGroup(String groupId) {
        return super.createNewGroup(groupId);
    }

    /**
     * 查找角色
     * @param userId
     * @return
     */
    @Override
    public List<Group> findGroupsByUser(final String userId) {
        if(userId==null){
            return null;
        }
        System.out.println("userId:"+userId);
        RocIdUser rocIdUser=rocIdUserMapper.selectByUserName(userId);
        List<RocIdUserRole> userRoleList=rocIdUserRoleMapper.selectByUserId(rocIdUser.getId());
        System.out.println("userRoleList size:"+userRoleList.size());
        List<Group> gs=new ArrayList<Group>();
        GroupEntity groupEntity;
        String roleId;
        String activitiRole;
        for (RocIdUserRole userRole:userRoleList
             ) {
            groupEntity=new GroupEntity();
            groupEntity.setRevision(1);
            groupEntity.setType("assignment");
            roleId=userRole.getRoleId();
            RocIdRole role=rocIdRoleMapper.selectByPrimaryKey(roleId);
            groupEntity.setId(role.getRole());
            groupEntity.setName(role.getRole());
            gs.add(groupEntity);
        }
        return gs;
    }

}

3).編寫User的管理工廠類

/**
 * 自定義user的管理工廠類
 */
@Service
public class CustomUserEntityManagerFactory implements SessionFactory{

    @Resource
    private CustomUserEntityManager customUserEntityManager;

    @Override
    public Class<?> getSessionType() {
        //此處也必須為activiti原生類
        return UserIdentityManager.class;
    }

    @Override
    public Session openSession() {
        return customUserEntityManager;
    }
    @Autowired
    public void setCustomUserEntityManager(CustomUserEntityManager customUserEntityManager){
        this.customUserEntityManager=customUserEntityManager;
    }
}

4).編寫Group的管理工廠類

/**
 * 角色類的管理
 */
@Service
public class CustomGroupEntityManagerFactory implements SessionFactory{
    @Resource
    private CustomGroupEntityManager customGroupEntityManager;

    @Override
    public Class<?> getSessionType() {
        //返回原始的groupmanager類型
        return GroupIdentityManager.class;
    }

    @Override
    public Session openSession() {
        //返回自定義的GroupManager實(shí)例
        return customGroupEntityManager;
    }

    @Autowired
    public void setCustomGroupEntityManager(CustomGroupEntityManager customGroupEntityManager){
        this.customGroupEntityManager=customGroupEntityManager;
    }
}

5).編寫工具類,將我們自定義的實(shí)體類轉(zhuǎn)換成Activiti的實(shí)體類。

/**
 * 將業(yè)務(wù)中自己定義的用戶、角色轉(zhuǎn)化為activiti中使用的user、group
 */
public class ActivitiUserUtils {
    public static User toActivitiUser(RocIdUser bUser){
        User userEntity=new UserEntity();
        userEntity.setId(bUser.getUsername());
        userEntity.setFirstName(bUser.getNickname());
        userEntity.setLastName(bUser.getNickname());
        userEntity.setPassword(bUser.getPassword());
        return userEntity;
    }
    public static GroupEntity toActivitiGroup(RocIdRole sysRole){
        GroupEntity groupEntity=new GroupEntity();
        groupEntity.setRevision(1);
        groupEntity.setType("assignment");
        groupEntity.setId(sysRole.getRole());
        groupEntity.setName(sysRole.getRole());
        return groupEntity;
    }
    public static List<Group> toActivitiGroups(List<RocIdRole> sysRoles){
        List<Group> groups=new ArrayList<Group>();
        for (RocIdRole role:sysRoles
             ) {
            GroupEntity groupEntity=toActivitiGroup(role);
            groups.add(groupEntity);
        }
        return groups;
    }
}

6).activiti.cfg.xml中進(jìn)行配置

    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          id="transactionManager">
        <!--注入數(shù)據(jù)庫(kù)連接池-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/roc_process?useUnicode=true&amp;characterEncoding=utf8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
    <!-- Activiti引擎配置 -->
    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource"/>
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseSchemaUpdate" value="true"/>
        <!-- 自定義SessionFactory -->
        <property name="customSessionFactories">
            <list>
                <bean class="com.uestc.roc.process.config.identity.CustomUserEntityManagerFactory">
                    <property name="customUserEntityManager" ref="customUserEntityManager">
                    </property>
                </bean>
                <bean class="com.uestc.roc.process.config.identity.CustomGroupEntityManagerFactory">
                    <property name="customGroupEntityManager" ref="customGroupEntityManager">
                    </property>
                </bean>
            </list>
        </property>
    </bean>
    <bean id="customUserEntityManager" class="com.uestc.roc.process.config.identity.CustomUserEntityManager">
    </bean>
    <bean id="customGroupEntityManager" class="com.uestc.roc.process.config.identity.CustomGroupEntityManager">
    </bean>

4.啟動(dòng)類中引入配置文件

@SpringBootApplication(exclude = SecurityAutoConfiguration.class)   //不添加會(huì)報(bào)錯(cuò)
@ImportResource(locations = {"classpath:activiti.cfg.xml"})  //引入xml配置文件
public class ProcessApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProcessApplication.class, args);
    }
}

總結(jié)

其實(shí)activiti用戶表的重構(gòu)核心在于編寫自定義的實(shí)體管理類和工廠類,覆蓋activiti原來(lái)的方法,在方法中進(jìn)行增刪查改時(shí)使用dao接口進(jìn)行操作,最后將得到的對(duì)象轉(zhuǎn)化為activiti的對(duì)象,不然會(huì)出錯(cuò)。


致謝
activiti 自定義用戶:https://blog.csdn.net/meng564764406/article/details/53789958

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容