• 自动生成SSM框架


    使用idea 新创建项目

    然后 新创建 java 、resources 文件夹...... 

     

    图上是项目结构

    java文件夹下的 文件夹 命名规范 com.nf147(组织名)+ oukele(作者)

    然后将 自动生成类 放入 其中 ,运行 ,按照步骤进行操作

    每操作完成一步 都需要 刷新一下 maven项目 如:

    generatorConfig.xml 文件生成完后 ,刷新maven项目,然后双击红色箭头处后,自动生成 entity dao mapper

    自动生成类

    package com.nf147.oukele;
    
    import java.io.*;
    import java.sql.Connection;
    import java.sql.DatabaseMetaData;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    
    /**
     * 自动生成SSM框架需要的东西
     *
     * @author OUKELE
     * @create 2019-03-13 10:57
     */
    
    public class Generate {
    
        // 当前项目路径
        static String projectPath = System.getProperty("user.dir");
        //包名
        //static String Package ="com.oukele.demo";
        //数据库驱动 只支持 mariadb数据库的)
        static String drive = "org.mariadb.jdbc.Driver";
        //URL
        static String url = "jdbc:mariadb://localhost:3306/test";
        //数据库账号
        static String user = "oukele";
        //数据库密码
        static String password = "oukele";
        //组织名 用于自动生成 dao 、mapper_xml、entity
        static String Package = "com.nf147.oukele";
        //项目名
        static String projectName = "demo1";
    
        public static void main(String[] args) {
            Use();
        }
    
        //命令式 调用
        private static void Use() {
            System.out.println("		请输入命令		
    " +
                    "输入 1 则生成 pom.xml 文件
    " +
                    "输入 2 则生成 generatorConfig.xml 文件
    " +
                    "输入 3 则生成 Spring、SpringMVC等 相关xml 文件
    " +
                    "输入 4 则生成 service、serviceIpml、controller
    " +
                    "输入 5 则 删除自动生成文件,避免污染项目
    " +
                    "==================================================");
            int number = 0;
            Scanner in = new Scanner(System.in);
            System.out.println("请输入你的命令:");
            number = in.nextInt();
            switch (number) {
                case 1:
                    generate_pomXML();
                    break;
                case 2:
                    generate_generatorConfig();
                    break;
                case 3:
                    start_1();
                    System.out.println("请刷新一下 项目,使用 Generator插件 生成 dao、entity、mapper文件夹.. ");
                    break;
                case 4:
                    start();
                    break;
                case 5:
                    del();
                    del_0();
                    System.out.println(" 自动生成文件已删除......");
                    System.out.println(" 请刷新 maven项目,然后尽情的使用吧.....");
                    break;
                default:
                    System.out.println("没有这个命令.....");
                    break;
            }
    
        }
    
        //生成 自动配置 pom.xml 文件
        public static void generate_pomXML() {
    
            File f = new File(projectPath + File.separator + "/pom.xml");
            try {
                OutputStream out = new FileOutputStream(f);
                StringBuffer sbf = new StringBuffer();
                sbf.append("<?xml version="1.0" encoding="UTF-8" ?>
    " +
                        "<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    " +
                        "         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    " +
                        "    <modelVersion>4.0.0</modelVersion>
    " +
                        "
    " +
                        "    <!--组织名-->
    " +
                        "    <groupId>" + Package + "</groupId>
    " +
                        "    <!--项目名-->
    " +
                        "    <artifactId>" + projectName + "</artifactId>
    " +
                        "    <!--版本信息-->
    " +
                        "    <version>1.0</version>
    " +
                        "    <!--项目类型-->
    " +
                        "    <packaging>war</packaging>
    " +
                        "
    " +
                        "
    " +
                        "    <!--版本信息管理-->
    " +
                        "    <properties>
    " +
                        "        <spring.version>5.1.2.RELEASE</spring.version>
    " +
                        "        <springmvc.version>5.1.2.RELEASE</springmvc.version>
    " +
                        "        <!--项目构建编码-->
    " +
                        "        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    " +
                        "        <!-- maven 编译资源 版本 -->
    " +
                        "        <maven.compiler.source>1.8</maven.compiler.source>
    " +
                        "        <!-- maven 编译目标版本  -->
    " +
                        "        <maven.compiler.target>1.8</maven.compiler.target>
    " +
                        "        <!-- maven 编译插件版本 -->
    " +
                        "        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    " +
                        "    </properties>
    " +
                        "
    " +
                        "    <!--jar包的依赖-->
    " +
                        "    <dependencies>
    " +
                        "        <!--Spring 的依赖-->
    " +
                        "        <dependency>
    " +
                        "            <groupId>org.springframework</groupId>
    " +
                        "            <artifactId>spring-web</artifactId>
    " +
                        "            <version>${spring.version}</version>
    " +
                        "        </dependency>
    " +
                        "        <dependency>
    " +
                        "            <groupId>org.springframework</groupId>
    " +
                        "            <artifactId>spring-aop</artifactId>
    " +
                        "            <version>${spring.version}</version>
    " +
                        "        </dependency>
    " +
                        "        <dependency>
    " +
                        "            <groupId>org.springframework</groupId>
    " +
                        "            <artifactId>spring-jdbc</artifactId>
    " +
                        "            <version>${spring.version}</version>
    " +
                        "        </dependency>
    " +
                        "        <dependency>
    " +
                        "            <groupId>org.springframework</groupId>
    " +
                        "            <artifactId>spring-test</artifactId>
    " +
                        "            <version>${spring.version}</version>
    " +
                        "            <scope>test</scope>
    " +
                        "        </dependency>
    " +
                        "
    " +
                        "        <!--Spring MVC 的依赖-->
    " +
                        "        <dependency>
    " +
                        "            <groupId>org.springframework</groupId>
    " +
                        "            <artifactId>spring-webmvc</artifactId>
    " +
                        "            <version>${springmvc.version}</version>
    " +
                        "        </dependency>
    " +
                        "        <dependency>
    " +
                        "            <groupId>org.springframework</groupId>
    " +
                        "            <artifactId>spring-web</artifactId>
    " +
                        "            <version>${springmvc.version}</version>
    " +
                        "        </dependency>
    " +
                        "
    " +
                        "        <!--mybatis 的依赖-->
    " +
                        "        <dependency>
    " +
                        "            <groupId>org.mybatis</groupId>
    " +
                        "            <artifactId>mybatis</artifactId>
    " +
                        "            <version>3.4.6</version>
    " +
                        "        </dependency>
    " +
                        "        <dependency>
    " +
                        "            <groupId>org.mybatis</groupId>
    " +
                        "            <artifactId>mybatis-spring</artifactId>
    " +
                        "            <version>1.3.2</version>
    " +
                        "        </dependency>
    " +
                        "        <!--mybatis 分页-->
    " +
                        "        <dependency>
    " +
                        "            <groupId>com.github.pagehelper</groupId>
    " +
                        "            <artifactId>pagehelper</artifactId>
    " +
                        "            <version>5.1.7</version>
    " +
                        "        </dependency>
    " +
                        "        <!--c3p0 连接池 -->
    " +
                        "        <dependency>
    " +
                        "            <groupId>com.mchange</groupId>
    " +
                        "            <artifactId>c3p0</artifactId>
    " +
                        "            <version>0.9.5.2</version>
    " +
                        "        </dependency>
    " +
                        "
    " +
                        "        <!--数据库驱动-->
    " +
                        "        <dependency>
    " +
                        "            <groupId>org.mariadb.jdbc</groupId>
    " +
                        "            <artifactId>mariadb-java-client</artifactId>
    " +
                        "            <version>2.3.0</version>
    " +
                        "        </dependency>
    " +
                        "
    " +
                        "        <!-- Junit 单元测试-->
    " +
                        "        <dependency>
    " +
                        "            <groupId>junit</groupId>
    " +
                        "            <artifactId>junit</artifactId>
    " +
                        "            <version>4.12</version>
    " +
                        "            <scope>test</scope>
    " +
                        "        </dependency>
    " +
                        "
    " +
                        "        <!--日志框架 logback-->
    " +
                        "        <dependency>
    " +
                        "            <groupId>ch.qos.logback</groupId>
    " +
                        "            <artifactId>logback-classic</artifactId>
    " +
                        "            <version>1.2.3</version>
    " +
                        "            <scope>test</scope>
    " +
                        "        </dependency>
    " +
                        "
    " +
                        "        <!--其他-->
    " +
                        "        <!--server 接口 jar包-->
    " +
                        "        <dependency>
    " +
                        "            <groupId>javax.servlet</groupId>
    " +
                        "            <artifactId>javax.servlet-api</artifactId>
    " +
                        "            <version>4.0.1</version>
    " +
                        "        </dependency>
    " +
                        "
    " +
                        "        <dependency>
    " +
                        "            <groupId>com.google.code.gson</groupId>
    " +
                        "            <artifactId>gson</artifactId>
    " +
                        "            <version>2.8.5</version>
    " +
                        "        </dependency>
    " +
                        "
    " +
                        "        <dependency>
    " +
                        "            <groupId>javax.servlet</groupId>
    " +
                        "            <artifactId>jstl</artifactId>
    " +
                        "            <version>1.2</version>
    " +
                        "        </dependency>
    " +
                        "    </dependencies>
    " +
                        "
    " +
                        "    <build>
    " +
                        "        <plugins>
    " +
                        "            <plugin>
    " +
                        "                <groupId>org.mybatis.generator</groupId>
    " +
                        "                <artifactId>mybatis-generator-maven-plugin</artifactId>
    " +
                        "                <version>1.3.7</version>
    " +
                        "                <!--配置信息-->
    " +
                        "                <configuration>
    " +
                        "                    <!--配置文件-->
    " +
                        "                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
    " +
                        "                    <overwrite>true</overwrite>
    " +
                        "                </configuration>
    " +
                        "                <!--数据库的驱动的依赖-->
    " +
                        "                <dependencies>
    " +
                        "                    <dependency>
    " +
                        "                        <groupId>org.mariadb.jdbc</groupId>
    " +
                        "                        <artifactId>mariadb-java-client</artifactId>
    " +
                        "                        <version>2.3.0</version>
    " +
                        "                    </dependency>
    " +
                        "                </dependencies>
    " +
                        "            </plugin>
    " +
                        "
    " +
                        "        </plugins>
    " +
                        "    </build>
    " +
                        "
    " +
                        "
    " +
                        "</project>");
    
                out.write(new String(sbf).getBytes());
                out.flush();
                out.close();
                System.out.println("pom.xml 文件 配置成功,请重新刷新一下 maven项目");
            } catch (Exception e) {
                System.out.println("pom.xml 文件 配置失败,异常信息为:" + e.getMessage());
            }
    
        }
    
        //自动生成 generatorConfig.xml 文件
        public static void generate_generatorConfig() {
            File f = new File(projectPath + File.separator + "/src/main/resources/generatorConfig.xml");
    
            try {
                if (!f.isFile()) {
                    f.createNewFile();
                }
    
                OutputStream out = new FileOutputStream(f);
    
                StringBuffer sbf = new StringBuffer();
                sbf.append("<?xml version="1.0" encoding="UTF-8"?>
    " +
                        "<!DOCTYPE generatorConfiguration
    " +
                        "        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    " +
                        "        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    " +
                        "
    " +
                        "<generatorConfiguration>
    " +
                        "
    " +
                        "    <context id="default" targetRuntime="MyBatis3Simple">
    " +
                        "        <!--创建class时,对注释进行控制-->
    " +
                        "        <commentGenerator>
    " +
                        "            <property name="suppressDate" value="true" />
    " +
                        "            <!--去除注释-->
    " +
                        "            <property name="suppressAllComments" value="true"/>
    " +
                        "        </commentGenerator>
    " +
                        "
    " +
                        "        <!--jdbc的数据库连接-->
    " +
                        "        <jdbcConnection driverClass="" + drive + ""
    " +
                        "                        connectionURL="" + url + ""
    " +
                        "                        userId="" + user + "" password="" + password + "">
    " +
                        "        </jdbcConnection>
    " +
                        "        <!-- Model模型生成器
    " +
                        "            targetPackage -> 指定生成的model生成所在的包名
    " +
                        "            targetProject -> 指定在该项目下所在的路径
    " +
                        "        -->
    " +
                        "        <javaModelGenerator targetPackage="" + Package + ".entity" targetProject="src/main/java">
    " +
                        "            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
    " +
                        "            <property name="trimStrings" value="true" />
    " +
                        "        </javaModelGenerator>
    " +
                        "        <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件-->
    " +
                        "        <sqlMapGenerator targetPackage="Mapper"  targetProject="src/main/resources"/>
    " +
                        "
    " +
                        "        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
    " +
                        "            type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
    " +
                        "            type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
    " +
                        "            type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
    " +
                        "        -->
    " +
                        "        <javaClientGenerator type="XMLMAPPER" targetPackage="" + Package + ".dao"  targetProject="src/main/java"/>
    " +
                        "
    " +
                        "        <!-- tableName 表名  % -> 全部表   -->
    " +
                        "        <table tableName="%">
    " +
                        "            <generatedKey column="id" sqlStatement="Mysql"/>
    " +
                        "        </table>
    " +
                        "    </context>
    " +
                        "</generatorConfiguration>");
    
                out.write(new String(sbf).getBytes());
                out.flush();
                out.close();
                System.out.println("generatorConfig.xml 文件创建成功,请刷新一下maven项目");
            } catch (Exception e) {
                System.out.println("generatorConfig.xml 文件创建失败,异常信息为:" + e.getMessage());
            }
    
        }
    
        /**
         * 把输入字符串的首字母改成大写
         *
         * @param str
         * @return
         */
        private static String initcap(String str) {
            char[] ch = str.toCharArray();
            if (ch[0] >= 'a' && ch[0] <= 'z') {
                ch[0] = (char) (ch[0] - 32);
            }
            return new String(ch);
        }
    
        /**
         * 把输入字符串的首字母改成小写
         *
         * @param str
         * @return
         */
        private static String initlow(String str) {
            char[] ch = str.toCharArray();
            if (ch[0] >= 'A' && ch[0] <= 'Z') {
                ch[0] = (char) (ch[0] + 32);
            }
            return new String(ch);
        }
    
        //首字母转换和下划线转换
        private static String tables(String table) {
            String[] tables = table.split("_");
            table = "";
            for (String s : tables) {
                table += initcap(s);
            }
            return table;
        }
    
        //获取 数据库中的表名
        private static List getTable() {
            List<String> table_name = new ArrayList<>();
            try {
                Class.forName(drive);
                Connection connection = DriverManager.getConnection(url, user, password);
    
                DatabaseMetaData metaData = connection.getMetaData();
                ResultSet tables = metaData.getTables(null, null, null, null);
    
                while (tables.next()) {
                    table_name.add(tables(tables.getString("TABLE_NAME")));
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return table_name;
        }
    
        //创建 service 层
    
        /**
         * @param tableName 表名
         */
        private static String generate_Service(String tableName) {
    
            StringBuffer sbf = new StringBuffer("package " + Package + ".service;
    " +
                    "
    " +
                    "import " + Package + ".entity." + tableName + ";
    " +
                    "
    " +
                    "import java.util.List;
    " +
                    "
    " +
                    "public interface " + tableName + "Service {
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 删除操作 根据id
    " +
                    "     *
    " +
                    "     * @param id
    " +
                    "     * @return
    " +
                    "     */" +
                    "
    " +
                    "    int deleteByPrimaryKey(Integer id);
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 添加操作
    " +
                    "     *
    " +
                    "     * @param " + initlow(tableName) + "
    " +
                    "     * @return
    " +
                    "     */" +
                    "
    " +
                    "    int insert(" + tableName + " " + initlow(tableName) + ");
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 根据id查询操作
    " +
                    "     *
    " +
                    "     * @param id
    " +
                    "     * @return
    " +
                    "     */" +
                    "
    " +
                    "    " + tableName + " selectByPrimaryKey(Integer id);
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 全部查询操作
    " +
                    "     *
    " +
                    "     * @return
    " +
                    "     */" +
                    "
    " +
                    "    List<" + tableName + "> selectAll();
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 修改操作
    " +
                    "     *
    " +
                    "     * @param " + initlow(tableName) + "
    " +
                    "     * @return
    " +
                    "     */" +
                    "
    " +
                    "    int updateByPrimaryKey(" + tableName + " " + initlow(tableName) + ");
    " +
                    "}");
            return new String(sbf);
        }
    
        /**
         * 创建ServiceImpl
         *
         * @param tableName 数据库表
         */
        private static String generate_ServiceImp(String tableName) {
    
            String serviceImpl = "package " + Package + ".service.impl;
    " +
                    "
    " +
                    "import " + Package + ".dao." + tableName + "Mapper;
    " +
                    "import " + Package + ".entity." + tableName + ";
    " +
                    "import " + Package + ".service." + tableName + "Service;
    " +
                    "import org.springframework.beans.factory.annotation.Autowired;
    " +
                    "import org.springframework.stereotype.Service;
    " +
                    "
    " +
                    "import java.util.List;
    " +
                    "
    " +
                    "@Service
    " +
                    "public class " + tableName + "ServiceImpl implements " + tableName + "Service {
    " +
                    "
    " +
                    "    @Autowired
    " +
                    "    private " + tableName + "Mapper " + initlow(tableName) + "Mapper;
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 删除操作 根据id删除
    " +
                    "     *
    " +
                    "     * @param id
    " +
                    "     * @return
    " +
                    "     */
    " +
                    "    @Override
    " +
                    "    public int deleteByPrimaryKey(Integer id) {
    " +
                    "        return " + initlow(tableName) + "Mapper.deleteByPrimaryKey(id);
    " +
                    "    }
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 添加操作
    " +
                    "     *
    " +
                    "     * @param " + initlow(tableName) + "
    " +
                    "     * @return
    " +
                    "     */
    " +
                    "    @Override
    " +
                    "    public int insert(" + tableName + " " + initlow(tableName) + ") {
    " +
                    "        return " + initlow(tableName) + "Mapper.insert(" + initlow(tableName) + ");
    " +
                    "    }
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 根据id查询操作
    " +
                    "     *
    " +
                    "     * @param id
    " +
                    "     * @return
    " +
                    "     */
    " +
                    "    @Override
    " +
                    "    public " + tableName + " selectByPrimaryKey(Integer id) {
    " +
                    "        return " + initlow(tableName) + "Mapper.selectByPrimaryKey(id);
    " +
                    "    }
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 全部查询操作
    " +
                    "     *
    " +
                    "     * @return
    " +
                    "     */
    " +
                    "    @Override
    " +
                    "    public List<" + tableName + "> selectAll() {
    " +
                    "        return " + initlow(tableName) + "Mapper.selectAll();
    " +
                    "    }
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 修改操作
    " +
                    "     *
    " +
                    "     * @param " + initlow(tableName) + "
    " +
                    "     * @return
    " +
                    "     */
    " +
                    "    @Override
    " +
                    "    public int updateByPrimaryKey(" + tableName + " " + initlow(tableName) + ") {
    " +
                    "        return " + initlow(tableName) + "Mapper.updateByPrimaryKey(" + initlow(tableName) + ");
    " +
                    "    }
    " +
                    "}
    ";
            return serviceImpl;
        }
    
        /**
         * 创建Controller
         *
         * @param tableName 数据库表
         */
        private static String generate_Controller(String tableName) {
            String controller = "package " + Package + ".controller;
    " +
                    "import " + Package + ".entity." + tableName + ";
    " +
                    "import " + Package + ".service." + tableName + "Service;
    " +
                    "import org.springframework.beans.factory.annotation.Autowired;
    " +
                    "import org.springframework.web.bind.annotation.*;
    " +
                    "import java.util.List;
    " +
                    "
    " +
                    "@RestController
    " +
                    "@RequestMapping("/" + initlow(tableName) + "")
    " +
                    "public class " + tableName + "Controller {
    " +
                    "    @Autowired
    " +
                    "    private " + tableName + "Service " + initlow(tableName) + "Service;
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 根据id删除
    " +
                    "     * 要求转入 id
    " +
                    "     *
    " +
                    "     * @param id
    " +
                    "     * @return
    " +
                    "     */
    " +
                    "    @GetMapping("/deleteByPrimaryKey/{id}")
    " +
                    "    public Object deleteByPrimaryKey(@PathVariable("id") int id) {
    " +
                    "        try {
    " +
                    "
    " +
                    "            return " + initlow(tableName) + "Service.deleteByPrimaryKey(id) > 0 ? "删除成功" : "删除失败";
    " +
                    "        } catch (Exception ex) {
    " +
                    "            return "出错,请重试!";
    " +
                    "        }
    " +
                    "    }
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 添加对象" + initlow(tableName) + "
    " +
                    "     *
    " +
                    "     * @param " + initlow(tableName) + "
    " +
                    "     * @return
    " +
                    "     */
    " +
                    "    @PostMapping("/insert")
    " +
                    "    public Object insert(@RequestBody " + tableName + " " + initlow(tableName) + ") {
    " +
                    "        try {
    " +
                    "            return " + initlow(tableName) + "Service.insert(" + initlow(tableName) + ") > 0 ? "添加成功!" : "添加失败!";
    " +
                    "        } catch (Exception ex) {
    " +
                    "            return "出错,请重试!";
    " +
                    "        }
    " +
                    "
    " +
                    "    }
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 根据id查找对象  最多只能返回一个对象
    " +
                    "     *
    " +
                    "     * @param id 
    " +
                    "     * @return
    " +
                    "     */
    " +
                    "    @GetMapping("/selectByPrimaryKey/{id}")
    " +
                    "    public Object selectByPrimaryKey(@PathVariable("id") int id) {
    " +
                    "        try {
    " +
                    "            " + tableName + " " + initlow(tableName) + "1 = " + initlow(tableName) + "Service.selectByPrimaryKey(id);
    " +
                    "            if (" + initlow(tableName) + "1 == null) {
    " +
                    "                return "无数据";
    " +
                    "            } else {
    " +
                    "                return " + initlow(tableName) + "1;
    " +
                    "            }
    " +
                    "        } catch (Exception ex) {
    " +
                    "            return "出错,请重试!";
    " +
                    "        }
    " +
                    "    }
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 查询所有数据
    " +
                    "     *
    " +
                    "     * @return
    " +
                    "     */
    " +
                    "    @GetMapping("/selectAll")
    " +
                    "    public Object selectAll() {
    " +
                    "        //public Result selectAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
    " +
                    "        try {
    " +
                    "            //分页
    " +
                    "            //PageHelper.startPage(pageNum, pageSize);
    " +
                    "            List<" + tableName + "> list = " + initlow(tableName) + "Service.selectAll();
    " +
                    "            if (list == null) {
    " +
                    "                return "无数据";
    " +
                    "            } else {
    " +
                    "                // return new Result().success(list, " + initlow(tableName) + "Service.count(""));
    " +
                    "                return list;
    " +
                    "            }
    " +
                    "        } catch (Exception ex) {
    " +
                    "            return "出错,请重试!";
    " +
                    "        }
    " +
                    "    }
    " +
                    "
    " +
                    "    /**
    " +
                    "     * 根据id修改全部字段
    " +
                    "     *
    " +
                    "     * @param " + initlow(tableName) + "
    " +
                    "     * @return
    " +
                    "     */
    " +
                    "    @PutMapping(value = "/updateByPrimaryKey")
    " +
                    "    public Object updateByPrimaryKey(@RequestBody " + tableName + " " + initlow(tableName) + ") {
    " +
                    "        try {
    " +
                    "            return " + initlow(tableName) + "Service.updateByPrimaryKey(" + initlow(tableName) + ") > 0 ? "修改成功" : "修改失败";
    " +
                    "        } catch (Exception ex) {
    " +
                    "            return "出错,请重试!";
    " +
                    "        }
    " +
                    "
    " +
                    "
    " +
                    "    }
    " +
                    "}
    ";
            return controller;
        }
    
        //创建 service serviceImp Controller
        private static void create(File file, String context) {
    
            //获取文件
            File parent = file.getParentFile();
            //如果不是目录
            if (parent != null) {
                //创建目录
                parent.mkdirs();
            }
            try {
                //创建文件
                file.createNewFile();
                FileWriter fileWriter = null;
                try {
                    fileWriter = new FileWriter(file);
                    fileWriter.write(context);
                    fileWriter.flush();
                    fileWriter.close();
                } catch (IOException e) {
    
                }
            } catch (IOException e) {
                System.out.println("创建文件失败:" + e.getMessage());
            }
        }
    
        // 调用入口
        private static void start() {
    
            String path = projectPath + "/src/main/java/";
            String[] split = Package.split("\.");
            for (String s : split) {
                path += s + "/";
            }
            List table = getTable();
            for (Object str : table) {
                create(new File(path + "service/" + str + "Service.java"), generate_Service((String) str));
                create(new File(path + "/service" + "/impl/" + str + "ServiceImpl.java"), generate_ServiceImp((String) str));
                create(new File(path + "controller/" + str + "Controller.java"), generate_Controller((String) str));
            }
        }
    
        //删除自动生成类
        private static void del_0() {
            String[] clazzName = Thread.currentThread().getStackTrace()[1].getClassName().split("\.");
    
            String path = projectPath + "/src/main/java/";
            String[] split = Package.split("\.");
            for (String s : split) {
                path += s + "/";
            }
            File f = new File(path + clazzName[3] + ".java");
            f.delete();
            System.out.println(clazzName[3] + ".java 文件已经删除....");
            String path1 = projectPath + "/src/main/resources/generatorConfig.xml";
            File f1 = new File(path1);
            f1.delete();
            del_build();
        }
    
        // 删除 pom.xml 中 build 节点
        private static void del_build(){
            String propertyPath = System.getProperty("user.dir");
            File f = new File(propertyPath + "/pom.xml" );
    
            try {
                InputStream in = new FileInputStream(f);
                byte[] arr = new byte[(int) f.length()];
                in.read(arr);
                in.close();
    
                String context = new String(arr);
    
                int i = context.indexOf("<build>");
                int ii = context.indexOf("</build>") + 8;
    
                String replace = context.replace(context.substring(i, ii), "");
    
                OutputStream out = new FileOutputStream(f);
                out.write(replace.getBytes());
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
        /**
         * ====================================下面是生成配置文件的=======================================================
         */
    
        //删除文件
        private static void del() {
            String path = System.getProperty("user.dir") + "/src/main/java/";
    
            String[] split = Package.split("\.");
            for (String s : split) {
                path += s + "/";
            }
            String[] className = Thread.currentThread().getStackTrace()[1].getClassName().split("\.");
    
            File f = new File(path + className[3] + ".java");
            f.delete();
    
        }
    
        //调用入口
        private static void start_1() {
    
            String path = System.getProperty("user.dir") + "/src/main/resources/";
            String path1 = System.getProperty("user.dir") + "/src/main/webapp/WEB-INF/web.xml";
            createFile(path + "jdbc.properties", new String(generate_jdbc()));
            createFile(path + "mybatis-config.xml", new String(generate_mybatisConfig()));
            createFile(path + "logback-config.xml", new String(generate_logBack()));
            createFile(path + "spring-root.xml", new String(generate_springRoot()));
            createFile(path + "spring-web.xml", new String(generate_springWeb()));
            createFile(path1, new String(generate_xml()));
        }
    
        private static void createFile(String path, String content) {
            File f = new File(path);
            try {
                f.createNewFile();
                OutputStream out = new FileOutputStream(f);
                out.write(content.getBytes());
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        //自动生成 jdbc.properties 文件
        private static StringBuffer generate_jdbc() {
            StringBuffer sbf = new StringBuffer();
            sbf.append("jdbc.driver=org.mariadb.jdbc.Driver
    " +
                    "jdbc.url=" + url + "
    " +
                    "jdbc.username=" + user + "
    " +
                    "jdbc.password=" + password + "");
            return sbf;
        }
    
        //自动生成 web.xml 文件
        private static StringBuffer generate_xml() {
            StringBuffer sbf = new StringBuffer();
            sbf.append("<?xml version="1.0" encoding="UTF-8"?>
    " +
                    "<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    " +
                    "         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    " +
                    "         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    " +
                    "         version="4.0">
    " +
                    "    <!--项目名-->
    " +
                    "    <display-name>" + projectName + "</display-name>
    " +
                    "
    " +
                    "    <!--配置spring容器-->
    " +
                    "    <context-param>
    " +
                    "        <param-name>contextConfigLocation</param-name>
    " +
                    "        <param-value>classpath:spring-root.xml</param-value>
    " +
                    "    </context-param>
    " +
                    "    <listener>
    " +
                    "        <!--上下文环境侦听器-->
    " +
                    "        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    " +
                    "    </listener>
    " +
                    "
    " +
                    "    <!--配置springMVC容器-->
    " +
                    "    <servlet>
    " +
                    "        <servlet-name>webs</servlet-name>
    " +
                    "        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    " +
                    "        <init-param>
    " +
                    "            <param-name>contextConfigLocation</param-name>
    " +
                    "            <param-value>classpath:spring-web.xml</param-value>
    " +
                    "        </init-param>
    " +
                    "        <multipart-config>
    " +
                    "            <!--上传文件的大小限制,比如下面表示 5 M-->
    " +
                    "            <max-file-size>5242880</max-file-size>
    " +
                    "            <!--一次表单提交中文件的大小限制,必须下面代表 10 M -->
    " +
                    "            <max-request-size>10485760</max-request-size>
    " +
                    "            <!-- 多大的文件会被自动保存到硬盘上。0 代表所有 -->
    " +
                    "            <file-size-threshold>0</file-size-threshold>
    " +
                    "        </multipart-config>
    " +
                    "    </servlet>
    " +
                    "    <!--将所有请求拦截下来,交给spring mvc 处理-->
    " +
                    "    <servlet-mapping>
    " +
                    "        <servlet-name>webs</servlet-name>
    " +
                    "        <url-pattern>/</url-pattern>
    " +
                    "    </servlet-mapping>
    " +
                    "
    " +
                    "    <!--汉字编码问题-->
    " +
                    "    <filter>
    " +
                    "        <filter-name>setEncoding</filter-name>
    " +
                    "        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    " +
                    "        <init-param>
    " +
                    "            <param-name>encoding</param-name>
    " +
                    "            <param-value>UTF-8</param-value>
    " +
                    "        </init-param>
    " +
                    "    </filter>
    " +
                    "    <filter-mapping>
    " +
                    "        <filter-name>setEncoding</filter-name>
    " +
                    "        <url-pattern>/*</url-pattern>
    " +
                    "    </filter-mapping>
    " +
                    "</web-app>");
            return sbf;
        }
    
        //自动生成 spring-root.xml 文件
        private static StringBuffer generate_springRoot() {
            StringBuffer sbf = new StringBuffer();
            sbf.append("<?xml version="1.0" encoding="UTF-8" ?>
    " +
                    "<beans xmlns="http://www.springframework.org/schema/beans"
    " +
                    "       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    " +
                    "       xmlns:contxt="http://www.springframework.org/schema/context"
    " +
                    "       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    " +
                    "
    " +
                    "    <!--扫描 service 包中所使用注解的类-->
    " +
                    "    <contxt:component-scan base-package="" + Package + ".service"/>
    " +
                    "
    " +
                    "
    " +
                    "    <!--加载jdbc资源文件-->
    " +
                    "    <contxt:property-placeholder location="classpath:jdbc.properties"/>
    " +
                    "    <!--配置数据源-->
    " +
                    "    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    " +
                    "        <!--加载数据库驱动-->
    " +
                    "        <property name="driverClass" value="${jdbc.driver}"/>
    " +
                    "        <!--连接的数据库字符串-->
    " +
                    "        <property name="jdbcUrl" value="${jdbc.url}"/>
    " +
                    "        <!--账号、密码-->
    " +
                    "        <property name="user" value="${jdbc.username}"/>
    " +
                    "        <property name="password" value="${jdbc.password}"/>
    " +
                    "    </bean>
    " +
                    "
    " +
                    "    <!--配置mybatis-->
    " +
                    "    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    " +
                    "        <!--实例化数据源-->
    " +
                    "        <property name="dataSource" ref="dataSource"/>
    " +
                    "        <!--加载mybatis的配置-->
    " +
                    "        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    " +
                    "        <!--映射mapper文件-->
    " +
                    "        <property name="mapperLocations" value="classpath:Mapper/*.xml"/>
    " +
                    "    </bean>
    " +
                    "
    " +
                    "    <!--简化调用-->
    " +
                    "    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    " +
                    "        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    " +
                    "        <!--加载使用的接口 -->
    " +
                    "        <property name="basePackage" value="" + Package + ".dao"/>
    " +
                    "    </bean>
    " +
                    "
    " +
                    "</beans>");
            return sbf;
        }
    
        //自动生成 spring-web.xml 文件
        private static StringBuffer generate_springWeb() {
    
            StringBuffer sbf = new StringBuffer();
            sbf.append("<?xml version="1.0" encoding="UTF-8" ?>
    " +
                    "<beans xmlns="http://www.springframework.org/schema/beans"
    " +
                    "       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    " +
                    "       xmlns:contxt="http://www.springframework.org/schema/context"
    " +
                    "       xmlns:mvc="http://www.springframework.org/schema/mvc"
    " +
                    "       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    " +
                    "
    " +
                    "    <!--扫描web包中的注解-->
    " +
                    "    <contxt:component-scan base-package="" + Package + ".controller"/>
    " +
                    "
    " +
                    "    <!--启动 mvc  常用注解-->
    " +
                    "    <mvc:annotation-driven ></mvc:annotation-driven>
    " +
                    "
    " +
                    "    <!--将所有静态资源交给server处理-->
    " +
                    "    <mvc:default-servlet-handler/>
    " +
                    "    
    " +
                    "    
    " +
                    "    <!--配置视图器-->
    " +
                    "    <!--前后端分离,这里使用不到-->
    " +
                    "    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    " +
                    "        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    " +
                    "        <!--前缀、后缀-->
    " +
                    "        <property name="prefix" value="/WEB-INF/jsp/"/>
    " +
                    "        <property name="suffix" value=".jsp"/>
    " +
                    "    </bean>
    " +
                    "
    " +
                    "</beans>");
            return sbf;
        }
    
        //自动生成 mybatis-config.xml 文件
        private static StringBuffer generate_mybatisConfig() {
            StringBuffer sbf = new StringBuffer();
            sbf.append("<?xml version="1.0" encoding="UTF-8" ?>
    " +
                    "<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    " +
                    "
    " +
                    "<configuration>
    " +
                    "    <settings>
    " +
                    "        <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
    " +
                    "        <setting name="useGeneratedKeys" value="true" />
    " +
                    "        <!-- 使用列别名替换列名 默认:true -->
    " +
                    "        <setting name="useColumnLabel" value="true" />
    " +
                    "        <!-- 开启驼峰命名转换:Table {create_time} -> Entity {createTime} -->
    " +
                    "        <setting name="mapUnderscoreToCamelCase" value="true" />
    " +
                    "    </settings>
    " +
                    "    <!--配置分页插件-->
    " +
                    "    <plugins>
    " +
                    "        <plugin interceptor="com.github.pagehelper.PageInterceptor" />
    " +
                    "    </plugins>
    " +
                    "
    " +
                    "</configuration>");
            return sbf;
        }
    
        //自动生成 logback-config.xml 文件
        private static StringBuffer generate_logBack() {
            StringBuffer sbf = new StringBuffer();
            sbf.append("<?xml version="1.0" encoding="UTF-8" ?>
    " +
                    "<configuration >
    " +
                    "    <property name="LOG_HOME" value="tp/log"/>
    " +
                    "
    " +
                    "    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender" >
    " +
                    "        <!-- 输出的格式 -->
    " +
                    "        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    " +
                    "            <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}: %msg%n</pattern>-->
    " +
                    "            <pattern>
    " +
                    "                %msg%n
    " +
                    "            </pattern>
    " +
                    "
    " +
                    "        </encoder>
    " +
                    "    </appender>
    " +
                    "
    " +
                    "    <!-- 监控 哪些包中的方法调用 输出日志 -->
    " +
                    "    <logger name="" + Package + ".dao" level="DEBUG" additivity="false">
    " +
                    "        <!--<level value="INFO" />-->
    " +
                    "        <appender-ref ref="STDOUT" />
    " +
                    "    </logger>
    " +
                    "
    " +
                    "    <root level="error"  additivity="false" >
    " +
                    "        <appender-ref ref="STDOUT" />
    " +
                    "    </root>
    " +
                    "</configuration>");
            return sbf;
        }
    
    }
    View Code
  • 相关阅读:
    《剑指offer》39题—数组中出现次数超过一半的数字
    常见排序算法实现
    剑指offer题目分类
    腾讯2019实习面试题
    Word2vec资料
    Hello World投票以太坊Dapp教程-Part1
    以太坊开发框架Truffle学习笔记
    linux查看端口进程占用情况
    重置fedora root密码
    docker挂载本地目录的方法总结
  • 原文地址:https://www.cnblogs.com/oukele/p/10525551.html
Copyright © 2020-2023  润新知