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

二、代码

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、ImplList<Map<String, Object>> tablesAndColumns = getMySQL(sqlName, sqlPassword, sqlProjectName);for (Map<String, Object> tableInfo : tablesAndColumns) {String tableName = (String) tableInfo.get("TABLE_NAME");//TODO EntityString 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 ServiceString 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 ServiceImplString 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 DaoString 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 ControllerString 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 neededdefault: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.mzph.cn/pingmian/65799.shtml

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

相关文章

【二】arcgis JavaScript api 实现加载不同坐标系的底图和三维服务

提示&#xff1a;如果是天地图底图参考这篇文章 【一】arcgis JavaScript api 实现加载不同坐标系的底图和三维服务_arcgis js api 调用三维地图服务-CSDN博客 需求&#xff1a; 前端开发实现底图&#xff08;wkid&#xff1a;3857&#xff0c;web墨卡托&#xff09;&#x…

HTML——46.制作课程表

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title>课程表</title></head><body><h3>课程表</h3><table border"1" cellspacing"0"><tr><th colspan"…

ARM64 Windows 10 IoT工控主板运行x86程序效率测试

ARM上的 Windows 10 IoT 企业版支持仿真 x86 应用程序&#xff0c;而 ARM上的 Windows 11 IoT 企业版则支持仿真 x86 和 x64 应用程序。英创推出的名片尺寸ARM64工控主板ESM8400&#xff0c;可预装正版Windows 10 IoT企业版操作系统&#xff0c;x86程序可无需修改而直接在ESM84…

小程序组件 —— 23 组件案例 - 轮播图图片添加

上一节实现了轮播图的最外层结构&#xff0c;但是没有通过轮播图来渲染图片&#xff0c;这一节我们先讲一下小程序中怎么来渲染图片&#xff0c;讲解完之后会通过轮播图来展示图片&#xff1b; 在微信小程序中&#xff0c;如果需要渲染图片&#xff0c;需要使用 image 组件&am…

阿克曼(Ackmann)函数

时间限制&#xff1a;C/C 1000MS&#xff0c;其他语言 2000MS 内存限制&#xff1a;C/C 256MB&#xff0c;其他语言 512MB 难度&#xff1a;中等 分数&#xff1a;100 OI排行榜得分&#xff1a;12(0.1*分数2*难度) 描述 阿克曼(Ackmann)函数A(m&#xff0c;n)中&#xff0c;m&…

自动驾驶新纪元:城区NOA功能如何成为智能驾驶技术的分水岭

目录 一、NOA 的定义 二、NOA 的主要特点 导航集成 场景覆盖 智能决策 高级感知能力 驾驶员参与 三、NOA 的优势 四、NOA的衡量指标 定性评价指标 安全性评价指标定义 可靠性评价指标定义 舒适性评价指标定义 通行效率评价指标 定量评价指标 五、代表厂商的实测…

石岩路边理发好去处

周末带娃去罗租公园玩&#xff0c;罗租公园旁边就是百佳华和如意豪庭小区&#xff0c;发现如意豪庭小区对面挺多路边理发摊点 理发摊点聚焦在这里的原因是刚好前面城管来了暂时避避&#xff0c;例如还有一个阿姨剪到一半就跟着过来。这里的城管只是拍了一处没有摊位的地方&…

STM32-笔记24-智能开关垃圾桶盖

一、模块及接线 二&#xff0c;项目目的 以下几个事件触发时&#xff0c;自动开盖&#xff0c;并伴随蜂鸣器短响一声&#xff0c;同时 LED 灯闪烁一下&#xff0c;2秒后自动关盖&#xff1a; 检测到有人靠近检测到有震动按下按键 KEY1 实验框图如下&#xff1a; 三、项目前期…

从零开始开发纯血鸿蒙应用之UI封装

从零开始开发纯血鸿蒙应用 一、题引二、UI 组成三、UI 封装原则四、实现 lib_comps1、封装 UI 样式1.1、attributeModifier 属性1.2、自定义AttributeModifier<T>类 2、封装 UI 组件 五、总结 一、题引 在开始正文前&#xff0c;为了大家能够从本篇博文中&#xff0c;汲…

使用ArcGIS Pro自带的Notebook计算多个遥感指数

在之前的分享中&#xff0c;我们介绍了如何使用ArcPy将GEE下载的遥感影像转为单波段文件。基于前面创建的单波段文件&#xff0c;我们可以一次性计算多种遥感指数&#xff0c;例如NDVI、EVI、NDSI等。我这里直接在ArcGIS Pro中自带的Notebook进行的运行。如下图所示&#xff0c…

超大规模分类(一):噪声对比估计(Noise Contrastive Estimation, NCE)

NCE损失对应的论文为《A fast and simple algorithm for training neural probabilistic language models》&#xff0c;发表于2012年的ICML会议。 背景 在2012年&#xff0c;语言模型一般采用n-gram的方法&#xff0c;统计单词/上下文间的共现关系&#xff0c;比神经概率语言…

275-增强型多功能数据采集卡PCIe-6251-EX

产品特点&#xff1a; 高速高精度数据采集&#xff0c;16bit10MSPS&#xff0c;32路单端/16路差分高速高精度任意波形发生&#xff0c;14bit165MHz&#xff0c;2路完全独立完全可编程的I/O端口&#xff0c;33个完全可编程的量程选择&#xff0c;0~5V/0~10V/5V/10VPCIe通信接口…

Llama 3 后训练(三)

目录 4. 后训练 4.1 建模 图表解读 4.1.1 聊天对话格式 4.1.2 奖励建模 4.1.3 监督微调&#xff08;Supervised Finetuning&#xff09; 4.1.4 直接偏好优化&#xff08;Direct Preference Optimization&#xff09; 4.1.5 模型平均&#xff08;Model Averaging&#x…

机器人C++开源库The Robotics Library (RL)使用手册(四)

建立自己的机器人3D模型和运动学模型 这里以国产机器人天机TR8为例,使用最普遍的DH运动学模型,结合RL所需的描述文件,进行生成。 最终,需要的有两个文件,一个是.wrl三维模型描述文件;一个是.xml运动学模型描述文件。 1、通过STEP/STP三维文件生成wrl三维文件 机器人的…

Go+chromedp实现Web UI自动化测试

1.为什么使用go进行UI自动化测试&#xff1f; 速度&#xff1a;Go速度很快&#xff0c;这在运行包含数百个UI测试的测试套件时是一个巨大的优势 并发性&#xff1a;可以利用Go的内置并发性(goroutines)来并行化测试执行 简单&#xff1a;Go的简约语法允许您编写可读且可维护…

【LLM综述】29种大模型Prompt Engineering技术

note 从零样本&#xff08;Zero-shot&#xff09;提示到最新进展的各种提示技术&#xff0c;包括推理和逻辑链&#xff08;Chain-of-Thought, CoT&#xff09;提示、自动链式思考&#xff08;Auto-CoT&#xff09;提示、自我一致性&#xff08;Self-Consistency&#xff09;提…

基于SpringBoot的实验室信息管理系统【源码+文档+部署讲解】

系统介绍 视频演示 基于SpringBootVue实现的实验室信息管理系统采用前后端分离的架构方式&#xff0c;系统分为管理员、老师、用户三种角色&#xff0c;实现了用户管理、设备管理、实验室查询、公告、课程、实验室耗材管理、我的等功能 技术选型 开发工具&#xff1a;idea2…

arcgis模版空库怎么用(一)

这里以某个项目的数据为例&#xff1a; 可以看到&#xff0c;属性表中全部只有列标题&#xff0c;无数据内容 可能有些人会认为空库是用来往里面加入信息的&#xff0c;其实不是&#xff0c;正确的用法如下&#xff1a; 一、下图是我演示用的数据&#xff0c;我们可以看到其中…

基于Spring Boot + Vue3实现的在线汽车保养维修预约管理系统源码+文档

前言 基于Spring Boot Vue3实现的在线汽车保养维修预约管理系统是一种前后端分离架构的应用&#xff0c;它结合了Java后端开发框架Spring Boot和现代JavaScript前端框架Vue.js 3.0的优势。这样的系统可以为汽车服务站提供一个高效的平台来管理客户的预约请求 技术选型 系统…

论文研读:Text2Video-Zero 无需微调,仅改动<文生图模型>推理函数实现文生视频(Arxiv 2023-03-23)

论文名&#xff1a;Text2Video-Zero: Text-to-Image Diffusion Models are Zero-Shot Video Generators 1. 摘要 1.1 方法总结 通过潜空间插值, 实现动作连续帧。 以第一帧为锚定&#xff0c;替换原模型的self-attention&#xff0c;改为cross-attention 实现 保证图片整体场…