生成代码目录结构如下:

Java自动生成代码技术_Java自动生成代码技术

如何使用:
  1. 在项目目录下新建工具类包util
  2. util包中新建 数据库连接工具类DbCon(类名必须为 DbCon)和 代码生成工具类 Automatically
  3. 修改代码配置,运行Automatically工具类即可
代码如下:

数据库连接工具类DbCon.java代码如下:

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author cimo
 * @create 2020/12
 */
public class DbCon {
    private static final String DBDRIVER = "com.mysql.jdbc.Driver";
    private static final String DBURL = "jdbc:mysql://localhost:3306/你的数据库?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
    private static final String DBUSER = "连接名";
    private static final String DBPASSWORD = "密码";
    private Connection conn;

    /**
     * 返回 conn 对象
     */
    public DbCon() throws Exception {
        try {
            Class.forName(DBDRIVER);
            this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
        } catch (Exception e) {
            throw e;
        }
    }

    public Connection getConn() {
        return this.conn;
    }

    public void close() throws Exception {
        if (this.conn != null) {
            try {
                this.conn.close();
            } catch (Exception e) {
                throw e;
            }
        }
    }
}

代码生成工具类Automatically.java代码如下:

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 *
 * @author cimo
 * @date 2020/12
 * 通过数据库表自动生成 JavaBean Dao Service Servlet Filter(请求响应编码过滤器)
 * 注意事项:
 *      1、确保数据库名为小写加下划线形式
 *      2、没有使用代码美化,请使用自己的代码编辑器美化代码
 * 命名规范:
 *      包名全小写,类名大驼峰,属性名小驼峰
 */
public class Automatically {

    /**
     * 指定实体生成所在包的路径
     */
    private static final String PACKAGE_OUT_PATH = "D:\\IDEA_workspace\\test\\final\\autot\\src\\main\\java";

    /**
     * 注释中作者名字
     */
    private static final String AUTHOR_NAME = "cimo";

    /**
     * 数据库表名数组
     */
    private static final String[] tableNameArray = new String[]{"admin_info"};
    private static String tableName = "";
    private static String daoName;
    private static String daoImplName;
    private static String serviceName;
    private static String serviceImplName;
    private static String servletName;

    private static final Pattern PATTERN = Pattern.compile("([A-Za-z\\d]+)(_)?");

    private static final String[] METHOD_PREFIX = {"insert", "delete", "update", "findAll", "findById"};

    /**
     * 各个模块路径
     */
    public static final String javaBeanPath = "\\com\\bean";
    public static final String servletPath = "\\com\\servlet";
    public static final String filterPath = "\\com\\filter";
    public static final String serviceImplPath = "\\com\\service\\impl";
    public static final String servicePath = "\\com\\service";
    public static final String daoPath = "\\com\\dao";
    public static final String daoImplPath = "\\com\\dao\\impl";

    /**
     * 列名数组
     */
    private static String[] colNames;

    /**
     * 列名类型数组
     */
    private static String[] colTypes;


    public static void main(String[] args) {
        // 遍历 多表
        for (String s : tableNameArray) {
            tableName = s;
            daoName = initCap(tableName,false) + "Dao";
            daoImplName = initCap(tableName,false) + "DaoImpl";
            serviceName = initCap(tableName,false) + "Service";
            serviceImplName = initCap(tableName,false) + "ServiceImpl";
            servletName = initCap(tableName,false) + "Servlet";
            //生成JavaBean
            generateJavaBean();
            //生成 Dao 接口
            generateDao();
            //生成 Dao 实现
            generateDaoImpl();
            //生成 service
            generateService();
            //生成 serviceImpl
            generateServiceImpl();
            //生成 servlet
            generateServlet();
            //生成编码 filter
            generateFilter();
        }
    }

    /**
     * 生成 JavaBean
     */
    private static void generateJavaBean() {
        //创建连接
        Connection con = null;
        PreparedStatement ps = null;
        //查要生成JavaBean的表
        String sql = "select * from " + tableName;
        try {
            //获取连接
            con = new DbCon().getConn();
            ps = con.prepareStatement(sql);
            ResultSetMetaData rsmd = ps.getMetaData();
            //返回列数
            int size = rsmd.getColumnCount();
            colNames = new String[size];
            colTypes = new String[size];
            //得到列名和列类型
            for (int i = 0; i < size; i++) {
                //列名
                colNames[i] = rsmd.getColumnName(i + 1);
                //列数据库类型名
                colTypes[i] = rsmd.getColumnTypeName(i + 1);
            }
            //得到JavaBean代码
            String content = parseJavaBean(colTypes);
            File directory = new File(PACKAGE_OUT_PATH + javaBeanPath);
            //创建目录
            directory.mkdirs();
            String outputPath = PACKAGE_OUT_PATH + javaBeanPath + "\\" + initCap(tableName,false) + ".java";
            PrintWriter pw = new PrintWriter(new FileWriter(outputPath));
            pw.println(content);
            pw.flush();
            pw.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成 JavaBean
     */
    private static String parseJavaBean(String[] colTypes) {
        StringBuffer sb = new StringBuffer();
        sb.append("package ").append(javaBeanPath.replace('\\', '.').substring(1)).append(";\r\n");
        sb.append("\r\n");
        for (String colType : colTypes) {
            if ("Date".equals(sqlType2JavaType(colType))) {
                sb.append("import java.sql.Date;\r\n\r\n");
            }
        }
        sb.append("import java.io.Serializable;\r\n\r\n");
        //注释部分
        sb.append("\r\n/**\r\n");
        sb.append(" * Created by @author " + AUTHOR_NAME + "\r\n");
        sb.append(" *\r\n");
        sb.append(" * @date ").append(new Date()).append("\r\n");
        sb.append(" */ \r\n");
        //实体部分
        sb.append("\r\n\r\npublic class ").append(initCap(tableName,false)).append(" implements Serializable {\r\n\r\n");
        //属性
        processAllAttrs(sb);
        //get set方法
        processAllMethod(sb);
        sb.append("}\r\n");
        return sb.toString();
    }

    /**
     * 生成属性
     */
    private static void processAllAttrs(StringBuffer sb) {
        for (int i = 0; i < colNames.length; i++) {
            try {
                sb.append("\tprivate ").append(sqlType2JavaType(colTypes[i])).append(" ").append(initCap(colNames[i])).append(";").append("\r\n");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        sb.append("\r\n");
    }

    /**
     * 生成 set get方法
     */
    private static void processAllMethod(StringBuffer sb) {
        //无参 有参
        sb.append("\tpublic ").append(initCap(tableName,false)).append("(){\r\n");
        sb.append("\t}\r\n\r\n");

        StringBuilder sb2 = new StringBuilder();
        for (int i = 0; i < colNames.length; i++) {
            sb2.append(sqlType2JavaType(colTypes[i])).append(" ").append(initCap(colNames[i])).append(",");
        }
        String s = sb2.toString();
        s = s.substring(0, s.length() - 1);
        sb.append("\tpublic ").append(initCap(tableName,false)).append("(").append(s).append("){\r\n");
        StringBuilder sb3 = new StringBuilder();
        for (String colName : colNames) {
            sb3.append("\t\tthis.").append(initCap(colName)).append(" = ").append(initCap(colName)).append(";\r\n");
        }
        String s2 = sb3.toString();
        sb.append(s2);
        sb.append("\t}\r\n\r\n");

        //set get
        for (int i = 0; i < colNames.length; i++) {
            sb.append("\tpublic void set").append(initCap(colNames[i],false)).append("(").append(sqlType2JavaType(colTypes[i])).append(" ").append(initCap(colNames[i])).append("){\r\n");
            sb.append("\t\tthis.").append(initCap(colNames[i])).append("=").append(initCap(colNames[i])).append(";\r\n");
            sb.append("\t}\r\n\r\n");
            sb.append("\tpublic ").append(sqlType2JavaType(colTypes[i])).append(" get").append(initCap(colNames[i],false)).append("(){\r\n");
            sb.append("\t\treturn ").append(initCap(colNames[i])).append(";\r\n");
            sb.append("\t}\r\n\r\n");
        }

        //toString
        sb.append("\t@Override\r\n");
        sb.append("\tpublic " + "String toString(){\r\n\t\treturn ");
        StringBuilder sb4 = new StringBuilder();
        for (String colName : colNames) {
            sb4.append("\",").append(initCap(colName)).append("=\" + ").append(initCap(colName)).append(" + \r\n\t\t");
        }
        String s3 = sb4.toString();
        s3 = s3.substring(0, 1) + s3.substring(2, s3.length() - 7) + ";";
        sb.append(s3);
        sb.append("\r\n\t}\r\n\n");


    }

    /**
     * 生成 Dao
     */
    public static void generateDao() {
        File directory = new File(PACKAGE_OUT_PATH + daoPath);
        //创建目录
        directory.mkdirs();
        try {
            //代码
            String content = parseDao();
            String outputPath = PACKAGE_OUT_PATH + daoPath + "\\" + daoName + ".java";
            FileWriter fw = new FileWriter(outputPath);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(content);
            pw.flush();
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 生成 Dao接口主体代码
     */
    private static String parseDao() {
        StringBuffer sb = new StringBuffer();
        sb.append("package "+daoPath.replace("\\",".").substring(1)+";\r\n");
        sb.append("\r\n");
        sb.append("import "+javaBeanPath.replace("\\",".").substring(1)+ "." + initCap(tableName,false) + ";");
        sb.append("\r\n");
        sb.append("import java.util.ArrayList;");
        sb.append("\r\n");
        //注释部分
        sb.append("\r\n/**\r\n");
        sb.append(" * Created by @author " + AUTHOR_NAME + "\r\n");
        sb.append(" *\r\n");
        sb.append(" * @date " + new Date() + "\r\n");
        sb.append(" */ \r\n");
        //实体部分
        sb.append("\r\n\r\npublic interface " + daoName + "{\r\n");
        //生成Dao所有方法
        processDaoAllMethod(sb);
        sb.append("}\r\n");

        return sb.toString();
    }

    /**
     * 功能:生成 Dao接口的方法
     * @param sb StringBuffer
     */
    private static void processDaoAllMethod(StringBuffer sb) {
        handleCRUD(sb);
    }

    private static void handleCRUD(StringBuffer sb) {
        for (String s : METHOD_PREFIX) {
            sb.append("\r\n");
            if ("findAll".equals(s)) {
                sb.append("\tArrayList<").append(initCap(tableName,false)).append("> ").append(s).append("();");
            } else if ("findById".equals(s)) {
                sb.append("\tArrayList<").append(initCap(tableName,false)).append("> ").append(s).append("(").append(initCap(tableName,false)).append(" ").append(initCap(tableName)).append(");");
            } else {
                sb.append("\tboolean ").append(s).append("(").append(initCap(tableName,false)).append(" ").append(initCap(tableName)).append(");");
            }
            sb.append("\t\r\n");
            sb.append("\r\n");
        }
    }

    /**
     * 生成 DaoService 接口实现
     */
    public static void generateDaoImpl() {
        File directory = new File(PACKAGE_OUT_PATH + daoImplPath);
        //创建目录
        directory.mkdirs();
        try {
            //代码
            String content = parseDaoImpl(directory);
            String outputPath = PACKAGE_OUT_PATH + daoImplPath + "\\" + daoImplName + ".java";
            FileWriter fw = new FileWriter(outputPath);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(content);
            pw.flush();
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能:生成 DaoImpl 主体代码
     * @param directory File
     */
    private static String parseDaoImpl(File directory) {
        StringBuffer sb = new StringBuffer();
        sb.append("package "+daoImplPath.replace("\\",".").substring(1)+";\r\n");
        sb.append("\r\n");
        sb.append("import java.util.ArrayList;");
        sb.append("\r\n");
        sb.append("import com.util.DbCon;");
        sb.append("\r\n");
        sb.append("import java.sql.*;");
        sb.append("\r\n");
        sb.append("import "+daoPath.replace("\\",".").substring(1)+".").append(daoName).append(";");
        sb.append("\r\n");
        sb.append("import "+javaBeanPath.replace("\\",".").substring(1)+".").append(initCap(tableName,false)).append(";");
        sb.append("\r\n");
        sb.append("\r\n");
        //注释部分
        sb.append("\r\n/**\r\n");
        sb.append(" * Created by @author ").append(AUTHOR_NAME).append("\r\n");
        sb.append(" *\r\n");
        sb.append(" * @date ").append(new Date()).append("\r\n");
        sb.append(" */ \r\n");
        //实体部分
        sb.append("\r\n\r\npublic class ").append(daoName).append("Impl").append(" implements ").append(daoName).append("{\r\n");
        //生成 Dao 所有方法
        processDaoImplAllMethod(sb);
        sb.append("}\r\n");
        return sb.toString();
    }

    /**
     * 生成 Dao 实现类方法
     */
    private static void processDaoImplAllMethod(StringBuffer sb) {

        String[] str = {"增加", "删除", "修改", "查询", "根据id进行查询"};
        for (int i = 0; i < METHOD_PREFIX.length; i++) {
            sb.append("\r\n\t");
            sb.append("/**\n" +
                    "\t * "+str[i]+"\n" +
                    "\t */");
            sb.append("\r\n");
            if ("增加".equals(str[i])) {
                sb.append("\t@Override\r\n");
                sb.append("\tpublic boolean ").append(METHOD_PREFIX[i]).append("(").append(initCap(tableName,false)).append(" ").append(initCap(tableName)).append(") {\r\n");
                daoImplInsert(sb);
                sb.append("\t}\r\n");
            } else if ("删除".equals(str[i])) {
                sb.append("\t@Override\r\n");
                sb.append("\tpublic boolean ").append(METHOD_PREFIX[i]).append("(").append(initCap(tableName,false)).append(" ").append(initCap(tableName)).append(") {\r\n");
                daoImplDelete(sb);
                sb.append("\t}\r\n");
            } else if ("修改".equals(str[i])) {
                sb.append("\t@Override\r\n");
                sb.append("\tpublic boolean ").append(METHOD_PREFIX[i]).append("(").append(initCap(tableName,false)).append(" ").append(initCap(tableName)).append(") {\r\n");
                daoImplUpdate(sb);
                sb.append("\t}\r\n");
            } else if ("查询".equals(str[i])) {
                sb.append("\t@Override\r\n");
                sb.append("\tpublic ArrayList<").append(initCap(tableName,false)).append("> ").append(METHOD_PREFIX[i]).append("() {\r\n");
                daoImplSelectAll(sb);
                sb.append("\t}\r\n");
            } else if ("根据id进行查询".equals(str[i])) {
                sb.append("\t@Override\r\n");
                sb.append("\tpublic ArrayList<").append(initCap(tableName,false)).append("> ").append(METHOD_PREFIX[i]).append("(").append(initCap(tableName,false)).append(" ").append(initCap(tableName)).append(") {\r\n");
                daoImplSelectAllById(sb);
                sb.append("\t}\r\n");
            }
            sb.append("\t\r\n");
        }
    }

    /**
     * 增加
     */
    private static void daoImplInsert(StringBuffer sb) {
        sb.append("\t\tboolean flag = false;");
        sb.append("\t\ttry {");
        //获取连接
        sb.append("\t\tConnection con = new DbCon().getConn();\r\n");
        sb.append("\t\tString sql = \"INSERT INTO ").append(tableName).append(" (");
        StringBuilder temporary = new StringBuilder();
        for (int i = 0; i < colNames.length; i++) {
            temporary.append(colNames[i]).append(",");
        }
        sb.append(temporary.substring(0, temporary.length() - 1));
        sb.append(")VALUES (");
        StringBuilder temporary2 = new StringBuilder();
        for (int i = 0; i < colNames.length; i++) {
            temporary2.append("?,");
        }
        sb.append(temporary2.substring(0, temporary2.length() - 1)).append(")\";");
        sb.append("\r\n\t\t");
        sb.append("PreparedStatement ps = con.prepareStatement(sql);");
        sb.append("\r\n\t\t");
        sb.append("\r\n\t\t\t");
        for (int i = 0; i < colNames.length; i++) {
            sb.append("\r\n\t\t\t");
            sb.append("ps.set").append(initCap(sqlType2JavaType(colTypes[i]), false)).append("(").append(i + 1).append(", ").append(initCap(tableName)).append(".get").append(initCap(colNames[i], false)).append("());");
        }
        sb.append("\r\n\t\t\t");
        sb.append(" int rs = ps.executeUpdate();\n" +
                "            if ( rs == 1 ) {\n" +
                "                flag = true;\n" +
                "            }");
        sb.append("ps.close();");
        sb.append("\r\n\t\t\t\t");
        sb.append("con.close();");

        sb.append("\r\n\t\t");
        sb.append("} catch (Exception e) {");
        sb.append("\r\n\t\t\t");
        sb.append("e.printStackTrace();");
        sb.append("\r\n\t\t");

        sb.append("\t\t}\r\n");
        sb.append("return flag;");
    }

    /**
     * 删除
     */
    private static void daoImplDelete(StringBuffer sb) {
        sb.append("\t\tboolean flag = false;");
        sb.append("try {");
        sb.append("\r\n\t\t\t");
        //获取连接
        sb.append("\t\tConnection con = new DbCon().getConn();\r\n");
        sb.append("\t\tString sql = \"DELETE FROM ").append(tableName).append(" WHERE ").append(colNames[0]).append(" = ?\";");
        sb.append("\r\n\t\t");
        sb.append("PreparedStatement ps = con.prepareStatement(sql);");
        sb.append("\r\n\t\t\t");
        sb.append("ps.set").append(initCap(sqlType2JavaType(colTypes[0]),false)).append("(").append(0 + 1).append(", ").append(initCap(tableName)).append(".get").append(initCap(colNames[0],false)).append("());");

        sb.append("\r\n\t\t\t");
        sb.append(" int rs = ps.executeUpdate();\n" +
                "            if ( rs == 1 ) {\n" +
                "                flag = true;\n" +
                "            }");
        sb.append("\r\n\t\t");
        sb.append("ps.close();");
        sb.append("\r\n\t\t\t\t");
        sb.append("con.close();");
        sb.append("} catch (Exception e) {");
        sb.append("\r\n\t\t\t");
        sb.append("e.printStackTrace();");
        sb.append("\r\n\t\t");
        sb.append("\t\t}\r\n");
        sb.append("return flag;");
    }

    /**
     * 修改
     */
    private static void daoImplUpdate(StringBuffer sb) {
        sb.append("\t\tboolean flag = false;");
        sb.append("\r\n\t\t");
        sb.append("try {");
        sb.append("\r\n\t\t\t");
        //获取连接
        sb.append("\n\t\tConnection con = new DbCon().getConn();\r\n");
        sb.append("\t\tString sql = \"UPDATE ").append(tableName).append(" SET ");
        StringBuilder temporary = new StringBuilder();
        for (String colname : colNames) {
            temporary.append(colname).append(" = ?,");
        }
        sb.append(temporary.substring(0, temporary.length() - 1)).append(" WHERE ").append(colNames[0]).append(" = ?\";");
        sb.append("\r\n\t\t");
        sb.append("PreparedStatement ps = con.prepareStatement(sql);");
        for (int i = 0; i < colNames.length; i++) {
            sb.append("\r\n\t\t\t");
            sb.append("ps.set").append(initCap(sqlType2JavaType(colTypes[i]),false)).append("(").append(i + 1).append(", ").append(initCap(tableName)).append(".get").append(initCap(colNames[i],false)).append("());");
        }
        sb.append("\r\n\t\t\t");
        sb.append("ps.set").append(initCap(sqlType2JavaType(colTypes[0]),false)).append("(").append(colNames.length + 1).append(", ").append(initCap(tableName)).append(".get").append(initCap(colNames[0],false)).append("());");
        sb.append("\r\n\t\t\t");
        sb.append(" int rs = ps.executeUpdate();\n" +
                "            if ( rs == 1 ) {\n" +
                "                flag = true;\n" +
                "            }");
        sb.append("\r\n\t\t");
        sb.append("ps.close();");
        sb.append("\r\n\t\t\t\t");
        sb.append("con.close();");
        sb.append("} catch (Exception e) {");
        sb.append("\r\n\t\t\t");
        sb.append("e.printStackTrace();");
        sb.append("\r\n\t\t");
        sb.append("}\r\n");
        sb.append("return flag;");
    }

    /**
     * 查询所有
     */
    private static void daoImplSelectAll(StringBuffer sb) {
        sb.append("\t\tArrayList<").append(initCap(tableName,false)).append("> list = new ArrayList<").append(initCap(tableName,false)).append(">();\r\n");
        sb.append("try {");
        //获取连接
        sb.append("\t\tConnection con = new DbCon().getConn();\r\n");
        sb.append("\t\tString sql = \"SELECT ");
        StringBuilder temporary = new StringBuilder();
        for (String colName : colNames) {
            temporary.append(colName).append(",");
        }
        sb.append(temporary.substring(0, temporary.length() - 1));
        sb.append(" FROM ").append(tableName).append("\";");
        sb.append("\r\n\t\t");
        sb.append("PreparedStatement ps = con.prepareStatement(sql);");
        sb.append("\r\n\t\t");
        sb.append("ResultSet rs = ps.executeQuery();");
        sb.append("\r\n\t\t");
        sb.append("\r\n\t\t\t");
        sb.append("while (rs.next()) {");
        sb.append("\r\n\t\t\t\t");
        sb.append(initCap(tableName,false)).append(" ").append(initCap(tableName)).append(" = new ").append(initCap(tableName,false)).append("();");
        for (int i = 0; i < colNames.length; i++) {
            sb.append("\r\n\t\t\t\t");
            sb.append(initCap(tableName)).append(".set").append(initCap(colNames[i],false)).append("(rs.get").append(initCap(sqlType2JavaType(colTypes[i]),false)).append("(\"").append(colNames[i]).append("\"));");
        }
        sb.append("\r\n\t\t\t\t");
        sb.append("list.add(").append(initCap(tableName)).append(");");
        sb.append("}");
        sb.append("\r\n\t\t\t");
        sb.append("rs.close();");
        sb.append("\r\n\t\t\t\t");
        sb.append("ps.close();");
        sb.append("\r\n\t\t\t\t");
        sb.append("con.close();");
        sb.append("return list;\r\n");
        sb.append("\r\n\t\t");
        sb.append("} catch (Exception e) {");
        sb.append("\r\n\t\t\t");
        sb.append("e.printStackTrace();");
        sb.append("\r\n\t\t");
        sb.append("} ");
        sb.append("\r\n\t\t");
        sb.append("return list;\r\n");
    }

    /**
     * 根据 id查询
     */
    private static void daoImplSelectAllById(StringBuffer sb) {
        sb.append("\t\tArrayList<").append(initCap(tableName,false)).append("> list = new ArrayList<").append(initCap(tableName,false)).append(">();\r\n");

        sb.append("\r\n\t\t");
        sb.append("try {");
        //获取连接
        sb.append("\t\tConnection con = new DbCon().getConn();\r\n");
        sb.append("\t\tString sql = \"SELECT ");
        StringBuilder temporary = new StringBuilder();
        for (String colname : colNames) {
            temporary.append(colname).append(",");
        }
        sb.append(temporary.substring(0, temporary.length() - 1));
        sb.append(" FROM ").append(tableName).append(" WHERE ").append(colNames[0]).append(" = ?\";");
        sb.append("\r\n\t\t");
        sb.append("PreparedStatement ps = con.prepareStatement(sql);");
        sb.append("\r\n\t\t");
        sb.append("ps.set").append(initCap(sqlType2JavaType(colTypes[0]),false)).append("(").append(1).append(", ").append(initCap(tableName)).append(".get").append(initCap(colNames[0],false)).append("());");
        sb.append("\r\n\t\t\t");
        sb.append("ResultSet rs = ps.executeQuery();");

        sb.append("\r\n\t\t\t");
        sb.append("while (rs.next()) {");
        sb.append("\r\n\t\t\t\t");
        sb.append(initCap(tableName,false)).append(" ").append(tableName.substring(0,1)).append(" = new ").append(initCap(tableName,false)).append("();");
        for (int i = 0; i < colNames.length; i++) {
            sb.append("\r\n\t\t\t\t");
            sb.append(tableName.substring(0,1)).append(".set").append(initCap(colNames[i],false)).append("(rs.get").append(initCap(sqlType2JavaType(colTypes[i]),false)).append("(\"").append(colNames[i]).append("\"));");
        }
        sb.append("\r\n\t\t\t\t");
        sb.append("list.add(").append(tableName.substring(0,1)).append(");");
        sb.append("\r\n\t\t\t");
        sb.append("}");
        sb.append("\r\n\t\t\t\t");
        sb.append("rs.close();");
        sb.append("\r\n\t\t\t\t");
        sb.append("ps.close();");
        sb.append("\r\n\t\t\t\t");
        sb.append("con.close();");
        sb.append("return list;\r\n");
        sb.append("\r\n\t\t");
        sb.append("} catch (Exception e) {");
        sb.append("\r\n\t\t\t");
        sb.append("e.printStackTrace();");
        sb.append("\r\n\t\t");
        sb.append("}");
        sb.append("\r\n\t\t");
        sb.append("return list;\r\n");
    }

    /**
     * 生成 Service
     */
    private static void generateService() {
        File directory = new File(PACKAGE_OUT_PATH + servicePath);
        directory.mkdirs();//创建目录
        try {
            String outputPath = PACKAGE_OUT_PATH + servicePath+ "\\" + serviceName + ".java";
            FileWriter fw = new FileWriter(outputPath);
            PrintWriter pw = new PrintWriter(fw);
            StringBuffer sb = new StringBuffer();
            sb.append("package com.service;\r\n");
            sb.append("\r\n");
            sb.append("import com.bean.").append(initCap(tableName,false)).append(";");
            sb.append("\r\n");
            sb.append("import java.util.ArrayList;");
            sb.append("\r\n");
            //注释部分
            sb.append("\r\n/**\r\n");
            sb.append(" * Created by @author ").append(AUTHOR_NAME).append("\r\n");
            sb.append(" *\r\n");
            sb.append(" * @date ").append(new Date()).append("\r\n");
            sb.append(" */ \r\n");
            //实体部分
            sb.append("\r\n\r\npublic interface ").append(serviceName).append("{\r\n");
            handleCRUD(sb);
            sb.append("}\r\n");
            //************************************************************************
            pw.println(sb.toString());
            pw.flush();
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成 ServiceImpl
     */
    private static void generateServiceImpl() {
        File directory = new File(PACKAGE_OUT_PATH + serviceImplPath);
        directory.mkdirs();//创建目录
        try {
            String outputPath = PACKAGE_OUT_PATH + serviceImplPath + "\\" + serviceImplName + ".java";
            FileWriter fw = new FileWriter(outputPath);
            PrintWriter pw = new PrintWriter(fw);
            //代码****************************************
            StringBuilder sb = new StringBuilder();
            sb.append("package "+serviceImplPath.replace("\\",".").substring(1)+";\r\n");
            sb.append("\r\n");
            sb.append("import java.util.ArrayList;");
            sb.append("\r\n");
            sb.append("import "+daoPath.replace("\\",".").substring(1)+".").append(daoName).append(";");
            sb.append("\r\n");
            sb.append("import "+daoPath.replace("\\",".").substring(1)+".impl.").append(daoImplName).append(";");
            sb.append("\r\n");
            sb.append("import "+servicePath.replace("\\",".").substring(1)+".").append(serviceName).append(";");
            sb.append("\r\n");
            sb.append("import "+javaBeanPath.replace("\\",".").substring(1)+".").append(initCap(tableName,false)).append(";");
            sb.append("\r\n");
            sb.append("\r\n");
            //注释部分
            sb.append("\r\n/**\r\n");
            sb.append(" * Created by @author ").append(AUTHOR_NAME).append("\r\n");
            sb.append(" *\r\n");
            sb.append(" * @date ").append(new Date()).append("\r\n");
            sb.append(" */ \r\n");
            //实体部分
            sb.append("\r\n\r\npublic class ").append(serviceImplName).append(" implements ").append(serviceName).append("{\r\n");
            for (String s : METHOD_PREFIX) {
                sb.append("\r\n\t@Override\r\n");
                if ("findById".equals(s)) {
                    sb.append("\tpublic ArrayList<").append(initCap(tableName,false)).append("> ").append(s).append("(").append(initCap(tableName,false)).append(" ").append(initCap(tableName)).append(") {\r\n");
                    sb.append("\t\t").append(daoName).append(" ").append(initCap(tableName)).append("DaoImpl = new ").append(daoImplName).append("();\r\n");
                    sb.append("\t\treturn ").append(initCap(tableName)).append("DaoImpl.").append(s).append("(").append(initCap(tableName)).append(");\r\n");;
                    sb.append("\t}\r\n");
                } else if ("findAll".equals(s)) {
                    sb.append("\tpublic ArrayList<").append(initCap(tableName,false)).append("> ").append(s).append("() {\r\n");
                    sb.append("\t\t").append(daoName).append(" ").append(initCap(tableName)).append("DaoImpl = new ").append(daoImplName).append("();\r\n");
                    sb.append("\t\treturn ").append(initCap(tableName)).append("DaoImpl.").append(s).append("();\r\n");
                    sb.append("\t}\r\n");
                } else {
                    sb.append("\tpublic boolean ").append(s).append("(").append(initCap(tableName,false)).append(" ").append(initCap(tableName)).append(") {\r\n");
                    sb.append("\t\t").append(daoName).append(" ").append(initCap(tableName)).append("DaoImpl = new ").append(daoImplName).append("();\r\n");
                    sb.append("\t\treturn ").append(initCap(tableName)).append("DaoImpl.").append(s).append("(").append(initCap(tableName)).append(");\r\n");
                    sb.append("\t}\r\n");
                }
                sb.append("\t\r\n");
            }
            sb.append("}\r\n");
            //***********************************************************************************
            pw.println(sb.toString());
            pw.flush();
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能生成 Servlet
     */
    private static void generateServlet() {
        File directory = new File(PACKAGE_OUT_PATH + servletPath);
        directory.mkdirs();//创建目录
        try {
            String outputPath = PACKAGE_OUT_PATH + servletPath + "\\" + servletName + ".java";
            FileWriter fw = new FileWriter(outputPath);
            PrintWriter pw = new PrintWriter(fw);
            //代码 ****************************************
            String sb = "package "+servletPath.replace("\\",".").substring(1)+";\r\n" +
                    "\r\n" +
                    "import javax.servlet.ServletException;" +
                    "\r\n" +
                    "import javax.servlet.annotation.WebServlet;" +
                    "\r\n" +
                    "import javax.servlet.http.HttpServlet;" +
                    "\r\n" +
                    "import javax.servlet.http.HttpServletRequest;" +
                    "\r\n" +
                    "import javax.servlet.http.HttpServletResponse;" +
                    "\r\n" +
                    "import java.io.IOException;" +
                    "\r\n" +
                    "import java.io.PrintWriter;" +
                    "\r\n" +
                    "\r\n" +
                    //注释部分
                    "\r\n/**\r\n" +
                    " * Created by @author " + AUTHOR_NAME + "\r\n" +
                    " *\r\n" +
                    " * @date " + new Date() + "\r\n" +
                    " */ \r\n" +
                    "@WebServlet(name = \"" + servletName + "\", urlPatterns = \"/" + servletName + "\")\r\n" +
                    //实体部分
                    "public class " + servletName + " extends HttpServlet {\r\n" +
                    "\t@Override\r\n" +
                    "\tprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r\n" +
                    "\t\tdoGet(request, response);\r\n" +
                    "\t}\r\n\r\n" +
                    "\t@Override\r\n" +
                    "\tprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r\n\r\n" +
                    "\t}\r\n" +
                    "}\r\n";
            pw.println(sb);
            pw.flush();
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建编码过滤器
     */
    private static void generateFilter() {
        File directory = new File(PACKAGE_OUT_PATH + filterPath);
        directory.mkdirs();//创建目录
        try {
            String outputPath = PACKAGE_OUT_PATH + filterPath + "\\EncodingFilter.java";
            FileWriter fw = new FileWriter(outputPath);
            PrintWriter pw = new PrintWriter(fw);
            //代码 ****************************************
            String sb = "package com.filter;\r\n" +
                    "import javax.servlet.*;\n" +
                    "import javax.servlet.annotation.WebFilter;\n" +
                    "import java.io.IOException;"+
                    "\r\n" +
                    "\r\n" +
                    //注释部分
                    "\r\n/**\r\n" +
                    " * Created by @author " + AUTHOR_NAME + "\r\n" +
                    " *\r\n" +
                    " * @date " + new Date() + "\r\n" +
                    " */ \r\n" +
                    "@WebFilter(filterName = \"EncodingFilter\", urlPatterns = \"/*\")" +
                    //实体部分
                    "public class EncodingFilter implements Filter {\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public void destroy() {\n" +
                    "    }\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {\n" +
                    "        // 设置请求编码\n" +
                    "        req.setCharacterEncoding(\"utf-8\");\n" +
                    "        // 设置请求响应编码\n" +
                    "        resp.setContentType(\"text/html;charset=utf-8\");\n" +
                    "        chain.doFilter(req, resp);\n" +
                    "    }\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public void init(FilterConfig config) throws ServletException {\n" +
                    "\n" +
                    "    }\n" +
                    "\n" +
                    "}";
            pw.println(sb);
            pw.flush();
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * mysql 下划线转驼峰
     * @param line 表明 :属性名
     * @param smallCamel true 小驼峰 : false 大驼峰
     */
    private static String initCap(String line, boolean ... smallCamel) {
        if (line == null || "".equals(line)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();

        Matcher matcher = PATTERN.matcher(line);
        //匹配正则表达式
        while (matcher.find()) {
            String word = matcher.group();
            //当是 true 或则是空的情况
            if((smallCamel.length == 0 || smallCamel[0] ) && matcher.start() == 0){
                sb.append(Character.toLowerCase(word.charAt(0)));
            }else{
                sb.append(Character.toUpperCase(word.charAt(0)));
            }
            int index = word.lastIndexOf('_');
            if (index > 0) {
                sb.append(word.substring(1, index).toLowerCase());
            } else {
                sb.append(word.substring(1).toLowerCase());
            }
        }
        return sb.toString();
    }

    /**
     * 功能:获得列的数据类型
     */
    private static String sqlType2JavaType(String sqlType) {
        if ("bit".equals(sqlType.toLowerCase())) {
            return "boolean";
        } else if ("tinyint".equals(sqlType.toLowerCase())) {
            return "byte";
        } else if ("smallint".equals(sqlType.toLowerCase())) {
            return "short";
        } else if ("int".equals(sqlType.toLowerCase())) {
            return "int";
        } else if ("bigint".equals(sqlType.toLowerCase())) {
            return "long";
        } else if ("float".equals(sqlType.toLowerCase())) {
            return "float";
        } else if ("decimal".equals(sqlType.toLowerCase()) || "numeric".equals(sqlType.toLowerCase()) || "real".equals(sqlType.toLowerCase()) || "money".equals(sqlType.toLowerCase()) || "smallmoney".equals(sqlType.toLowerCase())) {
            return "double";
        } else if ("varchar".equals(sqlType.toLowerCase()) || "char".equals(sqlType.toLowerCase()) || "nvarchar".equals(sqlType.toLowerCase()) || "nchar".equals(sqlType.toLowerCase()) || "text".equals(sqlType.toLowerCase())) {
            return "String";
        } else if ("datetime".equals(sqlType.toLowerCase()) || "date".equals(sqlType.toLowerCase())) {
            return "Date";
        } else if ("image".equals(sqlType.toLowerCase())) {
            return "Blob";
        } else if ("timestamp".equals(sqlType.toLowerCase())) {
            return "Timestamp";
        }
        return "String";
    }

}