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

news2025/1/5 8:30:30

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

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整合项目

二、代码

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 = 0;
                for (int i = 0; i < 2; i++) {
                    sql_status = connectMysql(sqlName, sqlPassword, sqlProjectName);
                    if (sql_status == 1) {
                        continue;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                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) {
                        String tableName = (String) tableInfo.get("TABLE_NAME");
                        //TODO Entity
                        String Entityjava = Entity_Dir + "/" + tableName + ".java";
                        String Entitycontent = "package com." + name + ".Entity;\n" +
                                "\n" +
                                "import lombok.Data;\n" +
                                "import java.math.BigDecimal;\n" +
                                "@Data\n" +
                                "public class " + tableName + "{\n" +
                                "\n";
                        //TODO Service
                        String Service = Service_Dir + "/" + tableName + "Service.java";
                        String Servicecontent = "package com." + name + ".Service;\n" +
                                "\n" +
                                "import com." + name + ".Entity." + tableName + ";\n" +
                                "import java.util.List;\n" +
                                "\n" +
                                "public interface " + tableName + "Service {\n" +
                                "} ";
                        //TODO ServiceImpl
                        String ServiceImpl = Service_Dir + "/Impl/" + tableName + "ServiceImpl.java";
                        String ServiceImplcontent = "package com." + name + ".Service.Impl;\n" +
                                "\n" +
                                "import com." + name + ".Dao." + tableName + "Mapper;\n" +
                                "import com." + name + ".Entity." + tableName + ";\n" +
                                "import com." + name + ".Service." + tableName + "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 " + tableName + "ServiceImpl implements " + tableName + "Service {\n" +
                                "} ";
                        //TODO Dao
                        String Dao = Dao_Dir + "/" + tableName + "Mapper.java";
                        String Daocontent = "package com." + name + ".Dao;\n" +
                                "\n" +
                                "import com." + name + ".Entity." + tableName + ";\n" +
                                "import org.apache.ibatis.annotations.*;\n" +
                                "\n" +
                                "import java.util.List;\n" +
                                "\n" +
                                "public interface " + tableName + "Mapper{\n" +
                                "} ";
                        //TODO Controller
                        String Controller = Controller_Dir + "/" + tableName + "Controller.java";
                        String Controllercontent = "package com." + name + ".Controller;\n" +
                                "\n" +
                                "\n" +
                                "import org.springframework.beans.factory.annotation.Autowired;\n" +
                                "import org.springframework.stereotype.Controller;\n" +
                                "import org.springframework.web.bind.annotation.GetMapping;\n" +
                                "import org.springframework.web.bind.annotation.PostMapping;\n" +
                                "import org.springframework.web.bind.annotation.RequestMapping;\n" +
                                "import org.springframework.web.bind.annotation.ResponseBody;\n" +
                                "\n" +
                                "@Controller\n" +
                                "public class " + tableName + "Controller {\n" +
                                "}";
                        System.out.println("Table Name: " + tableName);
                        List<Map<String, String>> columnsInfo = (List<Map<String, String>>) tableInfo.get("COLUMNS");
                        int id_count = 0;
                        for (Map<String, String> columnInfo : columnsInfo) {
                            String columnName = columnInfo.get("COLUMN_NAME");
                            String columnType = columnInfo.get("TYPE");
                            if (columnName.equals("id")) {
                                id_count = id_count + 1;
                            }
                            if (id_count > 1) {
                                continue;
                            } else {
                                Entitycontent = Entitycontent + "   " + "private " + columnType + " " + columnName + ";\n";
                                System.out.println("  Column Name: " + columnName + ", Type: " + columnType);
                            }
                        }
                        Entitycontent = Entitycontent + "}";
                        createXml(Entityjava, Entitycontent);
                        createXml(Service, Servicecontent);
                        createXml(ServiceImpl, ServiceImplcontent);
                        createXml(Dao, Daocontent);
                        createXml(Controller, Controllercontent);
                    }
                }else {
                    System.out.println("\n请再次运行程序,以开始创建必要插件!\n");
                }
            }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.恭喜,成功解放双手!!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2269741.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Unity3D仿星露谷物语开发12之创建道具列表

1、目标 道具是游戏的核心部分&#xff0c;道具包括你可以拾取的东西&#xff0c;你可以使用的工具和你能种的东西等。 本节就是创建道具的信息类。同时了解ScriptableObject类的使用。 2、创建道具枚举类 修改Assets -> Scripts -> Enums.cs脚本&#xff0c; 新增如…

华为配置 之 RIP

简介&#xff1a; RIP&#xff08;路由信息协议&#xff09;是一种广泛使用的内部网关协议&#xff0c;基于距离向量算法来决定路径。它通过向全网广播路由控制信息来动态交换网络拓扑信息&#xff0c;从而计算出最佳路由路径。RIP易于配置和理解&#xff0c;非常适用于小型网络…

使用new String(“yupi”)语句在Java中会创建多少个对象?

在 Java 编程中&#xff0c;字符串的处理是一个常见且重要的部分。理解字符串对象的创建和内存管理对于编写高效和优化的代码至关重要。当我们在 Java 中使用 new String("yupi") 语句时&#xff0c;实际上会涉及到多个对象的创建。本文将详细解释这一过程&#xff0…

vue使用el-select下拉框自定义复选框

在 Vue 开发中&#xff0c;高效且美观的组件能极大地提升用户体验和开发效率。在vue中使用elementplus 的 el-select下拉框实现了一个自定义的多选下拉框组件。 一、代码功能概述 这段代码创建了一个可多选的下拉框组件&#xff0c;通过el-select和el-checkbox-group结合的方…

Python基于EasyOCR进行路灯控制箱图像文本识别项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档视频讲解&#xff09;&#xff0c;如需数据代码文档视频讲解可以直接到文章最后关注获取。 1.项目背景 随着城市化进程的加快&#xff0c;智能城市建设成为了现代社会发展的重要方向。路灯作为城市基础设…

TDengine 新功能 VARBINARY 数据类型

1. 背景 VARBINARY 数据类型用于存储二进制数据&#xff0c;与 MySQL 中的 VARBINARY 数据类型功能相同&#xff0c;VARBINARY 数据类型长度可变&#xff0c;在创建表时指定最大字节长度&#xff0c;使用进按需分配存储&#xff0c;但不能超过建表时指定的最大值。 2. 功能说明…

使用位操作符实现加减乘除!

欢迎拜访&#xff1a;雾里看山-CSDN博客 本篇主题&#xff1a;使用位操作符实现加减乘除 发布时间&#xff1a;2025.1.1 隶属专栏&#xff1a;C语言 目录 位操作实现加法运算&#xff08;&#xff09;原理代码示例 位操作实现减法运算&#xff08;-&#xff09;原理代码示例 位…

基于SpringBoot的题库管理系统的设计与实现(源码+SQL+LW+部署讲解)

文章目录 摘 要1. 第1章 选题背景及研究意义1.1 选题背景1.2 研究意义1.3 论文结构安排 2. 第2章 相关开发技术2.1 前端技术2.2 后端技术2.3 数据库技术 3. 第3章 可行性及需求分析3.1 可行性分析3.2 系统需求分析 4. 第4章 系统概要设计4.1 系统功能模块设计4.2 数据库设计 5.…

MATLAB条件判断(switch-case-otherwise-end型)

在条件判断时&#xff0c;遇到很多个条件&#xff0c;如果再用 i f − e l s e if-else if−else语句就显得很繁琐&#xff0c;所以我们可以用 s w i t c h switch switch来解决 结构&#xff1a; 判断对象可以为数字&#xff0c;也可以为字符 如图&#xff1a; 注意&#x…

windows文件夹自定义右键调用powershell完成7zip加密打包

准备powershell脚本 2. regedit的路径是&#xff1a;计算机\HKEY_CLASSES_ROOT\Directory\shell\&#xff0c;在此项目下新增子项目diy_command\command&#xff0c;command的数据值为powershell D:\windowsProjects\directory_diy.ps1 %1 效果&#xff0c;点击后进入和power…

从0入门自主空中机器人-2-1【无人机硬件框架】

关于本课程&#xff1a; 本次课程是一套面向对自主空中机器人感兴趣的学生、爱好者、相关从业人员的免费课程&#xff0c;包含了从硬件组装、机载电脑环境设置、代码部署、实机实验等全套详细流程&#xff0c;带你从0开始&#xff0c;组装属于自己的自主无人机&#xff0c;并让…

C语言:位段

位段的内存分配: 1. 位段的成员可以是 int unsigned int signed int 或者是char &#xff08;属于整形家族&#xff09;类型 2. 位段的空间上是按照需要以4个字节&#xff08; 类型 int &#xff09;或者1个字节&#xff08; char &#xff09;的方式来开辟的。 3. 位段涉及…

【OceanBase】利用 OceanBase 向量检索能力构建文档智能问答小助手

文章目录 一、实验环境说明二、前期准备工作2.1 安装 Python 3.9 和 pip2.2 安装 Poetry2.3 安装并启动Docker(可选)2.4 安装 MySQL 客户端2.5 注册阿里云百炼账号并开通服务获取 API Key 三、构建智能问答小助手3.1 部署 OceanBase 集群3.1.1 方式一&#xff1a;使用 OBCloud …

http报头解析

http报文 http报文主要有两类是常见的&#xff0c;第一类是请求报文&#xff0c;第二类是响应报文&#xff0c;每个报头除了第一行&#xff0c;都是采用键值对进行传输数据&#xff0c;请求报文的第一行主要包括http方法&#xff08;GET&#xff0c;PUT&#xff0c; POST&#…

Lucene 漏洞历险记:修复损坏的索引异常

作者&#xff1a;来自 Elastic Benjamin Trent 有时&#xff0c;一行代码需要几天的时间才能写完。在这里&#xff0c;我们可以看到工程师在多日内调试代码以修复潜在的 Apache Lucene 索引损坏的痛苦。 做好准备 这篇博客与往常不同。它不是对新功能或教程的解释。这是关于花…

如何提升可视化大屏的用户体验?

一、什么是可视化大屏的用户体验 可视化大屏的用户体验是指用户在使用大屏幕可视化系统时所感受到的整体体验。这包括系统的易用性、交互性、视觉效果、信息展示方式等方面。一个好的可视化大屏用户体验应该能够让用户轻松地获取所需的信息&#xff0c;快速理解数据&#xff0…

overscroll-behavior-解决H5在ios上过度滚动的默认行为

1. 问题 开发H5的过程中&#xff0c;经常会有android和ios两边系统需要兼容的情况。在ios上一直有个问题是当H5内容触及到页面顶部或底部时&#xff0c;还是可以被人为的往下或往下拉动界面。当然可能有的情况是比较适用的&#xff0c;比如你往下拉动&#xff0c;然后在导航栏…

【无线传感网】无线传感器网络拓扑控制技术

文章目录 拓扑控制的意义影响整个网络的生存时间减小节点间通信干扰&#xff0c;提高网络通信效率为路由协议、时间同步提供基础影响数据融合弥补节点失效的影响 拓扑控制的设计目标能量消耗覆盖度连通性算法的分布式程度网络延迟&#x1f6a9;干扰和竞争对称性鲁棒性和可扩展性…

使用pandas把数据库中的数据转成csv文件

使用pandas把数据库中的数据转成csv文件 1、效果图 2、流程 1、连接数据库,获取数据 2、把一些中文字符转成gbk,忽略掉无法转化的 3、把数据转成csv 3、代码 import pymysql import pandas as pddef get_database(databasename):

点击锁定按钮,锁定按钮要变成解锁按钮,然后状态要从待绑定变成 已锁定(升级版)

文章目录 1、updateInviteCodeStatus2、handleLock3、InviteCodeController4、InviteCodeService5、CrudRepository 点击锁定按钮&#xff0c;锁定按钮要变成解锁按钮&#xff0c;然后状态要从待绑定变成 已锁定&#xff1a;https://blog.csdn.net/m0_65152767/article/details…