1.1.1.  activiti默认主键生成方式

;

下面我们看一下主键的生成策略:主键的生成策略定义在IdGenerator接口中,接口定义如下所示:

public interface IdGenerator {
  String getNextId();
}

getNextId()方法定义了主键生成的策略,每次需要主键的时候直接从getNextId()方法中获取即可。

下面看一下IdGenerator接口的实现类,具体的实现结构如下:

1.DbIdGenerator默认的方式,没有依赖第三方jar包。

2.org.activiti.engine.impl.persistence.StrongUuidGenerator类使用的uuid生成主键的策略。依赖第三方包com.fasterxml.uuid.impl.TimeBasedGenerator 使用的时候需要添加jar包。具体的maven仓库如下所示:

<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>3.1.4</version>
</dependency>

使用的时候,如果没有上面的jar包,程序报错不能使用。

1.1.2. activiti主键生成策略源码初始化分析

首先进入org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl类 查看initIdGenerator() 方法,这个方法是主键生成策略的选定。

protected void initIdGenerator() {
//用户自定义主键生成策略,优先使用自定义生成策略
    if (idGenerator==null) {
      CommandExecutor idGeneratorCommandExecutor = null;
//idGeneratorDataSource专门的主键生成的datasource配置
      if (idGeneratorDataSource!=null) {
        ProcessEngineConfigurationImpl processEngineConfiguration = new StandaloneProcessEngineConfiguration();
        processEngineConfiguration.setDataSource(idGeneratorDataSource);
        processEngineConfiguration.setDatabaseSchemaUpdate(DB_SCHEMA_UPDATE_FALSE);
        processEngineConfiguration.init();
        idGeneratorCommandExecutor = processEngineConfiguration.getCommandExecutor();
      } else if (idGeneratorDataSourceJndiName!=null) {
//jndi方式生成id,

分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519)

        ProcessEngineConfigurationImpl processEngineConfiguration = new StandaloneProcessEngineConfiguration();
        processEngineConfiguration.setDataSourceJndiName(idGeneratorDataSourceJndiName);
        processEngineConfiguration.setDatabaseSchemaUpdate(DB_SCHEMA_UPDATE_FALSE);
        processEngineConfiguration.init();
        idGeneratorCommandExecutor = processEngineConfiguration.getCommandExecutor();
      } else {
        idGeneratorCommandExecutor = getCommandExecutor();
      }
//上面的两种方式都没有使用,则使用默认的主键生成策略
      DbIdGenerator dbIdGenerator = new DbIdGenerator();
      dbIdGenerator.setIdBlockSize(idBlockSize);
      dbIdGenerator.setCommandExecutor(idGeneratorCommandExecutor);
      dbIdGenerator.setCommandConfig(getDefaultCommandConfig().transactionRequiresNew());
      idGenerator = dbIdGenerator;
    }
  }

1.1.3. 自定义IdGenerator生成策略

1.1.3.1. 自定义类

@Service
public class UUIDGenerator implements IdGenerator {
@Override
public String getNextId() {

分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519)

return UUID.randomUUID().toString().replace("-", "");
}
}

1.1.3.2. 配置使用

@Service
public class MyProcessEngineConfigurator  extends AbstractProcessEngineConfigurator{
@Autowired
private UUIDGenerator uUIDGenerator;

分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519)

@Override
public void beforeInit(
ProcessEngineConfigurationImpl processEngineConfiguration) {
DataSource dataSource = JdbcUtils.getReadDataSource();
processEngineConfiguration.setDataSource(dataSource);
processEngineConfiguration.setHistoryLevel(HistoryLevel.FULL);
processEngineConfiguration.setDbIdentityUsed(false);
processEngineConfiguration.setProcessEngineLifecycleListener(myProcessEngineConfigurator);
processEngineConfiguration.setIdGenerator(uUIDGenerator);
}
}

1.1.4. activiti主键策略小结

小结:

1.默认的DbIdGenerator生成策略,数据量不大的情况下,看起来直观一点,可以快速定位要需要查询的视图中。当数据量大的时候,看起来也不直观。

2.默认的DbIdGenerator生成策略,主键的id存储在数据库,缓存中没有的时候,回去查询数据库,所以如果频繁的查询数据库生成主键,不可取。对性能有影响。并发大的时候,此方案不可取。

3.内置的StrongUuidGenerator生成策略,生成的是UUID,缺点:使用的时候需要添加第三方的jar包。感觉没有必要生成uuid的时候,使用第三方工具。

使用自己的uuid生成类,简单明了,可以自己控制生成的策略,不需要第三方的生成工具,直接使用的jdk中的java.util.UUID类。可控性更强。

分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519)

activiti uuid主键的更多相关文章

  1. hibernate annotation 生成uuid主键

    JPA标准方式下,不可以生成uuid类型的主键,但是hibernate提供了一些方式生成uuid主键,具体如下: 1.主键生成器     @GeneratedValue(generator=" ...

  2. MySql中利用insert into select 准备数据uuid主键冲突

    MYSQL 中表1需要准备大量数据,内容主要取自表2,id必须为32位uuid (项目所有表都是这样,没办法), 准备这样插入: INSERT INTO TBL_ONE (ID, SOID, SNAM ...

  3. mybatis 主键UUID生成策略

    <insert id="insert" parameterType="com.lsfwpt.lawmis.po.SysUser"> <sele ...

  4. MySQL 使用自增ID主键和UUID 作为主键的优劣比较详细过程(从百万到千万表记录测试)

    测试缘由 一个开发同事做了一个框架,里面主键是uuid,我跟他建议说mysql不要用uuid用自增主键,自增主键效率高,他说不一定高,我说innodb的索引特性导致了自增id做主键是效率最好的,为了拿 ...

  5. MySQL 使用自增ID主键和UUID 作为主键的优劣比較具体过程(从百万到千万表记录測试)

      主键类型 SQL语句 运行时间 (秒)   (1)模糊范围查询1000条数据,自增ID性能要好于UUID 自增ID SELECT SQL_NO_CACHE t.* FROM test.`UC_US ...

  6. MySQL主键设计

    [TOC] 在项目过程中遇到一个看似极为基础的问题,但是在深入思考后还是引出了不少问题,觉得有必要把这一学习过程进行记录. MySQL主键设计原则 MySQL主键应当是对用户没有意义的. MySQL主 ...

  7. insert主键返回 selectKey使用

    有时候新增一条数据,知道新增成功即可,但是有时候,需要这条新增数据的主键,以便逻辑使用,再将其查询出来明显不符合要求,效率也变低了. 这时候,通过一些设置,mybatis可以将insert的数据的主键 ...

  8. Hibernate主键生成策略及选择

    1 .increment:适用于short,int,long作为主键,不是使用数据库自动增长机制 这是hibernate中提供的一种增长机制 在程序运行时,先进行查询:select max(id) f ...

  9. Mybatis里Mapper映射sql文件里insert的主键返回selectKey使用

    有时候新增一条数据,知道新增成功即可,但是有时候,需要这条新增数据的主键,以便逻辑使用,再将其查询出来明显不符合要求,效率也变低了. 这时候,通过一些设置,mybatis可以将insert的数据的主键 ...

随机推荐

  1. [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  2. Python默认版本切换

    Mac上自带python2.7 版本,但是我又下了一个3.7版本(下载的版本默认安装在 /Library/Frameworks/Python.framework/Versions/3.7/bin/py ...

  3. 机器学习技法:05 Kernel Logistic Regression

    Roadmap Soft-Margin SVM as Regularized Model SVM versus Logistic Regression SVM for Soft Binary Clas ...

  4. dnslog搭建

    为什么想重写这个呢,想说后面扫描ssrf和命令执行的时候,能快速改成自己想要的api,更容易修改一些. 工具改自:https://github.com/bugScanTeam/DNSLog 需要两个域 ...

  5. 个人建站&mac下安装hexo

    title: 个人建站&mac下安装hexo date: 2018-04-18 16:34:02 tags: [mac,blog,个人建站,markdown] --- 这两天使用了markdo ...

  6. bzoj 4830: [Hnoi2017]抛硬币

    Description 小A和小B是一对好朋友,他们经常一起愉快的玩耍.最近小B沉迷于**师手游,天天刷本,根本无心搞学习.但是 已经入坑了几个月,却一次都没有抽到SSR,让他非常怀疑人生.勤勉的小A ...

  7. [Codeforces 864D]Make a Permutation!

    Description Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to ...

  8. [SDOI2016]排列计数

    Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是 ...

  9. [Codeforces]862F - Mahmoud and Ehab and the final stage

    题目大意:n个字符串,支持修改一个位置上的字符串和查询一个区间的子区间中长度乘LCP的最大值,输入字符数和询问数不超过10^5. 做法:求出相邻的LCP长度,区间LCP等于区间最小值,查询分几种情况考 ...

  10. Codeforces Round #411 (Div. 2)

    来自FallDream的博客,未经允许,请勿转载,谢谢. 由于人傻又菜 所以这次又滚去div2了  一堆结论题真的可怕 看见E题不是很有思路   然后就去大力搞F题  T了最后一个点 真的绝望   但 ...