zoukankan      html  css  js  c++  java
  • AOP使用举例

     1.背景

    不同的日期查询数据,需要到不同的库去查:

     

    本系统采用AOP的方法,自动切换数据源。

    当前库:今天的数据

    历史库:前三个月的数据

    归档库:三个月之前的数据

     

     2.代码示例

    本系统AOP代码:

    1)由ChangeDataSource.class进入AOP

    @CloudComponent
    @TargetDataSource(location = ChangeDataSource.class)
    public class IOptionPortfolioStrategyTentrustServiceImpl implements IOptionPortfolioStrategyTentrustService {
    
        @Autowired
        private IOptionPortfolioStrategyTentrustMapper iOptionPortfolioStrategyTentrustMapper;
    
    
        /**
         * 日期类型切分
         */
        private static final String DATETYPE_1 = "1" ;
        private static final String DATETYPE_3 = "3" ;
    
    
        /**
         * 当前与历史查询
         * @param req
         * @return
         */
        @Override
        public PubstockOptionPortfolioStrategyTentrustResponse getTentrust(PubstockOptionPortfolioStrategyTentrustRequest req) {
            ValidUtils.notEmpty(req.getLoginCompany(), "公司序号不能为空");
            ValidUtils.notEmpty(req.getBeginDate(), "起始日期不能为空");
            ValidUtils.notEmpty(req.getEndDate(), "结束日期不能为空");
            PubstockOptionPortfolioStrategyTentrustResponse resp = new PubstockOptionPortfolioStrategyTentrustResponse();
            if (DATETYPE_1.equals(req.getDateType())) {
                MysqlPageHelper.startPage(req.getStartRow(), req.getPageSize(),false);
                List<OpubstockPtionPortfolioStrategyTentrust> rows = iOptionPortfolioStrategyTentrustMapper.tentrustQuery(req);
                MysqlPageHelper.endPage();
                // 添加判空操作
                if (rows != null && rows.size() > 0) {
                    resp.setRows(rows);
                }
            } else {
                MysqlPageHelper.startPage(req.getStartRow(), req.getPageSize(),false);
                List<OpubstockPtionPortfolioStrategyTentrust> rows2 = iOptionPortfolioStrategyTentrustMapper.tentrustHisQuery(req);
                MysqlPageHelper.endPage();
                // 添加判空操作
                if (rows2 != null && rows2.size() > 0) {
                    resp.setRows(rows2);
                }
            }
            return resp;
        }
    }
    根据begin_date,end_date 切换日期 end_date/business_date < 归档日cxhis, locationKey = dbarch .   否则 locationKey = default 默认数据库
     1 public class ChangeDataSource implements DsLocationable{
     2 
     3 
     4     /**
     5      * Get locationkey string
     6      *
     7      * @param args
     8      * @return string
     9      */
    10     @Override
    11     public String getLocationkey(Object[] args) {
    12         // TODO Auto-generated method stub
    13         String locationKey = DataSourceConstants.DEFAULT;
    14         AresUtil aresUtil = SpringUtils.getBean(AresUtil.class);
    15         IFinancepubService financePubService = aresUtil.getFinancepubService();
    16         IAresService iAresService = aresUtil.getAresService();
    17         for (Object obj : args) {
    18             CxServiceAspect cxAspect = new CxServiceAspect();
    19             YhpubPretradedayGetRequest yhpubPretradedayGetRequest = new YhpubPretradedayGetRequest();
    20             yhpubPretradedayGetRequest.setMarketNo(5);
    21             YhpubPretradedayGetResponse yhpubPretradedayGetResponse = iAresService.pretradedayGet(yhpubPretradedayGetRequest);
    22             //获取上一个业务日期
    23             Integer prevBusinessDate = yhpubPretradedayGetResponse!=null?yhpubPretradedayGetResponse.getPrevBusinessDate():0;
    24             //查询日期小于等于cxhis 归档日,数据全部移动到了归档库
    25             BatclearPrebusindatecalcRequest req = new BatclearPrebusindatecalcRequest();
    26             req.setBusinessDate(prevBusinessDate);
    27             BatclearPrebusindatecalcResponse response = financePubService.prebusindatecalc(req);
    28             int cxhis = response.getPrevBusinessDate();
    29 //            int cxhis = this.cxhisDate(prevBusinessDate);
    30 
    31             if (obj instanceof BaseDTO) {
    32                 BaseDTO baseDto = (BaseDTO) obj;
    33                 baseDto.setCxhis(cxhis);
    34             }
    35             /****** START报表查询逻辑处理START *********************/
    36             if (obj instanceof OcxCommonquery) {
    37                 /** 1.数据权限控制 **/
    38                 OcxCommonquery ocxCommonquery = (OcxCommonquery) obj;
    39                 Integer companyId = ocxCommonquery.getCompanyId();
    40 
    41                 /****** 3 数据源切换 START *********************/
    42                 // 切换到归档数据查询
    43                 int endDate = ocxCommonquery.getEndDate() == null ? 0 : ocxCommonquery.getEndDate().intValue();
    44                 int beginDate = ocxCommonquery.getBeginDate() == null ? 0 : ocxCommonquery.getBeginDate().intValue();
    45 
    46                 if (endDate > 0 && endDate <= cxhis) {
    47                     ocxCommonquery.setDateType("3");
    48                     locationKey = DataSourceConstants.DBARCH;
    49                 }
    50                 if (beginDate == 0 && endDate == 0) { // 如果只传入一个日期businessDate
    51                     Object value = cxAspect.getFieldValueByName("businessDate", obj);
    52                     // 如果入参中没有这个参数或者这个参数没有值,就默认查询当前库(dbtrade)
    53                     if (null == value) {
    54                         ocxCommonquery.setDateType("1");
    55                         // dbAspect.dataSourceExchange("1");
    56                         locationKey = DataSourceConstants.DEFAULT;
    57                     } else {
    58                         int businessDate = Integer.parseInt(String.valueOf(value));
    59                         if (businessDate > 0 && businessDate <= cxhis) {
    60                             ocxCommonquery.setDateType("3");
    61                             // dbAspect.dataSourceExchange("3");
    62                             locationKey = DataSourceConstants.DBARCH;
    63                         }
    64                     }
    65                 }
    66             }
    67 
    68         }
    69         return locationKey;
    70     }
    71 }

     

  • 相关阅读:
    完全卸载删除nginx
    多线程如何确定线程数
    【精】Linux磁盘I/O性能监控之iostat详解
    Linux信号处理和守护进程
    Linux进程间通信——使用信号
    kill 命令详解 系统信号
    Valgrind使用指南和错误分析
    Valgrind memcheck 8种错误实例
    Linux环境崩溃生成core文件以及调试
    linux Valgrind使用说明-内存泄漏
  • 原文地址:https://www.cnblogs.com/aaaazzzz/p/14003532.html
Copyright © 2011-2022 走看看