日期:2014-05-16  浏览次数:20445 次

数据库表SEQUENCE产生

sagacity(睿智)架构

第三讲:数据库sequence产生

先抛一个问题:

????? 大家在做系统时对一些表要产生主键(一般以流水或日期加流水方式产生),大家一般怎么做呢?

????? 是不是:

????? select max(id) from table 或则通过hibernate自带的uuid等方式产生呢?

????? 这种做法非常痛苦,要想sequence有点意义(如日期+流水)就比较麻烦,效率也低,每次都去对应的表里面做全量查询!

????? sagacity中怎么做呢?

????? 我们通过一个TABLE_SEQUENCE表存放各个表的当前sequence,表结构如下:

???? create table SYS_TABLE_SEQUENCE
???? (
?????? SEQUENCE_NAME??????? varchar(200) not null,
?????? CURRENT_KEY_VALUE??? numeric(20,0) not null,
?????? DATE_VALUE?????????? numeric(8,0),
?????? primary key (SEQUENCE_NAME)
??? );

??? alter table SYS_TABLE_SEQUENCE comment '系统流水表';

??? 同时通过hibernate建立一个对象,SysTableSequence.java放在sagacity核心库中

??? 项目中的用法:

??? 在applicationContext.xml中配置sessionFactory时添加

??? <property name="mappingResources">
???<list>
????<value>
?????org/sagacity/framework/dao/model/SysTableSequence.hbm.xml
????</value>

????

???? 在DAO中我们如此调用

???? 1.单个流水:

???? BigDecimal seq=this.getBigDecimalSequence(PmPurchaseOrderList.class);

???? 2. 批量流水,取得的流水为第一个,其它的流水通过seq+1

???? BigDecimal seq=this.getBigDecimalSequence(PmPurchaseOrderList.class,size);

???? 3.日期+几位流水

???? BigDecimal getDateSequence(Class entityClass, Date nowDate,String dateStyle, int length);

???? 4.批量取日期+几位流水

???? BigDecimal getDateSequence(Class entityClass, Date nowDate,String dateStyle, int length, int size);

?

??? 代码片段如下:

???? /**
? * 根据对象类获取序号
? *
? * @param entityClass
? * @return
? * @throws CreateSequenceException
? */
?protected BigDecimal getBigDecimalSequence(Class entityClass)
???throws CreateSequenceException {
??return getBigDecimalSequence(entityClass, 1);
?}

?

?/**
? * @todo example 获取以日期开头的序列号:seqName:orgData size:10 now seqence:10000
? *?????? return: 10001 now seqence:10010
? * @param seqName
? * @param size
? * @return
? * @throws CreateSequenceException
? */
?protected BigDecimal getBigDecimalSequence(Class entityClass, int size)
???throws CreateSequenceException {
??if (entityClass == null || size < 1)
???throw new CreateSequenceException("Input Parameters has Null!");
??else
???return getSequence(entityClass.getName(), null, null, null, size,
?????false);
?}

?

?/**
? * 根据日期获取SEQNO
? * @param entityClass
? * @param nowDate
? * @param dateStyle
? * @param length
? * @return
? * @throws CreateSequenceException
? */
?protected BigDecimal getDateSequence(Class entityClass, Date nowDate,
???String dateStyle, int length) throws CreateSequenceException {
??return getDateSequence(entityClass, nowDate, dateStyle, length, 1);
?}

?

?/**
? * @todo 获取以日期开头的序列号example:nowSeq=20051118001 ,
? *?????? condition:dateValue=20051118;length=3,size=4 update seq:20051118005
? *?????? return:20051118002
? * @param entityClass
? * @param nowDate
? * @param dateStyle
? * @param length
? * @param size
? * @return
? * @throws CreateSequenceException
? */
?protected BigDecimal getDateSequence(Class entityClass, Date nowDate,
???String dateStyle, int length, int size)
???throws CreateSequenceException {
??if (entityClass == null || length < 1 || size < 1)
???throw new CreateSequenceException("Input Parameters has Null!");
??else
???return