Commit a98a4dc6 by s_guodong

设置228

parent ac94c103
package com.brilliance.mda.support.mybatis;
import com.brilliance.mda.runtime.annotation.Module;
import com.brilliance.mda.runtime.mda.*;
import com.brilliance.mda.runtime.mda.driver.MdaDriver;
import com.brilliance.mda.runtime.mda.driver.MdaEnv;
......@@ -16,14 +15,6 @@ import com.brilliance.mda.support.mybatis.dync.mapper.DbExecuteMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.ir.annotations.Ignore;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.util.TablesNamesFinder;
import org.apache.commons.beanutils.MethodUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
......@@ -32,13 +23,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.io.StringReader;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;
import java.util.*;
import static com.brilliance.mda.support.mybatis.MybatisArgumentAdapter.AdaType.DELETE;
......@@ -220,6 +209,10 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
Map<String, Object> dyncMap = new HashMap<>();
dyncMap.put("inr", inr);
T entity = this.dyncReadByInr(module.getClass(), dyncMap);
if (entity == null) {
setNoMoreRow();
return null;
}
if (entity != null) {
MdaDriver.copyValues(module, entity);
}
......@@ -254,7 +247,6 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
}
List<Class<? extends IModule>> clazzList = new ArrayList<>();
for (IModuleList iModule : lists) {
clazzList.add(iModule.getDataClass());
......@@ -265,33 +257,33 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
}
//多表查询时,走CommonMapper查询
if(lists.length>1) {
if (lists.length > 1) {
//调用commonMapper中的dyncRead
List<HashMap<String , Object>> result = sqlSessionTemplate.selectList("com.brilliance.mda.support.mybatis.count.mapper.CommonMapper.dyncRead",
adapter.getSqlParams());
PageInfo<HashMap<String , Object>> pageInfo = new PageInfo<HashMap<String , Object>>(result);
List<HashMap<String, Object>> result = sqlSessionTemplate.selectList("com.brilliance.mda.support.mybatis.count.mapper.CommonMapper.dyncRead",
adapter.getSqlParams());
PageInfo<HashMap<String, Object>> pageInfo = new PageInfo<HashMap<String, Object>>(result);
long total = pageInfo.getTotal();
baseVO.setTotal(total);
if(result != null && result.size()>0){
if (result != null && result.size() > 0) {
String fieldName = "";
String methodName = "";
Object fieldVal = "";
Method method = null;
String tName = "";
//遍历结果集
for (HashMap<String , Object> hm: result) {
for (HashMap<String, Object> hm : result) {
//编列 moduleList 集合
for (IModuleList iModule : lists) {
try {
Class moduleClazz = Class.forName(iModule.getDataClass().getName());
AbstractModule module = (AbstractModule)moduleClazz.newInstance();
tName = module.getTableName();
AbstractModule module = (AbstractModule) moduleClazz.newInstance();
tName = module.getTableName();
Field[] fields = moduleClazz.getDeclaredFields();
//遍历module 中的 属性 将结果集中对应的 表名_属性名 的值拼接到
for (Field field : fields) {
//@Ignore 的属性不用赋值
if(MdaUtils.isEmpty(field.getAnnotation(Ignore.class))) {
if (MdaUtils.isEmpty(field.getAnnotation(Ignore.class))) {
fieldName = field.getName();
//获取属性对应列的值
fieldVal = hm.get((tName + "_" + fieldName).toUpperCase());
......@@ -301,35 +293,35 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
//查找方法
method = moduleClazz.getDeclaredMethod(methodName, field.getType());
//如果结果集对应列 值为null
if(fieldVal == null ){
if (fieldVal == null) {
//查询列结果为NULL时 对Integer 和Bigdecimal 赋初始值
if(field.getType().getName().equals("java.lang.Integer")){
if (field.getType().getName().equals("java.lang.Integer")) {
method.invoke(module, 0);
}else if(field.getType().getName().equals("java.math.BigDecimal")){
} else if (field.getType().getName().equals("java.math.BigDecimal")) {
method.invoke(module, new BigDecimal("0.00"));
}else {//其他类型根据入参类型反射生成对应类型
} else {//其他类型根据入参类型反射生成对应类型
method.invoke(module, field.getType().newInstance());
}
}else{//结果集对应列 值不为null
} else {//结果集对应列 值不为null
//如果入参需要Integer 实际传入的是BigDecimal 类型,将结果接转为Integer类型
if(field.getType().getName().equals("java.lang.Integer") && fieldVal instanceof BigDecimal) {
if (field.getType().getName().equals("java.lang.Integer") && fieldVal instanceof BigDecimal) {
method.invoke(module, ((BigDecimal) fieldVal).intValue());
}else{
} else {
method.invoke(module, fieldVal);
}
}
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
log.info("类{}中,根据方法名{},入参类型{}未找到方法",iModule.getDataClass().getName(),methodName,field.getType());
throw new RuleExecuteException("执行方法出错",e);
}catch (IllegalAccessException e) {
log.info("执行方法{},需要入参类型{},实际传入参数类型{}",methodName,field.getType(),fieldVal.getClass().getName());
throw new RuleExecuteException("执行方法出错",e);
}catch(IllegalArgumentException e){
log.error("类{}执行方法{},需要入参类型{},实际传入参数类型{}",iModule.getDataClass().getName(),methodName,
field.getType(),fieldVal.getClass().getName());
throw new RuleExecuteException("执行方法出错",e);
log.info("类{}中,根据方法名{},入参类型{}未找到方法", iModule.getDataClass().getName(), methodName, field.getType());
throw new RuleExecuteException("执行方法出错", e);
} catch (IllegalAccessException e) {
log.info("执行方法{},需要入参类型{},实际传入参数类型{}", methodName, field.getType(), fieldVal.getClass().getName());
throw new RuleExecuteException("执行方法出错", e);
} catch (IllegalArgumentException e) {
log.error("类{}执行方法{},需要入参类型{},实际传入参数类型{}", iModule.getDataClass().getName(), methodName,
field.getType(), fieldVal.getClass().getName());
throw new RuleExecuteException("执行方法出错", e);
}
}
}
......@@ -337,20 +329,22 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
iModule.add(module);
} catch (InstantiationException e) {
log.error("实例化{}错误",iModule.getDataClass().getName());
throw new RuleExecuteException(iModule.getDataClass().getName()+"实例化失败",e);
log.error("实例化{}错误", iModule.getDataClass().getName());
throw new RuleExecuteException(iModule.getDataClass().getName() + "实例化失败", e);
} catch (ClassNotFoundException e) {
log.error("类{}找不到",iModule.getDataClass().getName());
throw new RuleExecuteException(iModule.getDataClass().getName()+"找不到",e);
}catch (IllegalAccessException e) {
log.info("实例化{}错误",iModule.getDataClass().getName());
throw new RuleExecuteException(iModule.getDataClass().getName()+"实例化失败",e);
log.error("类{}找不到", iModule.getDataClass().getName());
throw new RuleExecuteException(iModule.getDataClass().getName() + "找不到", e);
} catch (IllegalAccessException e) {
log.info("实例化{}错误", iModule.getDataClass().getName());
throw new RuleExecuteException(iModule.getDataClass().getName() + "实例化失败", e);
}
}
}
} else {
setNoMoreRow();
}
}else{//单表查询走原来的逻辑
} else {//单表查询走原来的逻辑
for (IModuleList iModule : lists) {
List<T> result = this.dyncRead(iModule.getDataClass(), adapter);
iModule.addAll(result);
......@@ -474,7 +468,7 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
Set<Map.Entry<String, Object>> entries = map1.entrySet();
//结果集只有1列数据,切查询语句中包含COUNT 函数名,则判断SQL为查询数据行数
//将结果集值类型从BigDecimal 转换为Integer
if (entries.size() == 1 && sql.toUpperCase().contains("COUNT")){
if (entries.size() == 1 && sql.toUpperCase().contains("COUNT")) {
for (Map.Entry<String, Object> entry : entries) {
if (entry.getValue() instanceof BigDecimal) {
entry.setValue(((BigDecimal) entry.getValue()).intValue());
......@@ -595,8 +589,6 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
}
private <T> List<T> dyncRead(Class<T> clazz, Map params) {
String sqlId = getSqlId(clazz, SQLID_DYNCREAD);
......
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