首页>代码>ssm网站后台管理系统Espread,基于Spring+SpringMVC+Mybatis+Shiro+Quartz+Maven+Easyui技术>/espread/src/main/java/com/espread/common/aspect/SequenceAspect.java
001package com.espread.common.aspect;
002 
003import com.espread.common.annotation.SysSeqHelper;
004import com.espread.common.exception.SequenceAspectException;
005import com.espread.common.utils.FormatUtils;
006import com.espread.common.utils.StringUtils;
007import com.espread.sys.mapper.SysSequenceMapper;
008import com.espread.sys.model.SysSequence;
009import org.aspectj.lang.JoinPoint;
010import org.aspectj.lang.annotation.Aspect;
011import org.aspectj.lang.annotation.Before;
012import org.aspectj.lang.annotation.Pointcut;
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015import org.springframework.beans.factory.annotation.Autowired;
016import org.springframework.stereotype.Component;
017 
018import java.lang.reflect.Field;
019import java.util.UUID;
020import java.util.regex.Matcher;
021import java.util.regex.Pattern;
022 
023/**
024 * 主键字段-序列注入
025 * @author itdragons 2016-08-13 23:56:50
026 * 通过一张表维护主键序列
027 */
028@Component
029@Aspect
030public class SequenceAspect  {
031     
032    private Logger LOGGER = LoggerFactory.getLogger(SequenceAspect.class);
033 
034    @Autowired
035    private SysSequenceMapper sysSequenceMapper;
036    private final int maxNum = 5;//序列最大获取次数
037     
038    /**
039     * 切入点
040     */
041    @Pointcut(" execution(* com.espread.*.mapper.**.insert*(..))")
042    public void aspect(){}
043     
044    @Before("aspect()")
045    public void before(JoinPoint point){
046        Object[] args = point.getArgs();
047        if(args.length == 1){
048            Object obj = args[0];
049            Field[] fields = obj.getClass().getDeclaredFields();//获取内部所有字段
050            String newSeq = null;
051            for(Field field : fields){
052                field.setAccessible(true); //设置些属性是可以访问的
053                SysSeqHelper sysSeqHelper = field.getAnnotation(SysSeqHelper.class);
054                if(sysSeqHelper != null){
055                    String seqType = sysSeqHelper.type();
056                    String seqCode = sysSeqHelper.value();
057                    //情况1:类型默认为空
058                    if(StringUtils.isBlanks(seqType)){
059                        if (StringUtils.isBlanks(seqCode)) { // SeqCode为空的情况,返回UUID
060                            newSeq = UUID.randomUUID().toString();
061                        } else {
062                            newSeq = getSequence(seqCode);//获取新序列
063                        }
064                    //情况2:根据类型处理
065                    } else {
066                        if (sysSeqHelper.UUDI.equals(seqType)) { //类型为UUID
067                            newSeq = UUID.randomUUID().toString();
068                        } else if (sysSeqHelper.DB.equals(seqType)){ //从数据库获取
069                            if(StringUtils.isBlanks(seqCode)){
070                                newSeq = getSequence(obj.getClass());
071                            }else{
072                                newSeq = getSequence(seqCode);//获取新序列
073                            }
074                        }else{
075                            throw new SequenceAspectException("注入主键序列异常,注解type参数错误(" + obj + ")");
076                        }
077                    }
078                    try {
079                        if (field.getType() == Integer.class) {//主键为Integer类型
080                            String regEx="[^0-9]";  
081                            Matcher m = Pattern.compile(regEx).matcher(newSeq); 
082                            Integer newSeqInt =  Integer.valueOf(m.replaceAll("").trim());
083                            field.set(obj, newSeqInt);
084                        } else {
085                            field.set(obj, newSeq);
086                        }
087                    } catch (Exception e) {
088                        throw new SequenceAspectException("注入主键序列异常:" + obj );
089                    }
090                    break;
091                }
092            }
093        }
094    }
095     
096     
097    /**
098     * 获取序列
099     * @param seqCode
100     * @return
101     */
102    public String getSequence(String seqCode) {
103        //序列
104        Integer newSeq = null ;
105        SysSequence sysSequence = null;
106         
107        int forNum = 0;//当前获取次数
108        int resultCount = 0; //update result
109        while(resultCount == 0 && forNum < maxNum){
110            sysSequence = sysSequenceMapper.selectByPrimaryKey(seqCode);
111            if(sysSequence == null){
112                String errMSg = "序列查询失败,请检查数据库序列表配置:" + seqCode;
113                LOGGER.error(errMSg);
114                throw new SequenceAspectException(errMSg);
115            }
116            //序列+1
117            newSeq = sysSequence.getCurrentSeq() + 1;
118            resultCount = sysSequenceMapper.updateCurrentSeq( newSeq ,sysSequence.getSeqCode(), sysSequence.getCurrentSeq());
119             
120            forNum ++;
121        }
122        if(forNum == maxNum){
123            String errMSg = "序列更新失败,请检查数据库序列表配置:" + seqCode;
124            LOGGER.error(errMSg);
125            throw new SequenceAspectException(errMSg);
126        }
127        //拼接序列
128        int currentLength = String.valueOf(newSeq).length();//当前序列长度
129        int minLength = sysSequence.getMinLength();//最小长度
130        StringBuffer bf = new StringBuffer();
131        if(StringUtils.isNotBlank(sysSequence.getFixStart())){//固定头
132            bf.append(sysSequence.getFixStart());
133        }
134        int zeroLength = minLength - currentLength;
135        while(zeroLength > 0){//补0
136            bf.append("0");
137            zeroLength--;
138        }
139        bf.append(newSeq);
140        if(StringUtils.isNotBlank(sysSequence.getFixEnd())){//固定尾
141            bf.append(sysSequence.getFixEnd());
142        }
143        return bf.toString().trim();
144    }
145     
146    /**
147     * 获取序列(根据类名转为数据库名)
148     * @param dbEntity
149     * @return
150     */
151    public String getSequence(Class dbEntity) {
152        String classPath = dbEntity.getName();
153        String className = classPath.substring(classPath.lastIndexOf(".") + 1, classPath.length());
154        String seqCode = FormatUtils.classnameToDbname(className);//类名转数据库表名
155        return getSequence(seqCode);
156    }
157}
最近下载更多
educationAAA  LV11 2024年8月10日
hefeng  LV2 2024年5月27日
xuweiwowzy  LV5 2024年1月14日
xierhui  LV6 2022年9月20日
Hsy605  LV9 2022年6月22日
cxdxfx12  LV14 2022年6月21日
Jason137  LV8 2022年3月15日
Yee.  LV5 2022年1月22日
yxhxj2006  LV7 2021年9月8日
sunlea  LV20 2021年8月15日
最近浏览更多
xzhuike 2024年12月17日
暂无贡献等级
552124 2024年11月16日
暂无贡献等级
King_wsk  LV18 2024年11月2日
bluerstar  LV1 2024年10月23日
微信网友_7134912998903808  LV15 2024年8月29日
TY0165  LV20 2024年6月24日
hefeng  LV2 2024年5月27日
李俊雄  LV3 2024年5月8日
educationAAA  LV11 2024年4月26日
woldxy  LV12 2024年4月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友