All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lq.cli.mybatis.CreateMapperXmlTask Maven / Gradle / Ivy

package com.lq.cli.mybatis;


import com.lq.cli.comment.CreateTask;
import com.lq.cli.comment.FileUtil;
import com.lq.cli.comment.StringUtil;
import com.lq.cli.comment.TaskArgs;
import com.lq.cli.jdbc.TableFiledEntity;
import com.lq.cli.jdbc.TableInfo;

import java.io.File;
import java.util.List;

public class CreateMapperXmlTask extends CreateTask {

    private List tableInfos;

    public CreateMapperXmlTask(TaskArgs taskArgs, List tableInfos) {
        super(taskArgs);
        this.tableInfos = tableInfos;
    }

    @Override
    public Boolean call() throws Exception {
        String mapperResourcesPath = taskArgs.projectPath + File.separator + "src" + File.separator + "main"
                + File.separator + "resources" + File.separator + "mapper" + File.separator;
        File mapperDir = new File(mapperResourcesPath);
        if (!mapperDir.exists()) {
            boolean mkdirs = mapperDir.mkdirs();
            if (!mkdirs){
                return false;
            }
        }
        for (TableInfo tableInfo : tableInfos) {
            String mapperXml = tableInfo2MapperXml(tableInfo);
            File file = new File(mapperResourcesPath + tableInfo.getTransformTableInfo().getTableName()+ "Mapper.xml");
            if (!file.exists()){
                FileUtil.createWriteFile(file, mapperXml);
            }
        }
        return true;
    }


    private String tableInfo2MapperXml(TableInfo tableInfo) {
        TableInfo transformTableInfo = tableInfo.getTransformTableInfo();
        String mapperXmlHeader = "\n" +
                "\n\n\n")
                .append("\t\n\t\tinsert into ")
                .append(tableInfo.getTableName())
                .append("(");
        List filedEntities = tableInfo.getFiledEntities();
        StringBuilder sqlField = new StringBuilder();
        for (TableFiledEntity filedEntity : filedEntities) {
            sqlField.append(filedEntity.getName()).append(",");
        }
        sb.append(sqlField).deleteCharAt(sb.length() - 1).append(")\n\t\tvalues(");
        List filedEntities2 = transformTableInfo.getFiledEntities();
        for (TableFiledEntity filedEntity : filedEntities2) {
            sb.append("#{").append(filedEntity.getName()).append("},");
        }
        sb.deleteCharAt(sb.length() - 1).append(")\n\t");
        for (int i = 0; i < filedEntities2.size(); i++) {
            TableFiledEntity priKey = filedEntities2.get(i);
            if (priKey.getKey().equals("PRI")) {
                sb.append("\n\n\t\n\n\t\n\t\tdelete from ")
                        .append(tableInfo.getTableName())
                        .append(" where ")
                        .append(filedEntities.get(i).getName())
                        .append("=#{")
                        .append(priKey.getName())
                        .append("}\n\t\n\n\t\n\t\tupdate ")
                        .append(tableInfo.getTableName()).append("\n\t\tset ");
                for (int x = 0; x < filedEntities2.size(); x++) {
                    TableFiledEntity filedEntity = filedEntities.get(x);
                    TableFiledEntity filedEntity1 = filedEntities2.get(x);
                    if (!filedEntity.getName().equals(priKey.getName())) {
                        sb.append(filedEntity.getName()).append("=#{").append(filedEntity1.getName()).append("},");
                    }
                }
                sb.deleteCharAt(sb.length() - 1).append("\n\t\twhere ")
                        .append(filedEntities.get(i).getName())
                        .append("=#{")
                        .append(priKey.getName())
                        .append("}\n\t");
                break;
            }
        }
        sb.append("\n\n");
        return sb.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy