Commit 7371e304 by s_guodong

连表查询

parent ee9542a7
package com.ceb.gjjs.mda.manager.module; package com.ceb.gjjs.mda.manager.module;
import java.util.*; import com.brilliance.mda.runtime.annotation.BDGetter;
import java.math.BigDecimal; import com.brilliance.mda.runtime.annotation.Check;
import com.brilliance.mda.runtime.annotation.Init;
import com.brilliance.mda.runtime.annotation.Rule;
import com.brilliance.mda.runtime.mda.*; import com.brilliance.mda.runtime.mda.*;
import com.brilliance.mda.runtime.mda.impl.*; import com.brilliance.mda.runtime.mda.driver.MdaDriver;
import com.brilliance.mda.runtime.annotation.*; import com.brilliance.mda.runtime.mda.driver.MdaEnv;
import com.brilliance.mda.runtime.mda.driver.*; import com.brilliance.mda.runtime.mda.impl.AbstractModule;
import static com.brilliance.mda.runtime.mda.Constants.*; import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.*; import com.brilliance.mda.runtime.mda.impl.StreamImpl;
import com.fasterxml.jackson.annotation.*; import com.brilliance.mda.runtime.mda.util.MdaUtils;
import java.util.regex.*; import com.ceb.gjjs.mda.bo.Chn;
import lombok.Getter;
import lombok.Setter;
import com.ceb.gjjs.mda.bo.Wanrec; import com.ceb.gjjs.mda.bo.Wanrec;
import com.ceb.gjjs.mda.global.Platform; import com.ceb.gjjs.mda.global.Platform;
import com.ceb.gjjs.mda.bo.Chn; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.brilliance.mda.runtime.mda.Constants.*;
/** /**
* *
*/ */
...@@ -1151,7 +1160,46 @@ public class Rcvp extends AbstractModule{ ...@@ -1151,7 +1160,46 @@ public class Rcvp extends AbstractModule{
if(!"blksel".equals(ctx.getTransName())) if(!"blksel".equals(ctx.getTransName()))
{ {
log.debug("{}","将查询结果添加至rcvlst中"); log.debug("{}","将查询结果添加至rcvlst中");
this.getMsggrp().getRcvlst().add(this.getMsggrp().getMpss().getElement(i).getInr(),this.getMsggrp().getMpss().getElement(i).getDir(),this.getMsggrp().getMpss().getElement(i).getSrcchncod(),this.getMsggrp().getMpss().getElement(i).getChncod(),this.getMsggrp().getMpss().getElement(i).getNxtchncod(),this.getMsggrp().getMpss().getElement(i).getTgtchncod(),this.getMsggrp().getMpss().getElement(i).getSmhinr(),this.getMsggrp().getMpss().getElement(i).getSta(),this.getMsggrp().getMpss().getElement(i).getRspsta(),this.getMsggrp().getMpss().getElement(i).getRcvdattim(),this.getMsggrp().getMpss().getElement(i).getLstdattim(),this.getMsggrp().getMpss().getElement(i).getSttdattim(),this.getMsggrp().getMpss().getElement(i).getTgtdattime(),this.getMsggrp().getMpss().getElement(i).getSnddattim(),this.getMsggrp().getMpss().getElement(i).getRspdattim(),this.getMsggrp().getMpis().getElement(i).getOwnref(),this.getMsggrp().getMpis().getElement(i).getOthref(),this.getMsggrp().getMpis().getElement(i).getSndbic(),this.getMsggrp().getMpis().getElement(i).getRcvbic(),this.getMsggrp().getMpis().getElement(i).getAct(),this.getMsggrp().getMpis().getElement(i).getDtlchg(),this.getMsggrp().getMpis().getElement(i).getCur(),this.getMsggrp().getMpis().getElement(i).getAmt(),this.getMsggrp().getMpis().getElement(i).getValdat(),this.getMsggrp().getMpis().getElement(i).getMsgtyp(),this.getMsggrp().getMpis().getElement(i).getSubtyp(),this.getMsggrp().getMpis().getElement(i).getActbic(),this.getMsggrp().getMpis().getElement(i).getT53bic(),this.getMsggrp().getMpis().getElement(i).getGpi(),this.getMsggrp().getMpis().getElement(i).getUetr(),this.getMsggrp().getMpss().getElement(i).getLstdattim(),this.getMsggrp().getMpis().getElement(i).getLcnb(), this.getMsggrp().getMpss().getElement(i).getSweflg(),this.getMsggrp().getMpss().getElement(i).getBlktyp(),this.getMsggrp().getMpss().getElement(i).getXhxflg(),this.getMsggrp().getMpis().getElement(i).getLoginoprtp(),this.getMsggrp().getMpis().getElement(i).getSid(),this.getMsggrp().getMpis().getElement(i).getMtp(),this.getMsggrp().getMpis().getElement(i).getTxtpcd(),this.getMsggrp().getMpis().getElement(i).getCtgpcd(),this.getMsggrp().getMpis().getElement(i).getMid(),this.getMsggrp().getMpis().getElement(i).getCredat(),this.getMsggrp().getMpis().getElement(i).getInsgdp(),this.getMsggrp().getMpis().getElement(i).getInsdpl(),this.getMsggrp().getMpis().getElement(i).getInsgty(),this.getMsggrp().getMpis().getElement(i).getInsgpl(),this.getMsggrp().getMpis().getElement(i).getInsddp(),this.getMsggrp().getMpis().getElement(i).getInsddl(),this.getMsggrp().getMpis().getElement(i).getInsdty(),this.getMsggrp().getMpis().getElement(i).getInspdl(),this.getMsggrp().getMpis().getElement(i).getSyscod(),this.getMsggrp().getMpis().getElement(i).getRmk(),this.getMsggrp().getMpis().getElement(i).getOrgmid(),this.getMsggrp().getMpis().getElement(i).getOrginspty(),this.getMsggrp().getMpis().getElement(i).getOrginapty(),this.getMsggrp().getMpis().getElement(i).getOrgmsgtyp(),this.getMsggrp().getMpis().getElement(i).getOrgsnddat(),this.getMsggrp().getMpis().getElement(i).getStscod(),this.getMsggrp().getMpis().getElement(i).getPrcmsgdig(),this.getMsggrp().getMpis().getElement(i).getRspmsg(),this.getMsggrp().getMpis().getElement(i).getPrcsts(),this.getMsggrp().getMpis().getElement(i).getProccd(),this.getMsggrp().getMpis().getElement(i).getRjcinf(),this.getMsggrp().getMpis().getElement(i).getSstdat(),this.getMsggrp().getMpis().getElement(i).getRcvdat(),this.getMsggrp().getMpis().getElement(i).getTrndat(),this.getMsggrp().getMpis().getElement(i).getOrpmid(),this.getMsggrp().getMpis().getElement(i).getOrpinspty(),this.getMsggrp().getMpis().getElement(i).getOrpinapty(),this.getMsggrp().getMpis().getElement(i).getOrityp(),this.getMsggrp().getMpis().getElement(i).getOridat(),this.getMsggrp().getMpis().getElement(i).getOrists(),this.getMsggrp().getMpis().getElement(i).getOpinfo(),this.getMsggrp().getMpis().getElement(i).getOriaccdat(),this.getMsggrp().getMpis().getElement(i).getTrasts(),this.getMsggrp().getMpis().getElement(i).getBllpaysts(),this.getMsggrp().getMpis().getElement(i).getRemark());
// this.getMsggrp().getRcvlst().add( this.getMsggrp().getMpss().getElement(i).getDatafields().toArray(new IDatafield[0]));
this.getMsggrp().getRcvlst().add(MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"inr"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"dir"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"srcchncod"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"chncod"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"nxtchncod"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"tgtchncod"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"smhinr"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"sta"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"rspsta"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"rcvdattim"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"lstdattim"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"tgtdattime"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"sttdattim"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"snddattim"),
MdaDriver.getDatafield(this.getMsggrp().getMpss().getElement(i),"rspdattim"),
MdaDriver.getDatafield(this.getMsggrp().getMpis().getElement(i),"ownref"),
MdaDriver.getDatafield(this.getMsggrp().getMpis().getElement(i),"othref")
);
//$msggrp\\rcvlst.add($msggrp\\mpss[i]\\inr,$msggrp\\mpss[i]\\dir,$msggrp\\mpss[i]\\srcchncod
// ,$msggrp\\mpss[i]\\chncod,$msggrp\\mpss[i]\\nxtchncod,$msggrp\\mpss[i]\\tgtchncod
// ,$msggrp\\mpss[i]\\smhinr,$msggrp\\mpss[i]\\sta,$msggrp\\mpss[i]\\rspsta
// ,$msggrp\\mpss[i]\\rcvdattim,$msggrp\\mpss[i]\\lstdattim,$msggrp\\mpss[i]\\sttdattim
// ,$msggrp\\mpss[i]\\tgtdattime,$msggrp\\mpss[i]\\snddattim,$msggrp\\mpss[i]\\rspdattim
// ,$msggrp\\mpis[i]\\ownref,$msggrp\\mpis[i]\\othref,$msggrp\\mpis[i]\\sndbic,$msggrp\\mpis[i]\\rcvbic
// ,$msggrp\\mpis[i]\\act,$msggrp\\mpis[i]\\dtlchg,$msggrp\\mpis[i]\\cur,$msggrp\\mpis[i]\\amt,$msggrp\\mpis[i]\\valdat
// ,$msggrp\\mpis[i]\\msgtyp,$msggrp\\mpis[i]\\subtyp,$msggrp\\mpis[i]\\actbic,$msggrp\\mpis[i]\\t53bic,$msggrp\\mpis[i]\\gpi
// ,$msggrp\\mpis[i]\\uetr,$msggrp\\mpss[i]\\lstdattim,$msggrp\\mpis[i]\\lcnb, $msggrp\\mpss[i]\\sweflg,$msggrp\\mpss[i]\\blktyp,$msggrp\\mpss[i]\\xhxflg
// ,$msggrp\\mpis[i]\\loginoprtp,$msggrp\\mpis[i]\\sid,$msggrp\\mpis[i]\\mtp,$msggrp\\mpis[i]\\txtpcd,$msggrp\\mpis[i]\\ctgpcd
// ,$msggrp\\mpis[i]\\mid,$msggrp\\mpis[i]\\credat,$msggrp\\mpis[i]\\insgdp,$msggrp\\mpis[i]\\insdpl,$msggrp\\mpis[i]\\insgty
// ,$msggrp\\mpis[i]\\insgpl,$msggrp\\mpis[i]\\insddp,$msggrp\\mpis[i]\\insddl,$msggrp\\mpis[i]\\insdty,$msggrp\\mpis[i]\\inspdl
// ,$msggrp\\mpis[i]\\syscod,$msggrp\\mpis[i]\\rmk,$msggrp\\mpis[i]\\orgmid,$msggrp\\mpis[i]\\orginspty,$msggrp\\mpis[i]\\orginapty
// ,$msggrp\\mpis[i]\\orgmsgtyp,$msggrp\\mpis[i]\\orgsnddat,$msggrp\\mpis[i]\\stscod,$msggrp\\mpis[i]\\prcmsgdig,$msggrp\\mpis[i]\\rspmsg
// ,$msggrp\\mpis[i]\\prcsts,$msggrp\\mpis[i]\\proccd,$msggrp\\mpis[i]\\rjcinf,$msggrp\\mpis[i]\\sstdat,$msggrp\\mpis[i]\\rcvdat
// ,$msggrp\\mpis[i]\\trndat,$msggrp\\mpis[i]\\orpmid,$msggrp\\mpis[i]\\orpinspty,$msggrp\\mpis[i]\\orpinapty,$msggrp\\mpis[i]\\orityp
// ,$msggrp\\mpis[i]\\oridat,$msggrp\\mpis[i]\\orists,$msggrp\\mpis[i]\\opinfo,$msggrp\\mpis[i]\\oriaccdat,$msggrp\\mpis[i]\\trasts
// ,$msggrp\\mpis[i]\\bllpaysts,$msggrp\\mpis[i]\\remark);
if("mx".equals(this.getMsgtyp())) if("mx".equals(this.getMsgtyp()))
{ {
this.getMsggrp().getRcvlst().getElement(i).setMsgtyp( this.getMsgtyp()); this.getMsggrp().getRcvlst().getElement(i).setMsgtyp( this.getMsgtyp());
......
...@@ -217,8 +217,17 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -217,8 +217,17 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
} }
@Override @Override
public void dbReadset(IModuleList[] lists, int maxSize, String whereClause, Object[] datas) { public <T extends IModule> int dbReadset(IModuleList<T>[] lists, int maxSize, String whereClause, Object[] datas) {
List<Class<? extends IModule>> clazzList = new ArrayList<>();
for (IModuleList iModule : lists) {
clazzList.add(iModule.getDataClass());
}
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(SELECT, clazzList, whereClause, datas);
for (IModuleList iModule : lists) {
List<T> result = this.dyncRead(iModule.getDataClass(), adapter);
iModule.addAll(result);
}
return lists[0].size();
} }
@Override @Override
...@@ -296,6 +305,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -296,6 +305,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
return String.format(sqlIdTemplate, namespacePackage, clazz.getSimpleName(), sid); return String.format(sqlIdTemplate, namespacePackage, clazz.getSimpleName(), sid);
} }
private <T> T dyncReadOne(Class<? extends IModule> clazz, MybatisArgumentAdapter adapter) { private <T> T dyncReadOne(Class<? extends IModule> clazz, MybatisArgumentAdapter adapter) {
String sqlId = getSqlId(clazz, SQLID_DYNCREAD); String sqlId = getSqlId(clazz, SQLID_DYNCREAD);
...@@ -316,6 +326,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -316,6 +326,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
return new ArrayList<>(list); return new ArrayList<>(list);
} }
private <T> List<T> dyncRead(Class<T> clazz, Map params) { private <T> List<T> dyncRead(Class<T> clazz, Map params) {
String sqlId = getSqlId(clazz, SQLID_DYNCREAD); String sqlId = getSqlId(clazz, SQLID_DYNCREAD);
......
...@@ -6,18 +6,18 @@ import com.brilliance.mda.support.mybatis.entity.Column; ...@@ -6,18 +6,18 @@ import com.brilliance.mda.support.mybatis.entity.Column;
import com.brilliance.mda.support.mybatis.entity.Table; import com.brilliance.mda.support.mybatis.entity.Table;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.asm.Type;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static com.brilliance.mda.runtime.mda.OpType.AND; import static com.brilliance.mda.runtime.mda.OpType.*;
import static com.brilliance.mda.runtime.mda.OpType.ASC;
import static com.brilliance.mda.runtime.mda.OpType.DESC;
/** /**
* @author hulei * @author hulei
...@@ -25,105 +25,116 @@ import static com.brilliance.mda.runtime.mda.OpType.DESC; ...@@ -25,105 +25,116 @@ import static com.brilliance.mda.runtime.mda.OpType.DESC;
*/ */
public class MybatisArgumentAdapter { public class MybatisArgumentAdapter {
private static Map<String,String> annotCache = new ConcurrentHashMap<>(); private static Map<String, String> annotCache = new ConcurrentHashMap<>();
public static final String HOLDER_SQL = "sql"; public static final String HOLDER_SQL = "sql";
public static final String HOLDER_CONDS = "conditions"; public static final String HOLDER_CONDS = "conditions";
public static enum AdaType{ public static enum AdaType {
SELECT("SELECT * FROM %s WHERE %s"), SELECT("SELECT * FROM %s WHERE %s"),
DELETE("DELETE FROM %s WHERE %s"); DELETE("DELETE FROM %s WHERE %s");
private String value; private String value;
private AdaType(String val){
private AdaType(String val) {
this.value = val; this.value = val;
} }
public String getValue(){
public String getValue() {
return value; return value;
} }
} }
private Logger log = LoggerFactory.getLogger(this.getClass()); private Logger log = LoggerFactory.getLogger(this.getClass());
private Class<? extends IModule> clazz ; private Class<? extends IModule> clazz;
private List<Class<? extends IModule>> clazzList;
private Argument[] args; private Argument[] args;
private String sqlTemplate; private String sqlTemplate;
private Map<String,Object> sqlParams; private Map<String, Object> sqlParams;
private int idx = 0; private int idx = 0;
private String whereSql; private String whereSql;
private String pattern; private String pattern;
private Object[] params; private Object[] params;
private String tableName;
public MybatisArgumentAdapter(AdaType type, Class<? extends IModule> clazz, Argument... args){ public MybatisArgumentAdapter(AdaType type, Class<? extends IModule> clazz, Argument... args) {
this.clazz = clazz; this.clazz = clazz;
this.pattern = type.getValue(); this.pattern = type.getValue();
this.args = args; this.args = args;
this.tableName = convertTableName();
this.init(); this.init();
} }
public MybatisArgumentAdapter(AdaType type,Class<? extends IModule> clazz, String whereSql){ public MybatisArgumentAdapter(AdaType type, Class<? extends IModule> clazz, String whereSql) {
this(type,clazz,whereSql,null); this(type, clazz, whereSql, null);
} }
public MybatisArgumentAdapter(AdaType type,Class<? extends IModule> clazz, String whereSql,Object[] params){ public MybatisArgumentAdapter(AdaType type, Class<? extends IModule> clazz, String whereSql, Object[] params) {
this.clazz = clazz; this.clazz = clazz;
this.pattern = type.getValue(); this.pattern = type.getValue();
this.whereSql = whereSql; this.whereSql = whereSql;
this.params = params; this.params = params;
this.tableName = convertTableName();
this.init();
}
public MybatisArgumentAdapter(AdaType type, List<Class<? extends IModule>> clazzList, String whereSql, Object[] params) {
this.clazzList = clazzList;
this.pattern = type.getValue();
this.whereSql = whereSql;
this.params = params;
this.tableName = convertTableNames();
this.init(); this.init();
} }
public void init(){ public void init() {
sqlParams = new HashMap<>(); sqlParams = new HashMap<>();
String tableName = convertTableName();
String conditions = ""; String conditions = "";
if(whereSql != null && !whereSql.trim().equals("")){ if (whereSql != null && !whereSql.trim().equals("")) {
if(whereSql.toUpperCase().indexOf("WHERE") > -1){ if (whereSql.toUpperCase().indexOf("WHERE") > -1) {
pattern = pattern.replace("WHERE",""); pattern = pattern.replace("WHERE", "");
} }
if(params!= null && params.length>0){ if (params != null && params.length > 0) {
conditions = convertPlaceHolder(whereSql,params,sqlParams); conditions = convertPlaceHolder(whereSql, params, sqlParams);
}else { } else {
conditions = whereSql; conditions = whereSql;
} }
}else{ } else {
if(this.args != null && this.args.length >0){ if (this.args != null && this.args.length > 0) {
conditions = parserArgument(this.args); conditions = parserArgument(this.args);
}else{ } else {
conditions = " 1=1 "; conditions = " 1=1 ";
} }
} }
this.sqlTemplate = String.format(pattern,tableName,conditions); this.sqlTemplate = String.format(pattern, tableName, conditions);
this.sqlParams.put(HOLDER_SQL,this.sqlTemplate); this.sqlParams.put(HOLDER_SQL, this.sqlTemplate);
this.sqlParams.put(HOLDER_CONDS,conditions); this.sqlParams.put(HOLDER_CONDS, conditions);
} }
public String getSqlTemplate(){ public String getSqlTemplate() {
return sqlTemplate; return sqlTemplate;
} }
public Map<String,Object> getSqlParams(){ public Map<String, Object> getSqlParams() {
return sqlParams; return sqlParams;
} }
/** /**
* 获取直接WHERE条件进行参数替换 * 获取直接WHERE条件进行参数替换
*/ */
protected final String parserArgument(String whereClause, Object[] args) protected final String parserArgument(String whereClause, Object[] args) {
{
StringBuffer sb = new StringBuffer(" "); StringBuffer sb = new StringBuffer(" ");
Matcher m = Pattern.compile("\\?").matcher(whereClause); Matcher m = Pattern.compile("\\?").matcher(whereClause);
int offset = 0, i = 0; int offset = 0, i = 0;
while (m.find()) while (m.find()) {
{
sb.append(whereClause.substring(offset, m.start())); sb.append(whereClause.substring(offset, m.start()));
sb.append("#{args[" + i + "]}"); sb.append("#{args[" + i + "]}");
i++; i++;
...@@ -138,22 +149,19 @@ public class MybatisArgumentAdapter { ...@@ -138,22 +149,19 @@ public class MybatisArgumentAdapter {
/** /**
* 获取生成对应的SQL条件(这里完成最终的组装) * 获取生成对应的SQL条件(这里完成最终的组装)
*/ */
protected final String parserArgument(Argument... arguments) protected final String parserArgument(Argument... arguments) {
{
List<String> orders = new ArrayList(); List<String> orders = new ArrayList();
List<String> criterions = new ArrayList(); List<String> criterions = new ArrayList();
for (int i = 0; i < arguments.length; i++) for (int i = 0; i < arguments.length; i++)
process(arguments[i], i, orders, criterions); process(arguments[i], i, orders, criterions);
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
String conn = ""; String conn = "";
for (String criterion : criterions) for (String criterion : criterions) {
{
sb.append(conn).append(criterion); sb.append(conn).append(criterion);
conn = " AND "; conn = " AND ";
} }
conn = " ORDER BY "; conn = " ORDER BY ";
for (String criterion : orders) for (String criterion : orders) {
{
sb.append(conn).append(criterion); sb.append(conn).append(criterion);
conn = ","; conn = ",";
} }
...@@ -165,75 +173,71 @@ public class MybatisArgumentAdapter { ...@@ -165,75 +173,71 @@ public class MybatisArgumentAdapter {
/** /**
* 按Order和Criterion来处理Argument * 按Order和Criterion来处理Argument
*/ */
private final void process(Argument argument, int index, List orders, List criterions) private final void process(Argument argument, int index, List orders, List criterions) {
{
if (argument.opType == ASC) if (argument.opType == ASC)
orders.add(argument.fieldName); orders.add(argument.fieldName);
else if (argument.opType == DESC) else if (argument.opType == DESC)
orders.add(argument.fieldName + " DESC"); orders.add(argument.fieldName + " DESC");
else else {
{ String criterion = processCriterion(argument, index);
String criterion = processCriterion(argument,index);
if (criterion != null) if (criterion != null)
criterions.add(criterion); criterions.add(criterion);
} }
} }
private final String processCriterion(Argument argument,int idx) private final String processCriterion(Argument argument, int idx) {
{
if (argument == null || argument.opType == null) if (argument == null || argument.opType == null)
return null; return null;
StringBuffer sb; StringBuffer sb;
String conn; String conn;
String holderName ; String holderName;
switch (argument.opType) switch (argument.opType) {
{
case ISNULL: case ISNULL:
return convertColumn(argument.fieldName) + " IS NULL"; return convertColumn(argument.fieldName) + " IS NULL";
case ISNOTNULL: case ISNOTNULL:
return convertColumn(argument.fieldName) + " IS NOT NULL"; return convertColumn(argument.fieldName) + " IS NOT NULL";
case NE: case NE:
holderName = argument.fieldName+"_"+this.idx++; holderName = argument.fieldName + "_" + this.idx++;
sqlParams.put(holderName,argument.value); sqlParams.put(holderName, argument.value);
return convertColumn(argument.fieldName) + "!=" + placeholder(argument.value, holderName); return convertColumn(argument.fieldName) + "!=" + placeholder(argument.value, holderName);
case LE: case LE:
holderName = argument.fieldName+"_"+this.idx++; holderName = argument.fieldName + "_" + this.idx++;
sqlParams.put(holderName,argument.value); sqlParams.put(holderName, argument.value);
return convertColumn(argument.fieldName) + "<=" + placeholder(argument.value, holderName); return convertColumn(argument.fieldName) + "<=" + placeholder(argument.value, holderName);
case GE: case GE:
holderName = argument.fieldName+"_"+this.idx++; holderName = argument.fieldName + "_" + this.idx++;
sqlParams.put(holderName,argument.value); sqlParams.put(holderName, argument.value);
return convertColumn(argument.fieldName) + ">=" + placeholder(argument.value, holderName); return convertColumn(argument.fieldName) + ">=" + placeholder(argument.value, holderName);
case LT: case LT:
holderName = argument.fieldName+"_"+this.idx++; holderName = argument.fieldName + "_" + this.idx++;
sqlParams.put(holderName,argument.value); sqlParams.put(holderName, argument.value);
return convertColumn(argument.fieldName) + "<" + placeholder(argument.value, holderName); return convertColumn(argument.fieldName) + "<" + placeholder(argument.value, holderName);
case GT: case GT:
holderName = argument.fieldName+"_"+this.idx++; holderName = argument.fieldName + "_" + this.idx++;
sqlParams.put(holderName,argument.value); sqlParams.put(holderName, argument.value);
return convertColumn(argument.fieldName) + ">" + placeholder(argument.value, holderName); return convertColumn(argument.fieldName) + ">" + placeholder(argument.value, holderName);
case NOTLIKE: case NOTLIKE:
holderName = argument.fieldName+"_"+this.idx++; holderName = argument.fieldName + "_" + this.idx++;
sqlParams.put(holderName,argument.value); sqlParams.put(holderName, argument.value);
return convertColumn(argument.fieldName) + " NOT LIKE " + placeholder(argument.value, holderName); return convertColumn(argument.fieldName) + " NOT LIKE " + placeholder(argument.value, holderName);
case LIKE: case LIKE:
holderName = argument.fieldName+"_"+this.idx++; holderName = argument.fieldName + "_" + this.idx++;
sqlParams.put(holderName,argument.value); sqlParams.put(holderName, argument.value);
return convertColumn(argument.fieldName) + " LIKE " + placeholder(argument.value, holderName); return convertColumn(argument.fieldName) + " LIKE " + placeholder(argument.value, holderName);
case BETWEEN: case BETWEEN:
holderName = argument.fieldName+"_"+this.idx++; holderName = argument.fieldName + "_" + this.idx++;
Object[] ary = (Object[]) argument.value; Object[] ary = (Object[]) argument.value;
sqlParams.put(holderName+"_0",ary[0]); sqlParams.put(holderName + "_0", ary[0]);
sqlParams.put(holderName+"_1",ary[1]); sqlParams.put(holderName + "_1", ary[1]);
return convertColumn(argument.fieldName) + " BETWEEN " + placeholder(ary[0], holderName+"_0") + " AND " + placeholder(ary[1], holderName+"_1"); return convertColumn(argument.fieldName) + " BETWEEN " + placeholder(ary[0], holderName + "_0") + " AND " + placeholder(ary[1], holderName + "_1");
case IN: case IN:
holderName = argument.fieldName+"_"+this.idx++; holderName = argument.fieldName + "_" + this.idx++;
sb = new StringBuffer(convertColumn(argument.fieldName)).append(" IN ("); sb = new StringBuffer(convertColumn(argument.fieldName)).append(" IN (");
conn = ""; conn = "";
Object[] values = (Object[]) argument.value; Object[] values = (Object[]) argument.value;
for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {
sqlParams.put(holderName+"_"+i,values[i]); sqlParams.put(holderName + "_" + i, values[i]);
sb.append(conn).append(placeholder(values[i], holderName+"_"+i)); sb.append(conn).append(placeholder(values[i], holderName + "_" + i));
conn = ","; conn = ",";
} }
return sb.append(")").toString(); return sb.append(")").toString();
...@@ -242,8 +246,7 @@ public class MybatisArgumentAdapter { ...@@ -242,8 +246,7 @@ public class MybatisArgumentAdapter {
sb = new StringBuffer("("); sb = new StringBuffer("(");
conn = ""; conn = "";
Argument[] arguments = (Argument[]) argument.value; Argument[] arguments = (Argument[]) argument.value;
for (int i = 0; i < arguments.length; i++) for (int i = 0; i < arguments.length; i++) {
{
sb.append(conn).append(processCriterion(arguments[i], i)); sb.append(conn).append(processCriterion(arguments[i], i));
conn = argument.opType == AND ? " AND " : " OR "; conn = argument.opType == AND ? " AND " : " OR ";
} }
...@@ -251,15 +254,14 @@ public class MybatisArgumentAdapter { ...@@ -251,15 +254,14 @@ public class MybatisArgumentAdapter {
case NOT: case NOT:
return "!(" + processCriterion((Argument) argument.value, idx) + ")"; return "!(" + processCriterion((Argument) argument.value, idx) + ")";
default: default:
holderName = argument.fieldName+"_"+this.idx++; holderName = argument.fieldName + "_" + this.idx++;
sqlParams.put(holderName,argument.value); sqlParams.put(holderName, argument.value);
return convertColumn(argument.fieldName) + "=" + placeholder(argument.value, holderName); return convertColumn(argument.fieldName) + "=" + placeholder(argument.value, holderName);
} }
} }
private final String placeholder(Object value, String name) private final String placeholder(Object value, String name) {
{ return String.format("#{%s}", name);
return String.format("#{%s}",name);
} }
...@@ -275,28 +277,29 @@ public class MybatisArgumentAdapter { ...@@ -275,28 +277,29 @@ public class MybatisArgumentAdapter {
/** /**
* 将标准SQL占位符替换为mybatis格式的占位符,并将参数数组转换为HashMap * 将标准SQL占位符替换为mybatis格式的占位符,并将参数数组转换为HashMap
*
* @param sql * @param sql
* @param objects * @param objects
* @param map * @param map
* @return * @return
*/ */
public static String convertPlaceHolder(String sql,Object[] objects,Map<String,Object> map){ public static String convertPlaceHolder(String sql, Object[] objects, Map<String, Object> map) {
Pattern p = Pattern.compile("[?]"); Pattern p = Pattern.compile("[?]");
Matcher m = p.matcher(sql); Matcher m = p.matcher(sql);
StringBuffer buf = new StringBuffer(); StringBuffer buf = new StringBuffer();
for (int i=0; m.find() && i<objects.length; i++){ for (int i = 0; m.find() && i < objects.length; i++) {
String holder = "var_"+i; String holder = "var_" + i;
m.appendReplacement(buf,String.format("#{%s}",holder)); m.appendReplacement(buf, String.format("#{%s}", holder));
map.put(holder,objects[i]); map.put(holder, objects[i]);
} }
m.appendTail(buf); m.appendTail(buf);
return buf.toString(); return buf.toString();
} }
public String convertTableName(){ public String convertTableName() {
if(annotCache.containsKey(clazz.getName())){ if (annotCache.containsKey(clazz.getName())) {
return annotCache.get(clazz.getName()); return annotCache.get(clazz.getName());
} }
String value = clazz.getSimpleName(); String value = clazz.getSimpleName();
...@@ -310,24 +313,53 @@ public class MybatisArgumentAdapter { ...@@ -310,24 +313,53 @@ public class MybatisArgumentAdapter {
return value; return value;
} }
public String convertColumn(String fieldName){
String key = String.format("%s.%s",clazz.getName(),fieldName); private String convertTableNames() {
if(annotCache.containsKey(key)){ StringBuffer sb = new StringBuffer("");
for (Class c : clazzList) {
sb.append(c.getName());
}
if (annotCache.containsKey(sb.toString())) {
return annotCache.get(sb.toString());
}
List<String> tb = new ArrayList<>();
for (int i = 0; i < clazzList.size(); i++) {
int idx = i + 1;
Class<? extends IModule> c = clazzList.get(i);
String value = c.getSimpleName();
if (c.isAnnotationPresent(Table.class)) {
Table t = c.getAnnotation(Table.class);
if (t.value() != null) {
value = t.value();
}
}
tb.add(value + " m" + idx);
}
String value = String.join(",", tb);
annotCache.put(sb.toString(), value);
return value;
}
public String convertColumn(String fieldName) {
String key = String.format("%s.%s", clazz.getName(), fieldName);
if (annotCache.containsKey(key)) {
return annotCache.get(key); return annotCache.get(key);
} }
try { try {
Field field = clazz.getDeclaredField(fieldName); Field field = clazz.getDeclaredField(fieldName);
String value = fieldName; String value = fieldName;
if(field.isAnnotationPresent(Column.class)){ if (field.isAnnotationPresent(Column.class)) {
Column col = field.getAnnotation(Column.class); Column col = field.getAnnotation(Column.class);
if(!StringUtils.isEmpty(col.value())){ if (!StringUtils.isEmpty(col.value())) {
value = col.value(); value = col.value();
} }
} }
annotCache.put(key,value); annotCache.put(key, value);
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {
annotCache.put(key,fieldName); annotCache.put(key, fieldName);
return fieldName; return fieldName;
} }
return annotCache.get(key); return annotCache.get(key);
......
...@@ -24,7 +24,7 @@ public interface IDaoSession { ...@@ -24,7 +24,7 @@ public interface IDaoSession {
<T extends IModule> int dbReadset(IModuleList<T> list, String whereSql,Object[] params); <T extends IModule> int dbReadset(IModuleList<T> list, String whereSql,Object[] params);
void dbReadset(IModuleList[] lists, int maxSize, String whereClause, Object[] datas); <T extends IModule> int dbReadset(IModuleList<T>[] lists, int maxSize, String whereClause, Object[] datas);
void dbReadset(IModuleList[] lists, String whereClause, Object[] datas); void dbReadset(IModuleList[] lists, String whereClause, Object[] datas);
void dbExecuteSQL(String sql,Object... params); void dbExecuteSQL(String sql,Object... params);
......
...@@ -166,12 +166,12 @@ public class ModuleList<T extends IModule> extends ArrayList<T> implements IModu ...@@ -166,12 +166,12 @@ public class ModuleList<T extends IModule> extends ArrayList<T> implements IModu
@JsonIgnore @JsonIgnore
public T getElement(int idx) { public T getElement(int idx) {
return super.get(--idx); return super.get(idx--);
} }
private void setFieldValue(String name, Object value, T bean) { private void setFieldValue(String name, Object value, T bean) {
try { try {
Field f = this.getDataClass().getField(name); Field f = this.getDataClass().getDeclaredField(name);
f.setAccessible(true); f.setAccessible(true);
f.set(bean, value); f.set(bean, value);
} catch (Exception e) { } catch (Exception e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment