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] Next Greater Element II 下一个较大的元素之二

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  2. MySql 使用规范推荐

    前言 废话不多说-- 一.基础规范 1.使用InnoDB存储引擎 支持事务.行级锁.并发性能更好.CPU及内存缓存页优化使得资源利用率更高 2.推荐使用utf8mb4字符集 无需转码,无乱码风险, 支 ...

  3. git中的merge与rebase

    之前一直对git的merge与rebase很困惑,而且一般也只使用merge而不是使用rebase.今天受高人指点理清了两者的区别. 首先对于两者而言,他们的结果是一样的,差异在于合并的方式(产生的结 ...

  4. Basic command and advice for memcached

    Storage Commands set Most common command. Store this data, possibly overwriting any existing data. N ...

  5. [BZOJ]4805: 欧拉函数求和

    解题思路类似莫比乌斯函数之和 题目大意:求[1,n]内的欧拉函数$\varphi$之和.($n<=2*10^{9}$) 思路:令$ M(n)=\sum_{i=1}^{n}\varphi (i)  ...

  6. ●POJ 1873 The Fortified Forest

    题链: http://poj.org/problem?id=1873 题解: 计算几何,凸包 枚举被砍的树的集合.求出剩下点的凸包.然后判断即可. 代码: #include<cmath> ...

  7. 2017ACM/ICPC广西邀请赛-重现赛 1007.Duizi and Shunzi

    Problem Description Nike likes playing cards and makes a problem of it. Now give you n integers, ai( ...

  8. [2017/5/28]FJ四校联考

    来自FallDream的博客,未经允许,请勿转载,谢谢. 话说这一段时间算是过去了,好久好久之后终于又有联考了  没想到这次到我们学校出题,昨天才想起来,临时花一天赶了一套,我出了一个sbFFT,质量 ...

  9. # C语言程序设计第一次作业1234

    ---恢复内容开始--- C语言程序设计第一次作业 1.求圆面积和周长 输入圆的半径,计算圆的周长和面积 (1)流程图 (2)测试数据及运行结果 测试数据r=3 运行结果 2.判断闰年 输入一个四位年 ...

  10. 西安电话面试:谈谈Vue数据双向绑定原理,看看你的回答能打几分

    最近我参加了一次来自西安的电话面试(第二轮,技术面),是大厂还是小作坊我在这里按下不表,先来说说这次电面给我留下印象较深的几道面试题,这次先来谈谈Vue的数据双向绑定原理. 情景再现: 当我手机铃声响 ...