java实现通用查询_通用业务引用查询服务实现

publicclassBizReferenceServiceimplementsIBizReferenceService {privatestaticMap>ruleMaps;privatestaticfinalString PATTERN="#[\\w]+#";privatestaticfinalString CFG_FILE="bizReferenceRule.xml";

... .../*** 查询指定业务数据是否被其他业务表关联依赖.

*@parambizName 关联业务名称

*@parambizId 关联业务ID.

*@paramextParam 扩展条件

*@returntrue 被关联/false 未被关联.*/publicbooleanisBizReference(String bizName,String bizId,MapextParam)throwsServiceException {

Assert.notNull(bizName,"业务名称不能为空,bizName is NULL。");

Assert.notNull(bizId,"记录ID不能为空,bizId is NULL。");try{//逐个检查依赖项是否有数据关联.Listrules=getBizRelationRule(bizName);for(BizReferenceRule rule : rules){

StringBuilder sqlBuilder=newStringBuilder();

sqlBuilder.append("select count(*) from").append(rule.getRelTable()).append("where")

.append(rule.getRelField()).append("='").append(bizId).append("'");

String extConditon=rule.getExtCondition();if(StringUtil.isNotBlank(extConditon)){

initTenantParam(extParam);

sqlBuilder.append("and").append(getExtParamSql(extConditon,extParam));

}

logger.debug(sqlBuilder);intnCount=bizReferenceDao.getBizRelationCount(sqlBuilder.toString());if(nCount!=0)returntrue;

}returnfalse;

}catch(Exception ex){

logger.error("调用业务关联服务错误。"+bizName+",bizId:"+bizId+",extParam"+LogUtil.parserBean(extParam),ex);thrownewServiceException("调用业务关联服务错误。");

}

}/*** 组装扩展查询条件的sql

*@paramcondition

*@paramextParam

*@return*@throwsException*/privateString getExtParamSql(String condition,MapextParam)throwsException {

ListparamList=parseDyncParam(condition);for(String param : paramList){

String simpleParam=simpleName(param);if(!extParam.containsKey(simpleParam)){thrownewServiceException("动态参数值未设置! param:"+param+",extParam:"+LogUtil.parserBean(extParam));

}

condition=condition.replaceAll(param,"'"+String.valueOf(extParam.get(simpleParam))+"'");

}returncondition;

}/*** 解析扩展查询条件中的动态参数名.

*@paramcondition

*@return*@throwsException*/privateListparseDyncParam(String condition)throwsException {

PatternCompiler compiler=newPerl5Compiler();

PatternMatcher matcher=newPerl5Matcher();

MatchResult result=null;

PatternMatcherInput input=null;

ListparamList=newArrayList();

input=newPatternMatcherInput(condition);

Pattern pattern=compiler.compile(PATTERN,Perl5Compiler.CASE_INSENSITIVE_MASK);while(matcher.contains(input, pattern)){

result=matcher.getMatch();

input.setBeginOffset(result.length());

paramList.add(result.group(0));

}returnparamList;

}/*** 获取业务关联查询规则.*/privateListgetBizRelationRule(String bizName){

Assert.notNull(bizName,"业务名称不能为空,bizName is NULL。");//配置定义未加载到内存时,读取配置文件if(ruleMaps==null){

parseRuleConfig();if(ruleMaps==null)returnnull;

}returnruleMaps.get(bizName);

}/*** 读取业务关联规则配置文件*/@SuppressWarnings("unchecked")privatesynchronizedvoidparseRuleConfig(){if(ruleMaps!=null){return;

        }//解析业务引用定义文件.9b8a8a44dd1c74ae49c20a7cd451974e.png 

9b8a8a44dd1c74ae49c20a7cd451974e.png

}/*** 读取Xml文档

*@return*/privateDocument getXmlDocument(){

InputStream is=null;try{

ClassLoader loader=Thread.currentThread().getContextClassLoader();

is=loader.getResourceAsStream(CFG_FILE);

SAXBuilder sb=newSAXBuilder();returnsb.build(newBufferedInputStream(is));

}catch(Exception ex) {

logger.error("读取配置文件错误. file:"+CFG_FILE, ex);returnnull;

}finally{try{if(is!=null){

is.close();

is=null;

}

}catch(Exception ex) {

logger.error(ex);

}

}

}

9b8a8a44dd1c74ae49c20a7cd451974e.png 

9b8a8a44dd1c74ae49c20a7cd451974e.png

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值