Commit f2b9429f by s_guodong

多表分页问题

parent 8be5ce1f
......@@ -91,7 +91,7 @@ public class MybatisArgumentAdapter {
this.init();
}
public MybatisArgumentAdapter(AdaType type, List<Class<? extends IModule>> clazzList, String whereSql, Object[] params,String selectTable) {
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;
......@@ -168,66 +168,68 @@ public class MybatisArgumentAdapter {
String sql = "";
if (join) {
sql = String.format(pattern, conditions);
//如果是多表查询
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());
} else {
sql = String.format(pattern, tableName, conditions);
}
//如果是多表查询
if (null != clazzList && clazzList.size() > 1) {
StringBuffer selectColumn = new StringBuffer();
//获取SQL中别名和表明的对应关系
Map<String, String> tableNameMap = parseTableNameAndAlias(sql);
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());
}
} else {
sql = String.format(pattern, tableName, conditions);
//拼接完成之后替换 “*”
sql = sql.replace("*", selectColumn.toString());
}
this.sqlTemplate = sql;
this.sqlParams.put(HOLDER_SQL, this.sqlTemplate);
this.sqlParams.put(HOLDER_CONDS, conditions);
}
// 获取表及对应的别名
public Map<String,String> parseTableNameAndAlias(String sql){
Map<String,String> map = new HashMap<>();
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());
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());
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){
log.error("解析SQL:/n{} ---------error",sql);
throw new RuleExecuteException("parseTableNameAndAlias:解析SQL时出错",e);
} catch (Exception e) {
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