Spring自动化创建脚本-解放繁琐的初始化配置!!!(自动化SSM整合)

一、实现功能(原创,转载请告知)

1.自动配置pom配置文件

2.自动识别数据库及数据表,创建Entity、Dao、Service、Controller等

3.自动创建database.properties、mybatis-config.xml等数据库文件

4.自动创建spring-dao.xml spring-mvc.xml spring-service.xml等配置文件

5.自动生成web.xml配置文件

总结:你只需要写前端sql语句controller控制逻辑 ,即可完成SSM整合项目!

二、代码

java 复制代码
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.util.*;

public class auto_main {
    public static void main(String[] args) {
        System.out.println("Designed by LJL");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入昵称:");
        String name = scanner.nextLine();
        if (name != null && !name.equals("")) {
            System.out.println("请输入数据库账号:");
            String sqlName = scanner.nextLine();
            System.out.println("请输入数据库密码:");
            String sqlPassword = scanner.nextLine();
            System.out.println("请输入项目数据库名称:");
            String sqlProjectName = scanner.nextLine();
            // 获取当前工作目录的路径
            String currentDir = System.getProperty("user.dir");
            // 构建上级目录的路径,并创建名为src的文件夹
            String java_Dir = currentDir + File.separator + "src/main/java/com/" + name;

            //TODO pom文件依赖覆盖
            String pomxml = currentDir + File.separator + "/pom.xml";
            String pomxmlcontent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<project xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" +
                    "         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                    "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
                    "    <modelVersion>4.0.0</modelVersion>\n" +
                    "\n" +
                    "    <groupId>com." + name + "</groupId>\n" +
                    "    <artifactId>book-management</artifactId>\n" +
                    "    <version>1.0-SNAPSHOT</version>\n" +
                    "    <packaging>war</packaging>\n" +
                    "\n" +
                    "    <properties>\n" +
                    "        <spring.version>5.3.9</spring.version>\n" +
                    "        <mybatis.version>3.5.7</mybatis.version>\n" +
                    "    </properties>\n" +
                    "\n" +
                    "    <dependencies>\n" +
                    "        <!-- Spring -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.springframework</groupId>\n" +
                    "            <artifactId>spring-webmvc</artifactId>\n" +
                    "            <version>5.3.20</version>\n" +
                    "        </dependency>\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.springframework</groupId>\n" +
                    "            <artifactId>spring-jdbc</artifactId>\n" +
                    "            <version>5.3.9</version>\n" +
                    "        </dependency>\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.springframework</groupId>\n" +
                    "            <artifactId>spring-context</artifactId>\n" +
                    "            <version>5.3.10</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!-- MyBatis -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.mybatis</groupId>\n" +
                    "            <artifactId>mybatis</artifactId>\n" +
                    "            <version>3.5.11</version>\n" +
                    "        </dependency>\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.mybatis</groupId>\n" +
                    "            <artifactId>mybatis-spring</artifactId>\n" +
                    "            <version>2.0.6</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!-- MySQL -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>mysql</groupId>\n" +
                    "            <artifactId>mysql-connector-java</artifactId>\n" +
                    "            <version>8.0.32</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!-- 数据库连接池 -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>com.mchange</groupId>\n" +
                    "            <artifactId>c3p0</artifactId>\n" +
                    "            <version>0.9.5.5</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!-- Lombok -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.projectlombok</groupId>\n" +
                    "            <artifactId>lombok</artifactId>\n" +
                    "            <version>1.18.36</version>\n" +
                    "            <scope>provided</scope>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!-- Servlet & JSP -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>javax.servlet</groupId>\n" +
                    "            <artifactId>javax.servlet-api</artifactId>\n" +
                    "            <version>4.0.1</version>\n" +
                    "            <scope>provided</scope>\n" +
                    "        </dependency>\n" +
                    "        <dependency>\n" +
                    "            <groupId>javax.servlet.jsp</groupId>\n" +
                    "            <artifactId>jsp-api</artifactId>\n" +
                    "            <version>2.2</version>\n" +
                    "            <scope>provided</scope>\n" +
                    "        </dependency>\n" +
                    "        <dependency>\n" +
                    "            <groupId>javax.servlet</groupId>\n" +
                    "            <artifactId>jstl</artifactId>\n" +
                    "            <version>1.2</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!-- JUnit -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>org.junit.jupiter</groupId>\n" +
                    "            <artifactId>junit-jupiter</artifactId>\n" +
                    "            <version>RELEASE</version>\n" +
                    "            <scope>compile</scope>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!-- Jackson -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>com.fasterxml.jackson.core</groupId>\n" +
                    "            <artifactId>jackson-core</artifactId>\n" +
                    "            <version>2.15.4</version>\n" +
                    "        </dependency>\n" +
                    "        <dependency>\n" +
                    "            <groupId>com.fasterxml.jackson.core</groupId>\n" +
                    "            <artifactId>jackson-databind</artifactId>\n" +
                    "            <version>2.15.4</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!-- 文件上传库 -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>commons-fileupload</groupId>\n" +
                    "            <artifactId>commons-fileupload</artifactId>\n" +
                    "            <version>1.4</version>\n" +
                    "        </dependency>\n" +
                    "\n" +
                    "        <!-- Minio -->\n" +
                    "        <dependency>\n" +
                    "            <groupId>io.minio</groupId>\n" +
                    "            <artifactId>minio</artifactId>\n" +
                    "            <version>8.5.4</version>\n" +
                    "        </dependency>\n" +
                    "    </dependencies>\n" +
                    "\n" +
                    "    <build>\n" +
                    "        <plugins>\n" +
                    "            <plugin>\n" +
                    "                <groupId>org.apache.maven.plugins</groupId>\n" +
                    "                <artifactId>maven-compiler-plugin</artifactId>\n" +
                    "                <version>3.8.1</version>\n" +
                    "                <configuration>\n" +
                    "                    <source>1.8</source>\n" +
                    "                    <target>1.8</target>\n" +
                    "                </configuration>\n" +
                    "            </plugin>\n" +
                    "        </plugins>\n" +
                    "    </build>\n" +
                    "</project>";
            updateFile(pomxml, pomxmlcontent);
            System.out.println("等待用户进行pom依赖更新!\n完成后请输入:1");
            int user_input = scanner.nextInt();
            if (user_input == 1) {
                int sql_status = connectMysql(sqlName, sqlPassword, sqlProjectName);
                if (sql_status == 1) {
                    //TODO Entity目录
                    String Entity_Dir = java_Dir + File.separator + "Entity";
                    //TODO Dao目录
                    String Dao_Dir = java_Dir + File.separator + "Dao";
                    //TODO Service及Impl目录
                    String Service_Dir = java_Dir + File.separator + "Service";
                    String ServiceImpl_Dir = Service_Dir + File.separator + "Impl";
                    //TODO Controller目录
                    String Controller_Dir = java_Dir + File.separator + "Controller";
                    //TODO resources/spring目录,存放配置文件
                    String resources_Dir = currentDir + File.separator + "src" + File.separator + "main" + File.separator + "resources";
                    String spring_Dir = resources_Dir + File.separator + "spring";
                    //TODO webapp/WEB-INF/pages目录
                    String pages_Dir = currentDir + File.separator + "src/main/webapp/WEB-INF/pages/";


                    createDir(spring_Dir);
                    createDir(Entity_Dir);
                    createDir(Dao_Dir);
                    createDir(Service_Dir);
                    createDir(ServiceImpl_Dir);
                    createDir(Controller_Dir);
                    createDir(pages_Dir);

                    String springdaoxml = spring_Dir + "/spring-dao.xml";
                    String springmvcxml = spring_Dir + "/spring-mvc.xml";
                    String springservicexml = spring_Dir + "/spring-service.xml";
                    String databaseproperties = resources_Dir + "/database.properties";
                    String mybatisconfigxml = resources_Dir + "/mybatis-config.xml";
                    String webxml = currentDir + File.separator + "src/main/webapp/WEB-INF/web.xml";
                    String indexjsp = currentDir + File.separator + "src/main/webapp/index.jsp";


                    String springdaocontent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                            "<beans xmlns=\"http://www.springframework.org/schema/beans\"\n" +
                            "       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                            "       xmlns:context=\"http://www.springframework.org/schema/context\"\n" +
                            "       xmlns:tx=\"http://www.springframework.org/schema/tx\"\n" +
                            "       xsi:schemaLocation=\"http://www.springframework.org/schema/beans\n" +
                            "        http://www.springframework.org/schema/beans/spring-beans.xsd\n" +
                            "        http://www.springframework.org/schema/context\n" +
                            "        http://www.springframework.org/schema/context/spring-context.xsd\n" +
                            "        http://www.springframework.org/schema/tx\n" +
                            "        http://www.springframework.org/schema/tx/spring-tx.xsd\">\n" +
                            "\n" +
                            "    <!-- 配置整合mybatis -->\n" +
                            "    <!-- 1.关联数据库文件 -->\n" +
                            "    <context:property-placeholder location=\"classpath:database.properties\"/>\n" +
                            "\n" +
                            "    <!-- 2.数据库连接池 -->\n" +
                            "    <bean id=\"dataSource\" class=\"com.mchange.v2.c3p0.ComboPooledDataSource\">\n" +
                            "        <!-- 配置连接池属性 -->\n" +
                            "        <property name=\"driverClass\" value=\"${jdbc.driver}\"/>\n" +
                            "        <property name=\"jdbcUrl\" value=\"${jdbc.url}\"/>\n" +
                            "        <property name=\"user\" value=\"${jdbc.username}\"/>\n" +
                            "        <property name=\"password\" value=\"${jdbc.password}\"/>\n" +
                            "\n" +
                            "        <!-- 基本连接池配置 -->\n" +
                            "        <property name=\"initialPoolSize\" value=\"5\"/>\n" +
                            "        <property name=\"maxPoolSize\" value=\"30\"/>\n" +
                            "        <property name=\"minPoolSize\" value=\"10\"/>\n" +
                            "    </bean>\n" +
                            "\n" +
                            "    <!-- 3.配置SqlSessionFactory对象 -->\n" +
                            "    <bean id=\"sqlSessionFactory\" class=\"org.mybatis.spring.SqlSessionFactoryBean\">\n" +
                            "        <!-- 注入数据库连接池 -->\n" +
                            "        <property name=\"dataSource\" ref=\"dataSource\"/>\n" +
                            "        <!-- 配置MyBatis全局配置文件:mybatis-config.xml -->\n" +
                            "        <property name=\"configLocation\" value=\"classpath:mybatis-config.xml\"/>\n" +
                            "        <!-- 扫描entity包 使用别名 -->\n" +
                            "        <property name=\"typeAliasesPackage\" value=\"com." + name + ".Entity\"/>\n" +
                            "    </bean>\n" +
                            "\n" +
                            "    <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->\n" +
                            "    <bean class=\"org.mybatis.spring.mapper.MapperScannerConfigurer\">\n" +
                            "        <!-- 注入sqlSessionFactory -->\n" +
                            "        <property name=\"sqlSessionFactoryBeanName\" value=\"sqlSessionFactory\"/>\n" +
                            "        <!-- 给出需要扫描Dao接口包 -->\n" +
                            "        <property name=\"basePackage\" value=\"com." + name + ".Dao\"/>\n" +
                            "    </bean>\n" +
                            "\n" +
                            "    <!-- 5.配置事务管理器 -->\n" +
                            "    <bean id=\"transactionManager\" class=\"org.springframework.jdbc.datasource.DataSourceTransactionManager\">\n" +
                            "        <!-- 注入数据库连接池 -->\n" +
                            "        <property name=\"dataSource\" ref=\"dataSource\"/>\n" +
                            "    </bean>\n" +
                            "\n" +
                            "    <!-- 6.开启事务注解 -->\n" +
                            "    <tx:annotation-driven transaction-manager=\"transactionManager\"/>\n" +
                            "</beans> ";

                    String springmvccontent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                            "<beans xmlns=\"http://www.springframework.org/schema/beans\"\n" +
                            "       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                            "       xmlns:context=\"http://www.springframework.org/schema/context\"\n" +
                            "       xmlns:mvc=\"http://www.springframework.org/schema/mvc\"\n" +
                            "       xsi:schemaLocation=\"http://www.springframework.org/schema/beans\n" +
                            "        http://www.springframework.org/schema/beans/spring-beans.xsd\n" +
                            "        http://www.springframework.org/schema/context\n" +
                            "        http://www.springframework.org/schema/context/spring-context.xsd\n" +
                            "        http://www.springframework.org/schema/mvc\n" +
                            "        http://www.springframework.org/schema/mvc/spring-mvc.xsd\">\n" +
                            "\n" +
                            "    <!-- 1.开启SpringMVC注解驱动 -->\n" +
                            "    <mvc:annotation-driven/>\n" +
                            "    \n" +
                            "    <!-- 2.静态资源默认配置 -->\n" +
                            "    <mvc:default-servlet-handler/>\n" +
                            "\n" +
                            "    <!-- 3.配置jsp 显示ViewResolver视图解析器 -->\n" +
                            "    <bean class=\"org.springframework.web.servlet.view.InternalResourceViewResolver\">\n" +
                            "        <property name=\"viewClass\" value=\"org.springframework.web.servlet.view.JstlView\"/>\n" +
                            "        <property name=\"prefix\" value=\"/WEB-INF/pages/\"/>\n" +
                            "        <property name=\"suffix\" value=\".jsp\"/>\n" +
                            "    </bean>\n" +
                            "\n" +
                            "    <!-- 4.扫描web相关的bean -->\n" +
                            "    <context:component-scan base-package=\"com." + name + ".Controller\"/>\n" +
                            "<bean id=\"multipartResolver\" class=\"org.springframework.web.multipart.commons.CommonsMultipartResolver\">\n" +
                            "        <property name=\"defaultEncoding\" value=\"UTF-8\"/>\n" +
                            "        <property name=\"maxUploadSize\" value=\"2097152\"/>\n" +
                            "    </bean>\n" +
                            "</beans> \n";
                    String springservicecontent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                            "<beans xmlns=\"http://www.springframework.org/schema/beans\"\n" +
                            "       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                            "       xmlns:context=\"http://www.springframework.org/schema/context\"\n" +
                            "       xsi:schemaLocation=\"http://www.springframework.org/schema/beans\n" +
                            "        http://www.springframework.org/schema/beans/spring-beans.xsd\n" +
                            "        http://www.springframework.org/schema/context\n" +
                            "        http://www.springframework.org/schema/context/spring-context.xsd\">\n" +
                            "\n" +
                            "    <!-- 扫描service相关的bean -->\n" +
                            "    <context:component-scan base-package=\"com." + name + ".Service\" />\n" +
                            "</beans> ";
                    String databasepropertiescontent = "jdbc.driver=com.mysql.cj.jdbc.Driver\n" +
                            "jdbc.url=jdbc:mysql://localhost:3306/" + sqlProjectName + "?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf8\n" +
                            "jdbc.username=" + sqlName +"\n" +
                            "jdbc.password=" + sqlPassword;
                    String mybatisconfigxmlcontent = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
                            "<!DOCTYPE configuration\n" +
                            "        PUBLIC \"-//mybatis.org//DTD Config 3.0//EN\"\n" +
                            "        \"http://mybatis.org/dtd/mybatis-3-config.dtd\">\n" +
                            "<configuration>\n" +
                            "    <!-- 配置全局属性 -->\n" +
                            "    <settings>\n" +
                            "        <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->\n" +
                            "        <setting name=\"useGeneratedKeys\" value=\"true\" />\n" +
                            "        <!-- 使用列别名替换列名 默认:true -->\n" +
                            "        <setting name=\"useColumnLabel\" value=\"true\" />\n" +
                            "        <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->\n" +
                            "        <setting name=\"mapUnderscoreToCamelCase\" value=\"true\" />\n" +
                            "    </settings>\n" +
                            "</configuration> ";
                    String webxmlcontent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                            "<web-app xmlns=\"http://xmlns.jcp.org/xml/ns/javaee\"\n" +
                            "         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                            "         xsi:schemaLocation=\"http://xmlns.jcp.org/xml/ns/javaee\n" +
                            "                             http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd\"\n" +
                            "         version=\"4.0\">\n" +
                            "\n" +
                            "    <!--允许访问jpg。 并且必须加在springmvc的servlet之前-->\n" +
                            "    <servlet-mapping>\n" +
                            "        <servlet-name>default</servlet-name>\n" +
                            "        <url-pattern>*.jpg</url-pattern>\n" +
                            "    </servlet-mapping>\n" +
                            "    <servlet-mapping>\n" +
                            "        <servlet-name>default</servlet-name>\n" +
                            "        <url-pattern>*.docx</url-pattern>\n" +
                            "    </servlet-mapping>\n" +
                            "    <servlet-mapping>\n" +
                            "        <servlet-name>default</servlet-name>\n" +
                            "        <url-pattern>*.pdf</url-pattern>\n" +
                            "    </servlet-mapping>\n" +
                            "\n" +
                            "    <!-- 配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件 -->\n" +
                            "    <listener>\n" +
                            "        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>\n" +
                            "    </listener>\n" +
                            "\n" +
                            "    <!-- 配置Spring配置文件的位置 -->\n" +
                            "    <context-param>\n" +
                            "        <param-name>contextConfigLocation</param-name>\n" +
                            "        <param-value>\n" +
                            "            classpath:spring/spring-dao.xml\n" +
                            "            classpath:spring/spring-service.xml\n" +
                            "        </param-value>\n" +
                            "    </context-param>\n" +
                            "\n" +
                            "    <!-- 配置DispatcherServlet -->\n" +
                            "    <servlet>\n" +
                            "        <servlet-name>DispatcherServlet</servlet-name>\n" +
                            "        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>\n" +
                            "        <init-param>\n" +
                            "            <param-name>contextConfigLocation</param-name>\n" +
                            "            <param-value>classpath:spring/spring-mvc.xml</param-value>\n" +
                            "        </init-param>\n" +
                            "        <load-on-startup>1</load-on-startup>\n" +
                            "    </servlet>\n" +
                            "    <servlet-mapping>\n" +
                            "        <servlet-name>DispatcherServlet</servlet-name>\n" +
                            "        <url-pattern>/</url-pattern>\n" +
                            "    </servlet-mapping>\n" +
                            "\n" +
                            "    <!-- 编码过滤器 -->\n" +
                            "    <filter>\n" +
                            "        <filter-name>encodingFilter</filter-name>\n" +
                            "        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>\n" +
                            "        <init-param>\n" +
                            "            <param-name>encoding</param-name>\n" +
                            "            <param-value>UTF-8</param-value>\n" +
                            "        </init-param>\n" +
                            "    </filter>\n" +
                            "    <filter-mapping>\n" +
                            "        <filter-name>encodingFilter</filter-name>\n" +
                            "        <url-pattern>/*</url-pattern>\n" +
                            "    </filter-mapping>\n" +
                            "</web-app>\n";
                    String indexjspcontent = "<%@ page contentType=\"text/html;charset=UTF-8\" language=\"java\" %>\n" +
                            "<% response.sendRedirect(request.getContextPath() + \"/\"); %>";
                    createXml(springdaoxml, springdaocontent);
                    createXml(springmvcxml, springmvccontent);
                    createXml(springservicexml, springservicecontent);
                    createXml(databaseproperties, databasepropertiescontent);
                    createXml(mybatisconfigxml, mybatisconfigxmlcontent);
                    updateFile(webxml, webxmlcontent);
                    updateFile(indexjsp, indexjspcontent);

                    //TODO 根据数据库创建Entity实体类、Service、Impl
                    List<Map<String, Object>> tablesAndColumns = getMySQL(sqlName, sqlPassword, sqlProjectName);
                    for (Map<String, Object> tableInfo : tablesAndColumns) {
                        //TODO Entity
                        String Entityjava = Entity_Dir + "/" + tableInfo.get("TABLE_NAME") + ".java";
                        String Entitycontent = "package com." + name + ".Entity;\n" +
                                "\n" +
                                "import lombok.Data;\n" +
                                "import java.math.BigDecimal;\n" +
                                "@Data\n" +
                                "public class " + tableInfo.get("TABLE_NAME") + "{\n" +
                                "\n";
                        //TODO Service
                        String Service = Service_Dir + "/" + tableInfo.get("TABLE_NAME") + "Service.java";
                        String Servicecontent = "package com." + name + ".Service;\n" +
                                "\n" +
                                "import com." + name + ".Entity." + tableInfo.get("TABLE_NAME") + ";\n" +
                                "import java.util.List;\n" +
                                "\n" +
                                "public interface " + tableInfo.get("TABLE_NAME") + "Service {\n" +
                                "} ";
                        //TODO ServiceImpl
                        String ServiceImpl = Service_Dir + "/Impl/" + tableInfo.get("TABLE_NAME") + "ServiceImpl.java";
                        String ServiceImplcontent = "package com." + name + ".Service.Impl;\n" +
                                "\n" +
                                "import com." + name + ".Dao." + tableInfo.get("TABLE_NAME") + "Mapper;\n" +
                                "import com." + name + ".Entity." + tableInfo.get("TABLE_NAME") + ";\n" +
                                "import com." + name + ".Service." + tableInfo.get("TABLE_NAME") + "Service;\n" +
                                "import org.springframework.beans.factory.annotation.Autowired;\n" +
                                "import org.springframework.stereotype.Service;\n" +
                                "\n" +
                                "import java.util.List;\n" +
                                "\n" +
                                "@Service\n" +
                                "public class " + tableInfo.get("TABLE_NAME") + "ServiceImpl implements " + tableInfo.get("TABLE_NAME") + "Service {\n" +
                                "} ";
                        //TODO Dao
                        String Dao = Dao_Dir + "/" + tableInfo.get("TABLE_NAME") + "Mapper.java";
                        String Daocontent = "package com." + name + ".Dao;\n" +
                                "\n" +
                                "import com." + name + ".Entity." + tableInfo.get("TABLE_NAME") + ";\n" +
                                "import org.apache.ibatis.annotations.*;\n" +
                                "\n" +
                                "import java.util.List;\n" +
                                "\n" +
                                "public interface " + tableInfo.get("TABLE_NAME") + "Mapper{\n" +
                                "} ";
                        System.out.println("Table Name: " + tableInfo.get("TABLE_NAME"));
                        List<Map<String, String>> columnsInfo = (List<Map<String, String>>) tableInfo.get("COLUMNS");
                        int id_count = 0;
                        for (Map<String, String> columnInfo : columnsInfo) {
                            if (columnInfo.get("COLUMN_NAME").equals("id")) {
                                id_count = id_count + 1;
                            }
                            if (id_count > 1) {
                                continue;
                            } else {
                                Entitycontent = Entitycontent + "   " + "private " + columnInfo.get("TYPE") + " " + columnInfo.get("COLUMN_NAME") + ";\n";
                                System.out.println("  Column Name: " + columnInfo.get("COLUMN_NAME") + ", Type: " + columnInfo.get("TYPE"));
                            }
                        }
                        Entitycontent = Entitycontent + "}";
                        createXml(Entityjava, Entitycontent);
                        createXml(Service, Servicecontent);
                        createXml(ServiceImpl, ServiceImplcontent);
                        createXml(Dao, Daocontent);
                    }
                }else {
                    System.out.println("数据库链接失败Q");
                }
            }else {
                System.out.println("用户输入有误!");
            }
        } else {
            System.out.println("输入错误!");
        }
    }

    private static int connectMysql(String username, String password, String projectName) {
        // 数据库URL,用户名和密码
        String url = "jdbc:mysql://localhost:3306/" + projectName + "?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf8";

        // 加载JDBC驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
            return 0;
        }

        // 建立连接
        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            return 1;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static List<Map<String, Object>> getMySQL(String username, String password, String projectName) {
        List<Map<String, Object>> tableInfoList = new ArrayList<>();
        // 数据库URL,用户名和密码
        String url = "jdbc:mysql://localhost:3306/" + projectName + "?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf8";
        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            DatabaseMetaData metaData = connection.getMetaData();

            // 获取所有表
            ResultSet tables = metaData.getTables(projectName, null, "%", new String[]{"TABLE"});
            while (tables.next()) {
                String tableName = tables.getString("TABLE_NAME");
                Map<String, Object> tableInfo = new HashMap<>();
                tableInfo.put("TABLE_NAME", tableName);

                // 获取表的字段和类型
                ResultSet columns = metaData.getColumns(null, null, tableName, null);
                List<Map<String, String>> columnsInfo = new ArrayList<>();
                while (columns.next()) {
                    Map<String, String> columnInfo = new HashMap<>();
                    String columnName = columns.getString("COLUMN_NAME");
                    String columnType = columns.getString("TYPE_NAME");
                    String javaType = getJavaType(columnType);
                    columnInfo.put("COLUMN_NAME", columnName);
                    columnInfo.put("TYPE", javaType); // 使用Java类型名称
                    columnsInfo.add(columnInfo);
                }
                columns.close();
                tableInfo.put("COLUMNS", columnsInfo); // 直接存储列表

                tableInfoList.add(tableInfo);
            }
            tables.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tableInfoList;
    }

    private static String getJavaType(String mysqlType) {
        switch (mysqlType.toUpperCase()) {
            case "INT":
            case "TINYINT":
            case "SMALLINT":
            case "MEDIUMINT":
            case "BIGINT":
                return "int";
            case "VARCHAR":
            case "CHAR":
            case "TEXT":
            case "TINYTEXT":
            case "MEDIUMTEXT":
            case "LONGTEXT":
                return "String";
            case "DECIMAL":
            case "DOUBLE":
                return "Double";
            // Add more cases for other types as needed
            default:
                return mysqlType; // Return the original type if not recognized
        }
    }

    private static void createDir(String dirPath) {
        File dir = new File(dirPath);
        if (dir.mkdirs()) {
            System.out.println("目录 " + dirPath + " 创建成功!");
        } else {
            System.out.println("目录 " + dirPath + " 创建失败!");
        }
    }

    private static void createXml(String fileName, String fileContent){
        try (FileWriter writer = new FileWriter(fileName)) {
            writer.write(fileContent);
            System.out.println("XML文件创建成功:" + fileName);
        } catch (IOException e) {
            System.out.println("创建XML文件时出错:" + e.getMessage());
        }
    }

    private static void updateFile(String fileName, String fileContent){
        try (FileWriter writer = new FileWriter(fileName, false)) { // false表示不追加,清空文件
            writer.write(fileContent);
            System.out.println("文件内容已更新。");
        } catch (IOException e) {
            System.out.println("处理文件时出错:" + e.getMessage());
        }
    }
}

三、食用指南

1.在spring项目中创建java文件

2.把上面代码放在项目中任意位置

3.运行代码

4.按照提示输入昵称、数据库账号、密码、数据表名称

5.手动更新pom文件

6.重启代码

7.恭喜,成功解放双手!!!!

相关推荐
计算机毕设定制辅导-无忧学长3 小时前
Redis 在小型项目中的实战运用
数据库·redis·mybatis
小冯的编程学习之路3 小时前
【Redis】:初识Redis
数据库·redis·缓存
Maiko Star4 小时前
MySQL管理
数据库·mysql·oracle
?333334 小时前
vulnhub靶场-jangow-01-1.0.1(截止至获取shell)
linux·数据库·mysql·安全·网络安全
m0_748245744 小时前
MySQL root用户密码忘记怎么办(Reset root account password)
数据库·mysql·adb
不会玩技术的技术girl5 小时前
AI 自动化编程的现状与局限
运维·人工智能·自动化
天空之外1366 小时前
Spring Boot Actuator、Spring Boot Actuator使用、Spring Boot Actuator 监控、Spring程序监控
java·spring boot·spring
MySheep.6 小时前
数据库在大数据领域的探索与实践:动态存储与查询优化
大数据·数据库
瓜牛_gn7 小时前
苍穹外卖day07缓存部分分析
java·spring
m0_748244837 小时前
【MySQL】表的约束
数据库·mysql·性能优化