连表查询 select 生成

连表查询 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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值