Search in sources :

Example 1 with IdGenerator

use of org.sagacity.sqltoy.plugins.id.IdGenerator in project sagacity-sqltoy by chenrenfei.

the class SqlToyDaoSupport method generateBizId.

/**
 * @todo 根据实体对象对应的POJO配置的业务主键策略,提取对象的属性值产生业务主键
 * @param entity
 * @return
 */
protected String generateBizId(Serializable entity) {
    EntityMeta entityMeta = this.getEntityMeta(entity.getClass());
    if (entityMeta == null || !entityMeta.isHasBizIdConfig()) {
        throw new IllegalArgumentException(StringUtil.fillArgs("对象:{},没有配置业务主键生成策略,请检查POJO 的业务主键配置!", entity.getClass().getName()));
    }
    String businessIdType = entityMeta.getColumnJavaType(entityMeta.getBusinessIdField());
    Integer[] relatedColumn = entityMeta.getBizIdRelatedColIndex();
    Object[] fullParamValues = BeanUtil.reflectBeanToAry(entity, entityMeta.getFieldsArray());
    // 提取关联属性的值
    Object[] relatedColValue = null;
    if (relatedColumn != null) {
        relatedColValue = new Object[relatedColumn.length];
        for (int meter = 0; meter < relatedColumn.length; meter++) {
            relatedColValue[meter] = fullParamValues[relatedColumn[meter]];
            if (relatedColValue[meter] == null) {
                throw new IllegalArgumentException("对象:" + entity.getClass().getName() + " 生成业务主键依赖的关联字段:" + relatedColumn[meter] + " 值为null!");
            }
        }
    }
    IdGenerator idGenerator = (entityMeta.getBusinessIdGenerator() == null) ? entityMeta.getIdGenerator() : entityMeta.getBusinessIdGenerator();
    return idGenerator.getId(entityMeta.getTableName(), entityMeta.getBizIdSignature(), entityMeta.getBizIdRelatedColumns(), relatedColValue, new Date(), businessIdType, entityMeta.getBizIdLength(), entityMeta.getBizIdSequenceSize()).toString();
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) RedisIdGenerator(org.sagacity.sqltoy.plugins.id.impl.RedisIdGenerator) IdGenerator(org.sagacity.sqltoy.plugins.id.IdGenerator) Date(java.util.Date)

Example 2 with IdGenerator

use of org.sagacity.sqltoy.plugins.id.IdGenerator in project sagacity-sqltoy by chenrenfei.

the class ShardingUtils method assignPKs.

/**
 * @todo 批量主键赋值
 * @param sqlToyContext
 * @param entityMeta
 * @param entities
 * @throws Exception
 */
public static void assignPKs(SqlToyContext sqlToyContext, EntityMeta entityMeta, List<?> entities) throws Exception {
    IdGenerator idGenerator = entityMeta.getIdGenerator();
    String[] pks = entityMeta.getIdArray();
    // 存在主键策略,且只能是单主键
    if (idGenerator == null || pks == null || pks.length > 1) {
        return;
    }
    if (idGenerator != null) {
        String table = entityMeta.getTableName();
        String idType = entityMeta.getIdType();
        // 业务主键跟主键重叠,已经将主键长度设置为业务主键长度
        int idLength = entityMeta.getIdLength();
        int sequenceSize = entityMeta.getBizIdSequenceSize();
        String[] reflectColumns = entityMeta.getFieldsArray();
        // 标识符
        String signature = entityMeta.getBizIdSignature();
        Integer[] relatedColumnIndex = entityMeta.getBizIdRelatedColIndex();
        List<Object[]> ids = BeanUtil.reflectBeansToInnerAry(entities, pks, null, null);
        Object pkValue;
        Object[] relatedColValue = null;
        Object[] fullParamValues;
        for (int i = 0; i < entities.size(); i++) {
            pkValue = ids.get(i)[0];
            // 主键值未赋予,则自动赋予
            if (pkValue == null || pkValue.toString().trim().equals("")) {
                if (entityMeta.isBizIdEqPK()) {
                    fullParamValues = BeanUtil.reflectBeanToAry(entities.get(i), reflectColumns);
                    if (relatedColumnIndex != null) {
                        relatedColValue = new Object[relatedColumnIndex.length];
                        for (int meter = 0; meter < relatedColumnIndex.length; meter++) {
                            relatedColValue[meter] = fullParamValues[relatedColumnIndex[meter]];
                            if (relatedColValue[meter] == null) {
                                throw new IllegalArgumentException("对象:" + entityMeta.getEntityClass().getName() + " 生成业务主键依赖的关联字段:" + relatedColumnIndex[meter] + " 值为null!");
                            }
                        }
                    }
                }
                // 回写主键值
                BeanUtil.setProperty(entities.get(i), pks[0], idGenerator.getId(table, signature, entityMeta.getBizIdRelatedColumns(), relatedColValue, null, idType, idLength, sequenceSize));
            }
        }
    }
}
Also used : IdGenerator(org.sagacity.sqltoy.plugins.id.IdGenerator)

Aggregations

IdGenerator (org.sagacity.sqltoy.plugins.id.IdGenerator)2 Date (java.util.Date)1 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)1 RedisIdGenerator (org.sagacity.sqltoy.plugins.id.impl.RedisIdGenerator)1