Commit 610b8efa by 吴佳

dbReadset 分页查询调整

parent ec0f1b57
......@@ -4,6 +4,7 @@ import com.brilliance.mda.runtime.mda.*;
import com.brilliance.mda.runtime.mda.driver.MdaDriver;
import com.brilliance.mda.runtime.mda.driver.MdaEnv;
import com.brilliance.mda.runtime.mda.impl.AbstractDaoSession;
import com.brilliance.mda.runtime.mda.impl.AbstractModule;
import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.impl.ModuleList;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
......@@ -12,6 +13,14 @@ 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 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;
......@@ -20,6 +29,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
......@@ -229,26 +239,39 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
@Override
public <T extends IModule> int dbReadset(IModuleList<T>[] lists, CacheOption cacheOption, String whereSql, Object[] param) {
IContext ctx = MdaEnv.getContext();
BaseVO baseVO = ctx.getVo();
int pageNum = baseVO.getPageNum();
int pageSize = baseVO.getPageSize();
if (isMultiDb) {
String moduleDB = MdaUtils.getModuleListsDB(lists);
DynamicDataSourceContextHolder.setDataSourceType(moduleDB);
}
if (cacheOption != null) {
IContext ctx = MdaEnv.getContext();
BaseVO baseVO = ctx.getVo();
int pageNum = baseVO.getPageNum();
int pageSize = baseVO.getPageSize();
PageHelper.startPage(pageNum, pageSize);
}
List<Class<? extends IModule>> clazzList = new ArrayList<>();
for (IModuleList iModule : lists) {
clazzList.add(iModule.getDataClass());
}
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(SELECT, clazzList, whereSql, param);
for (IModuleList iModule : lists) {
List<T> result = this.dyncRead(iModule.getDataClass(), adapter);
iModule.addAll(result);
//如果是多表查询 ,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);
}
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) {
......@@ -476,6 +499,8 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
}
private <T> List<T> dyncRead(Class<T> clazz, Map params) {
String sqlId = getSqlId(clazz, SQLID_DYNCREAD);
......
......@@ -2,8 +2,14 @@ package com.brilliance.mda.support.mybatis;
import com.brilliance.mda.runtime.mda.IModule;
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 net.sf.jsqlparser.parser.CCJSqlParserUtil;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
......@@ -59,6 +65,7 @@ public class MybatisArgumentAdapter {
private String pattern;
private Object[] params;
private String tableName;
private String selectTable;
public MybatisArgumentAdapter(AdaType type, Class<? extends IModule> clazz, Argument... args) {
......@@ -82,6 +89,16 @@ public class MybatisArgumentAdapter {
this.init();
}
public MybatisArgumentAdapter(AdaType type, List<Class<? extends IModule>> clazzList, String whereSql, Object[] params,String selectTable) {
this.clazzList = clazzList;
this.pattern = type.getValue();
this.whereSql = whereSql;
this.params = params;
this.tableName = convertTableNames();
this.selectTable = selectTable;
this.init();
}
public MybatisArgumentAdapter(AdaType type, List<Class<? extends IModule>> clazzList, String whereSql, Object[] params) {
this.clazzList = clazzList;
this.pattern = type.getValue();
......@@ -148,11 +165,19 @@ public class MybatisArgumentAdapter {
String sql = "";
if (join) {
if(conditions.startsWith("SELECT") || conditions.startsWith("select")){
sql = conditions;
}else {
sql = String.format(pattern, conditions);
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(conditions.startsWith("SELECT") || conditions.startsWith("select")){
// sql = conditions;
// }else {
//
// sql = String.format(pattern, conditions);
// }
} else {
sql = String.format(pattern, tableName, conditions);
}
......@@ -161,6 +186,30 @@ public class MybatisArgumentAdapter {
this.sqlParams.put(HOLDER_CONDS, conditions);
}
// 获取表及对应的别名
public Map<String,String> parseTableNameAndAlias(String sql){
Map<String,String> map = new HashMap<>();
try {
Select select = (Select) CCJSqlParserUtil.parse(sql);
SelectBody selectBody = select.getSelectBody();
PlainSelect plainSelect = (PlainSelect)selectBody;
net.sf.jsqlparser.schema.Table table = (net.sf.jsqlparser.schema.Table)plainSelect.getFromItem();
if(table.getAlias() != null){
map.put(table.getName(),table.getAlias().getName());
}
for(Join join : plainSelect.getJoins()){
net.sf.jsqlparser.schema.Table table1 = (net.sf.jsqlparser.schema.Table)join.getRightItem();
if(table1.getAlias()!=null){
map.put(table1.getName(),table1.getAlias().getName());
}
}
}catch (Exception e){
e.printStackTrace();
}
return map;
}
public String getSqlTemplate() {
return sqlTemplate;
......
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