连表查询 select 生成
结果
select
plan.*,
time_slot.*,
program.*
from plan, plan_time_slot,
time_slot, time_slot_program,
program
where plan.id = plan_time_slot.plan_id
and plan_time_slot. time_slot_id = time_slot.id
and time_slot.id = time_slot_program.time_slot_id
and time_slot_program. program_id = program.id ;
package com.gm.wj.util;
import com.gm.wj.entity.Notice;
import lombok.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Data
public class CodeGen {
// public static void main(String[] args) {
List<String> tableNames=new ArrayList<>();
List<String> tableNames= Arrays.asList("plan","time_slot","program");
int frontIdx=0;
int linkIdx=1;
int endIdx=2;
String whereCode=" where ";
for (int i = 0; i < tableNames.size(); i++) {
String frontTable = tableNames.get(frontIdx);
String linkTable = tableNames.get(linkIdx);
String backTable = tableNames.get(endIdx);
String linkTableName=frontTable+"_"+backTable;
whereCode+="#frontTable#.id = #linkTableName#.#frontTable#_id ";
whereCode+="#linkTableName#. #endTable#_id = #endTable#.id ";
}
// List<String> tableNames= Arrays.asList("plan","time_slot","program");
// String whereCode = makeWhereCode(tableNames);
// String selectCode = makeSelectCode(tableNames);
// String fromCode = makeFromCode(tableNames);
// System.out.println(selectCode+fromCode+whereCode);
// }
static String makeFromCode( List<String> tableNames){
// List<String> tableNames= Arrays.asList("plan","time_slot","program");
int frontIdx=0;
// int linkIdx=1;
int backIdx=1;
// int endIdx=2;
// String whereCode=" where ";
// 0 1 2
StringBuilder whereCode= new StringBuilder(" from ");
String firstTable = tableNames.get(0);
whereCode.append(firstTable).append(", ");
for (int i = 0; i < tableNames.size(); i++) {
String frontTable = tableNames.get(frontIdx);
// String linkTable = tableNames.get(backIdx);
String backTable = tableNames.get(backIdx);
// String backTable = tableNames.get(endIdx);
String linkTableName=frontTable+"_"+backTable;
// whereCode.append(" #frontTable#,\n#linkTableName#,\n#backTable#,"
whereCode.append(" #linkTableName#,\n#backTable#,"
.replace("#frontTable#", frontTable)
.replace("#backTable#", backTable)
.replace("#linkTableName#", linkTableName))
;
frontIdx++;
backIdx++;
if(backIdx>=tableNames.size()){
break;
}
}
// whereCode.rela
whereCode = new StringBuilder(whereCode.substring(0, whereCode.length() - 1));
whereCode.append("\n");
// whereCode= whereCode.replaceFirst("and","where");
// System.out.println(whereCode);
return whereCode.toString();
}
static String makeSelectCode( List<String> tableNames){
// List<String> tableNames= Arrays.asList("plan","time_slot","program");
int frontIdx=0;
// int linkIdx=1;
int backIdx=1;
// int endIdx=2;
// String whereCode=" where ";
StringBuilder whereCode= new StringBuilder(" select \n");
String firstTable = tableNames.get(0);
whereCode.append(firstTable+".*").append(",\n ");
List<String>codes=new ArrayList<>();
for (int i = 0; i < tableNames.size(); i++) {
String frontTable = tableNames.get(frontIdx);
// String linkTable = tableNames.get(backIdx);
String backTable = tableNames.get(backIdx);
// String backTable = tableNames.get(endIdx);
String linkTableName=frontTable+"_"+backTable;
// whereCode.append(" #frontTable#.*, \n #backTable#.*,"
codes.add(backTable+".*");
// whereCode.append(" #backTable#.*,"
// .replace("#frontTable#", frontTable)
// .replace("#backTable#", backTable)
// .replace("#linkTableName#", linkTableName))
// ;
frontIdx++;
backIdx++;
if(backIdx>=tableNames.size()){
break;
}
}
// System.out.println(codes);
String tables = String.join(",\n", codes);
whereCode.append(tables);
// whereCode = new StringBuilder(whereCode.substring(0, whereCode.length() - 1));
whereCode.append("\n");
// System.out.println(whereCode);
return whereCode.toString();
}
static String makeWhereCode( List<String> tableNames){
// List<String> tableNames= Arrays.asList("plan","time_slot","program");
int frontIdx=0;
// int linkIdx=1;
int backIdx=1;
// int endIdx=2;
// String whereCode=" where ";
StringBuilder whereCode= new StringBuilder(" ");
for (int i = 0; i < tableNames.size(); i++) {
String frontTable = tableNames.get(frontIdx);
// String linkTable = tableNames.get(backIdx);
String backTable = tableNames.get(backIdx);
// String backTable = tableNames.get(endIdx);
String linkTableName=frontTable+"_"+backTable;
whereCode.append(" \n and #frontTable#.id = #linkTableName#.#frontTable#_id \n and #linkTableName#. #backTable#_id = #backTable#.id "
.replace("#frontTable#", frontTable)
.replace("#backTable#", backTable)
.replace("#linkTableName#", linkTableName))
;
frontIdx++;
backIdx++;
if(backIdx>=tableNames.size()){
break;
}
}
whereCode = new StringBuilder(whereCode.toString().replaceFirst("and", "where"));
// System.out.println(whereCode);
return whereCode.toString();
}
public static void main(String[] args) {
// Notice notice = new Notice();
// String gen = gen(notice);
// System.out.println(gen);
// CodeGen codeGen = new CodeGen();
// codeGen.genAxios(notice);
codeGen.axiosGetCode.so
// System.out.println(codeGen.axiosPostCode);
List<String> tableNames= Arrays.asList("plan","time_slot","program");
// String whereCode = makeWhereCode(tableNames);
// String selectCode = makeSelectCode(tableNames);
// String fromCode = makeFromCode(tableNames);
// System.out.println(selectCode+fromCode+whereCode);
String linkedTableQuery = genLinkedTableQuery(tableNames);
System.out.println(linkedTableQuery);
}
static String genLinkedTableQuery( List<String> tableNames){
// List<String> tableNames= Arrays.asList("plan","time_slot","program");
String whereCode = makeWhereCode(tableNames);
String selectCode = makeSelectCode(tableNames);
String fromCode = makeFromCode(tableNames);
// System.out.println(selectCode+fromCode+whereCode);
return selectCode+fromCode+whereCode+";";
}
String axiosGetCode;
String axiosPostCode;
public void genAxios(Object object) {
String tableName = getTableName(object);
String jsonAllFiled = ObjectToJson.Object2JsonAllFiled(object);
String axiosGetCodeTpl = "axios.get( common.baseURL + \"/#tableName#\").then((resp) => {\n" +
" if (resp && resp.code === 200) {\n" +
" let data=resp.data.data\n" +
" this.#tableName#=data\n"+
" }\n" +
" });";
String axiosPostCodeTpl = "axios.post( common.baseURL + \"/#tableName#\",#dataForm#).then((resp) => {\n" +
" if (resp && resp.code === 200) {\n" +
" let data=resp.data.data\n" +
" this.#tableName#=data\n"+
" }\n" +
" });";
axiosGetCode = axiosGetCodeTpl.replace("#tableName#", tableName);
axiosPostCode = axiosPostCodeTpl.replace("#tableName#", tableName)
.replace("#dataForm#", jsonAllFiled);
}
public static String gen(Object object) {
String tableName = getTableName(object);
String jsonAllFiled = ObjectToJson.Object2JsonAllFiled(object);
String axiosCode = "axios.get( common.baseURL + \"/#tableName#\").then((resp) => {\n" +
" if (resp && resp.code === 200) {\n" +
" let data=resp.data.data" +
" }\n" +
" });";
String axiosPostCode = "axios.post( common.baseURL + \"/#tableName#\",#dataForm#).then((resp) => {\n" +
" if (resp && resp.code === 200) {\n" +
" let data=resp.data.data" +
" }\n" +
" });";
String replace = axiosCode.replace("#tableName#", tableName);
String replace1 = axiosPostCode.replace("#tableName#", tableName)
.replace("#dataForm#", jsonAllFiled);
return replace;
}
// public static <T> String getTableName(T obj) {
// String tableName = obj.getClass().getSimpleName();
// tableName = StringUtils.underscoreNameLower(tableName);
// return tableName;
// }
public static String getTableName(Object obj) {
String tableName = obj.getClass().getSimpleName();
tableName = StringUtils.underscoreNameLower(tableName);
return tableName;
}
}