SpringBoot+mybatis快速生成实体类,controller,service,dao,entity

此类是加上了swagger的


import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class GenEntityMysql {
   
    private static final GenEntityMysql INSTANCE = new GenEntityMysql();

    private String tableName;// 表名
    private String[] colNames; // 列名数组
    private String[] colTypes; // 列名类型数组
    private String[] colComment; // 列名注释
    private int[] colSizes; // 列名大小数组
    private boolean needUtil = false; // 是否需要导入包java.util.*
    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final String SQL = "select column_name,column_type,column_comment from INFORMATION_SCHEMA.Columns where table_name = ";// 数据库操作

    // TODO 需要修改的地方
    private static final String URL = "";//数据库地址
    private static final String NAME = "";
    private static final String PASS = "";
    private static final String DRIVER = "com.mysql.jdbc.Driver";

    private String entityOutPath = "";//实体类生成所在包的路径
    private String daoOutPath = "";  // 指定dao层生成所在包的路径
    private String serviceOutPath = "";  // 指定service层生成所在包的路径
    private String controlOutPath = "";  // 指定control层生成所在包的路径

    private String resultPath = "import com.nc.result.Result;";//自行封装的结果类


    // 无需修改,自动生成
    private String daoClassName; // dao层类名
    private String serviceClassName; // service层类型
    private String controlClassName; // control层类名

    private String authorName = "tcd";// 作者名字
    private String[] generateTables = {
   ""};//指定需要生成的表的表名

    /**
     * 类的构造方法
     */
    private GenEntityMysql() {
   
    }

    /**
     * @return 生成class的所有内容
     * @update 2020/05/11 17:30
     */
    private String parse() {
   
        StringBuffer sb = new StringBuffer();
        sb.append("package " + entityOutPath + ";\r\n");
        sb.append("\r\n");
        sb.append("import io.swagger.annotations.ApiModel;\r\n");

        sb.append("import java.util.Date;\r\n");
        sb.append("import io.swagger.annotations.ApiModelProperty;\r\n");
        sb.append("import lombok.Data;\nimport lombok.NoArgsConstructor;\nimport lombok.AllArgsConstructor;\n");
        // 判断是否导入工具包
        if (needUtil) {
   
            sb.append("import java.util.Date;\r\n");
        }
        // 注释部分
        sb.append("/**\r\n");
        sb.append(" *\r\n");
        sb.append(" * Created by " + authorName + " on " + SDF.format(new Date()) + "\r\n");
        sb.append(" */ \r\n");
        // 实体部分
        sb.append("@Data\n@ApiModel\n@AllArgsConstructor\n@NoArgsConstructor\n");
        sb.append("public class " + getTransStr(tableName, true) + "{\r\n\r\n");
        processAllAttrs(sb);// 属性
        sb.append("\r\n");
        //   processAllMethod(sb);// get set方法
        sb.append("}\r\n");
        return sb.toString();
    }

    /**
     * @return 生成dao的所有内容
     * @update 2020/05/11 17:30
     */
    private String daoParse() {
   
        StringBuffer sb = new StringBuffer();
        String transStr = getTransStr(tableName, true);
        String transStr1 = getTransStr(tableName, false);
        sb.append("package " + daoOutPath + ";\r\n");
        sb.append("\r\n");
        sb.append("import org.springframework.stereotype.Repository;\nimport org.apache.ibatis.annotations.*;\n");
        sb.append("import " + entityOutPath + "." + transStr + ";\r\n");
        sb.append("import java.util.List;\n");
        sb.append("\r\n");
        // 注释部分
        sb.append("/**\r\n");
        sb.append(" * Created by " + authorName + " on " + SDF.format(new Date()) + "\r\n");
        sb.append(" */ \r\n");

        sb.append("@Repository\n");
        sb.append("public interface " + transStr + "Dao" + "{\r\n\r\n");

        sb.append("\t//查询" + tableName + "1信息");
        sb.append("\r\n");
        sb.append("\t@Select(\"select * from " + tableName + "\")\n");   // select
        sb.append("\tList<" + transStr + "> find" + transStr + "(" + transStr + " " + transStr1 + ");\n\n");

        StringBuffer insertCol = new StringBuffer();
        StringBuffer insertCol1 = new StringBuffer();
        for (int i =  colNames.length - 1; i > 0 ; i--) {
   
            insertCol.append((i == colNames.length - 1 ? "" : ",") + colNames[i]);
            insertCol1.append("#{" + getTransStr(colNames[i], false) + "}" + (i == colNames.length || i == 1 ? "" : ","));
        }
        sb.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值