Commit 54c3c53b by 吴佳

dbReadset 分页查询优化 ,需要添加CommonMapper类 和 commonmapper.xml

parent 890cb66e
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;
......@@ -13,6 +14,7 @@ import com.brilliance.mda.support.mybatis.config.MybatisDbUtil;
import com.brilliance.mda.support.mybatis.count.CounterService;
import com.brilliance.mda.support.mybatis.dync.mapper.DbExecuteMapper;
import com.github.pagehelper.PageHelper;
import jdk.nashorn.internal.ir.annotations.Ignore;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
......@@ -30,9 +32,12 @@ 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;
......@@ -253,27 +258,101 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
for (IModuleList iModule : lists) {
clazzList.add(iModule.getDataClass());
}
for (IModuleList iModule : lists) {
//如果是多表查询 ,whereSql拼接 select 别名.*
try {
String tableName = ((AbstractModule)Class.forName(iModule.getDataClass().getName()).newInstance()).getTableName();
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(SELECT, clazzList, whereSql, param,tableName);
if (cacheOption != null) {
PageHelper.startPage(pageNum, pageSize);
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(SELECT, clazzList, whereSql, param);
if (cacheOption != null) {
PageHelper.startPage(pageNum, pageSize);
}
//多表查询时,走CommonMapper查询
if(lists.length>1) {
//调用commonMapper中的dyncRead
List<HashMap<String , Object>> result = sqlSessionTemplate.selectList("com.ceb.gjjs.mda.dao.CommonMapper.dyncRead",
adapter.getSqlParams());
if(result != null && result.size()>0){
String fieldName = "";
String methodName = "";
Object fieldVal = "";
Method method = null;
String tName = "";
//遍历结果集
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();
Field[] fields = moduleClazz.getDeclaredFields();
//遍历module 中的 属性 将结果集中对应的 表名_属性名 的值拼接到
for (Field field : fields) {
//@Ignore 的属性不用赋值
if(MdaUtils.isEmpty(field.getAnnotation(Ignore.class))) {
fieldName = field.getName();
//获取属性对应列的值
fieldVal = hm.get((tName + "_" + fieldName).toUpperCase());
//拼接setXXX方法名
methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
//查找方法
method = moduleClazz.getDeclaredMethod(methodName, field.getType());
//如果结果集对应列 值为null
if(fieldVal == null ){
//查询列结果为NULL时 对Integer 和Bigdecimal 赋初始值
if(field.getType().getName().equals("java.lang.Integer")){
method.invoke(module, 0);
}else if(field.getType().getName().equals("java.math.BigDecimal")){
method.invoke(module, new BigDecimal("0.00"));
}else {//其他类型根据入参类型反射生成对应类型
method.invoke(module, field.getType().newInstance());
}
}else{//结果集对应列 值不为null
//如果入参需要Integer 实际传入的是BigDecimal 类型,将结果接转为Integer类型
if(field.getType().getName().equals("java.lang.Integer") && fieldVal instanceof BigDecimal) {
method.invoke(module, ((BigDecimal) fieldVal).intValue());
}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);
}
}
}
//将解析后的module 添加到IModuleList 中
iModule.add(module);
} catch (InstantiationException 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);
}
}
}
}
}else{//单表查询走原来的逻辑
for (IModuleList iModule : lists) {
List<T> result = this.dyncRead(iModule.getDataClass(), adapter);
iModule.addAll(result);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
if (cacheOption != null) {
PageHelper.clearPage();
}
......
package com.brilliance.mda.support.mybatis;
import com.brilliance.mda.runtime.mda.IModule;
import com.brilliance.mda.runtime.mda.RuleExecuteException;
import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.support.mybatis.entity.Column;
import com.brilliance.mda.support.mybatis.entity.Table;
import jdk.nashorn.internal.ir.annotations.Ignore;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.PlainSelect;
......@@ -166,18 +168,37 @@ public class MybatisArgumentAdapter {
String sql = "";
if (join) {
sql = String.format(pattern, conditions);
Map<String, String> tableNameMap = parseTableNameAndAlias(sql);
String aliasName = tableNameMap.get(this.selectTable);
if(!MdaUtils.isEmpty(aliasName)){
sql = sql.replace("*" , aliasName+".*");
//如果是多表查询
if(null != clazzList && clazzList.size()>1) {
StringBuffer selectColumn = new StringBuffer();
//获取SQL中别名和表明的对应关系
Map<String, String> tableNameMap = parseTableNameAndAlias(String.format(pattern, whereSql));
for (Class<? extends IModule> clazz : clazzList) {
//根据模型注解中的@Table 获取当前模型对应的表名
String tableNam = clazz.getAnnotation(Table.class).value();
//根据表明获取表别名
String aliasName = tableNameMap.get(tableNam);
//当前模型属性
Field[] fields = clazz.getDeclaredFields();
//遍历模型中的属性 ,拼接SQL中 SELECT 列部分的内容
//拼接规则 别名.属性名(字段名) 表名_属性名(字段名)
//后续对结果集进行解析 根据表名和属性名解析
for (Field field : fields) {
//@Ignore 的字段不做拼接
if(MdaUtils.isEmpty(field.getAnnotation(Ignore.class))) {
if (!MdaUtils.isEmpty(selectColumn)) {
selectColumn.append(",");
}
selectColumn.append(" ");
selectColumn.append(aliasName).append(".").append(field.getName());
selectColumn.append(" ");
selectColumn.append(tableNam).append("_").append(field.getName());
}
}
}
//拼接完成之后替换 “*”
sql = sql.replace("*" , selectColumn.toString());
}
// if(conditions.startsWith("SELECT") || conditions.startsWith("select")){
// sql = conditions;
// }else {
//
// sql = String.format(pattern, conditions);
// }
} else {
sql = String.format(pattern, tableName, conditions);
}
......@@ -205,7 +226,8 @@ public class MybatisArgumentAdapter {
}
}
}catch (Exception e){
e.printStackTrace();
log.error("解析SQL:/n{} ---------error",sql);
throw new RuleExecuteException("parseTableNameAndAlias:解析SQL时出错",e);
}
return map;
}
......
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