自动生成
只需要创建好maven项目,然后创建一个类Test,复制代码粘贴即可
使用注意:
代码
import java.io.*; public class Test { //包名格式 //列如配置到com.wbg.ssm包下 程序会自动添加daocontrollerserviceentity private final static String com = "com.wbg.ssm"; //数据库名称 private final static String database = ""; //数据库账号 private final static String user = "root"; //数据库密码 private final static String password = "123456"; public static void main(String[] args) throws IOException { createStart(); } static void createStart() { //获取当前项目的路径 String url = System.getProperty("user.dir"); System.out.println("开始配置pom.xml"); System.out.println(configPomXml(url)); url = url + File.separator + "src" + File.separator + "main"; System.out.println("开始配置resources目录"); createResources(url + File.separator + "resources"); System.out.println("完成配置resources目录"); System.out.println("开始配置webapp目录"); createWebapp(url + File.separator + "webapp"); System.out.println("完成配置webapp目录"); } //***********************Resources************************ /** * 创建四个配置文件 * dbc.properties * log4j.properties * mybatis-config.xml * spring-web.xml * * @return */ static boolean createResources(String url) { if (createJdbcProperties(url)) { System.out.println("jdbc.properties配置成功"); } else { System.out.println("jdbc.properties配置失败"); } if (log4jProperties(url)) { System.out.println("log4j.properties配置成功"); } else { System.out.println("log4j.properties配置失败"); } if (mybatisConfig(url)) { System.out.println("mybatis-config.xml配置成功"); } else { System.out.println("mybatis-config.xml配置失败"); } if (springWeb(url)) { System.out.println("spring-web.xml配置成功"); } else { System.out.println("spring-web.xml配置失败"); } if (generatorConfig(url)) { System.out.println("generatorConfig.xml配置成功"); } else { System.out.println("generatorConfig.xml配置失败"); } // esourcesspring if (springDao(url + File.separator + "spring")) { System.out.println("spring-dao.xml配置成功"); } else { System.out.println("spring-dao.xml配置失败"); } // esourcesspring if (springService(url + File.separator + "spring")) { System.out.println("spring-service.xml配置成功"); } else { System.out.println("spring-service.xml配置失败"); } return true; } /** * 创建jdbc.properties配置文件 * * @param url 路径 * @return */ static boolean createJdbcProperties(String url) { File file = new File(url, "jdbc.properties"); String context = "jdbc.driver=org.mariadb.jdbc.Driver " + "jdbc.url=jdbc:mariadb://localhost:3306/" + database + " " + "jdbc.user="+user+" " + "jdbc.password="+password+""; return createFile(file, context); } /** * 创建log4j.properties日志文件 * * @param url 路径 * @return */ static boolean log4jProperties(String url) { File file = new File(url, "log4j.properties"); String context = "# Global logging configuration " + "log4j.rootLogger=ERROR, ooo " + " " + "# MyBatis logging configuration... " + "log4j.logger." + com + ".dao=DEBUG " + " " + "# 规则1,名字为 ooo,向标准输出 System.err/out " + "log4j.appender.ooo=org.apache.log4j.ConsoleAppender " + "log4j.appender.ooo.layout=org.apache.log4j.PatternLayout " + "log4j.appender.ooo.layout.ConversionPattern=%5p [%t] ~ %m%n "; return createFile(file, context); } /** * 创建mybatis-config.xml配置文件 * * @param url 路径 * @return */ static boolean mybatisConfig(String url) { File file = new File(url, "mybatis-config.xml"); String context = "<?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 createFile(file, context); } /** * 创建spring-web.xml配置文件 * * @return */ static boolean springWeb(String url) { File file = new File(url, "spring-web.xml"); String context = "<?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:context="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-3.0.xsd"> " + " <!-- 配置SpringMVC --> " + " <!-- 1.开启SpringMVC注解模式 --> " + " <!-- 简化配置: " + " (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter " + " (2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持 " + " --> " + " <mvc:annotation-driven /> " + " " + " <!-- 2.静态资源默认servlet配置 " + " (1)加入对静态资源的处理:js,gif,png " + " (2)允许使用"/"做整体映射 " + " --> " + " <mvc:default-servlet-handler/> " + " " + " <!-- 3.配置jsp 显示ViewResolver --> " + " <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> " + " <!-- 4.扫描web相关的bean --> " + " <context:component-scan base-package="" + com + ".controller" /> " + "</beans>"; return createFile(file, context); } /** * 创建spring-dao.xml配置文件 * * @param url 路径 * @return */ static boolean springDao(String url) { File file = new File(url, "spring-dao.xml"); String context = "<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> " + " " + " " + " " + " <!-- 配置整合mybatis过程 --> " + " <!-- 1.配置数据库相关参数properties的属性:${url} --> " + " <context:property-placeholder location="classpath:jdbc.properties" /> " + " " + " <!-- 2.数据库连接池 --> " + " <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}" /> " + " " + " <!-- c3p0连接池的私有属性 --> " + " <property name="maxPoolSize" value="30" /> " + " <property name="minPoolSize" value="10" /> " + " <!-- 关闭连接后不自动commit --> " + " <property name="autoCommitOnClose" value="false" /> " + " <!-- 获取连接超时时间 --> " + " <property name="checkoutTimeout" value="10000" /> " + " <!-- 当获取连接失败重试次数 --> " + " <property name="acquireRetryAttempts" value="2" /> " + " </bean> " + " " + " <!-- 3.配置SqlSessionFactory对象 --> " + " <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> " + " <!-- 注入数据库连接池 --> " + " <property name="dataSource" ref="dataSource" /> " + " <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> " + " <property name="configLocation" value="classpath:mybatis-config.xml" /> " + " <!-- 扫描entity包 使用别名 --> " + " <property name="typeAliasesPackage" value="" + com + ".entity" /> " + " <!-- 扫描sql配置文件:mapper需要的xml文件 --> " + " <property name="mapperLocations" value="classpath:mapper/*.xml" /> " + " </bean> " + " " + " <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --> " + " <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> " + " <!-- 注入sqlSessionFactory --> " + " <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> " + " <!-- 给出需要扫描Dao接口包 --> " + " <property name="basePackage" value="" + com + ".dao" /> " + " </bean> " + " " + " <!--配置声明式事务管理--> " + " <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> " + " <property name="dataSource" ref="dataSource" /> " + " </bean> " + " <tx:annotation-driven proxy-target-class="true" /> " + " " + "</beans>"; return createFile(file, context); } /** * 创建spring-service.xml配置文件 * * @param url 路径 * @return */ static boolean springService(String url) { File file = new File(url, "spring-service.xml"); String context = "<?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:context="http://www.springframework.org/schema/context" " + " xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" " + " xmlns:aop="http://www.springframework.org/schema/aop" " + " 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> " + " <!-- 扫描service包下所有使用注解的类型 --> " + " <context:component-scan base-package="" + com + ".service" /> " + " <mvc:annotation-driven /> " + " <!-- 启用 aspectj 方式 AOP--> " + " <aop:aspectj-autoproxy proxy-target-class="true" /> " + "</beans>"; return createFile(file, context); } /** * 创建generatorConfig.xml配置文件 * @param url * @return */ static boolean generatorConfig(String url) { File file = new File(url, "generatorConfig.xml"); String context = "<?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="xxx" targetRuntime="MyBatis3Simple"> " + " " + " " + " <commentGenerator> " + " <property name="suppressDate" value="true" /> " + " </commentGenerator> " + " <!-- 数据库连接 --> " + " <jdbcConnection driverClass="org.mariadb.jdbc.Driver" " + " connectionURL="jdbc:mariadb://localhost/"+database+"" " + " userId=""+user+"" password=""+password+""> " + " </jdbcConnection> " + " " + " <!-- Model生成规则 --> " + " <javaModelGenerator targetPackage=""+com+".entity" targetProject="src/main/java"> " + " <property name="trimStrings" value="true" /> " + " </javaModelGenerator> " + " " + " <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/> " + " <!-- dao 规则 --> " + " <javaClientGenerator type="XMLMAPPER" targetPackage=""+com+".dao" targetProject="src/main/java"> " + " <property name="enableSubPackages" value="true" /> " + " </javaClientGenerator> " + " <table tableName="%"> " + " <generatedKey column="id" sqlStatement="Mysql"/> " + " </table> " + " </context> " + "</generatorConfiguration>"; return createFile(file, context); } //***********************webapp************************ static boolean createWebapp(String url) { if (webXml(url + File.separator + "WEB-INF")) { System.out.println("web.xml配置成功"); } else { System.out.println("web.xml配置失败"); } createCSSJSDirectory(url + File.separator); return true; } /** * 创建WEB-INFweb.xml配置文件 * * @param url 路径 * @return */ static boolean webXml(String url) { File file = new File(url, "web.xml"); String context = "<?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>自动生成</display-name> " + " " + " <!--解决中文乱码--> " + " <filter> " + " <filter-name>encodingFilter</filter-name> " + " <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> " + " <async-supported>true</async-supported> " + " <init-param> " + " <param-name>encoding</param-name> " + " <param-value>UTF-8</param-value> " + " </init-param> " + " " + " </filter> " + " <filter-mapping> " + " <filter-name>encodingFilter</filter-name> " + " <url-pattern>/*</url-pattern> " + " </filter-mapping> " + " " + " <!--配置 Spring 的容器--> " + " <context-param> " + " <param-name>contextConfigLocation</param-name> " + " <param-value>classpath:spring/spring-*.xml</param-value> " + " </context-param> " + " <listener> " + " <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> " + " </listener> " + " " + " <!--配置 MVC 容器--> " + " <!--将所有的请求都交给 Spring MVC 处理--> " + " <servlet> " + " <servlet-name>app</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> " + " </servlet> " + " <servlet-mapping> " + " <servlet-name>app</servlet-name> " + " <url-pattern>/</url-pattern> " + " </servlet-mapping> " + "</web-app>"; return createFile(file, context); } /** * 创建css和js * * @param url 路径 */ static boolean createCSSJSDirectory(String url) { File fcss = new File(url + "css"); if (fcss.mkdirs()) { System.out.println("成功创建css文件夹"); } File fjs = new File(url + "js"); if (fjs.mkdirs()) { System.out.println("成功创建js文件夹"); } return true; } /** * @param file 创建的文件 * @param context 文件里面的内容 */ static boolean createFile(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) { return false; } } catch (IOException e) { System.out.println("创建文件失败:" + e.getMessage()); } return true; } //***********************pom.xml************************ /** * 配置pom.xml文件 * * @param url 路径 */ static String configPomXml(String url) { File file = new File(url, "pom.xml"); InputStream inputStream = null; byte b[] = new byte[Integer.parseInt(String.valueOf(file.length()))]; StringBuffer stringBuffer = null; try { inputStream = new FileInputStream(file); inputStream.read(b); inputStream.close(); stringBuffer = new StringBuffer(new String(b)); stringBuffer.replace(Integer.parseInt(String.valueOf(file.length())) - 10, Integer.parseInt(String.valueOf(file.length())), ""); stringBuffer.append(pomContext()); } catch (Exception e) { return "程序出错,请重试 -- pom.xml文件配置失败"; } if (createFile(file, stringBuffer.toString())) { return "pom.xml文件配置完成"; } return "pom.xml文件配置失败"; } /** * pom.xml配置文件需要加的配置 * * @return */ static String pomContext() { return "<!--打包--> " + " <packaging>war</packaging> " + " <!--设置编码--> " + " <properties> " + " <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> " + " <maven.compiler.source>1.8</maven.compiler.source> " + " <maven.compiler.target>1.8</maven.compiler.target> " + " <spring.version>5.1.0.RELEASE</spring.version> " + " </properties> " + " <!--引入文件--> " + " <dependencies> " + " <!-- Spring Web MVC --> " + " <dependency> " + " <groupId>org.springframework</groupId> " + " <artifactId>spring-web</artifactId> " + " <version>${spring.version}</version> " + " </dependency> " + " <dependency> " + " <groupId>org.springframework</groupId> " + " <artifactId>spring-webmvc</artifactId> " + " <version>${spring.version}</version> " + " </dependency> " + " " + " <!-- servlet 系列的支持 --> " + " <dependency> " + " <groupId>javax</groupId> " + " <artifactId>javaee-api</artifactId> " + " <version>8.0</version> " + " <scope>provided</scope> " + " </dependency> " + " <dependency> " + " <groupId>javax.servlet</groupId> " + " <artifactId>jstl</artifactId> " + " <version>1.2</version> " + " </dependency> " + " " + " <dependency> " + " <groupId>com.github.pagehelper</groupId> " + " <artifactId>pagehelper</artifactId> " + " <version>5.1.7</version> " + " </dependency> " + " " + " <!-- Springframework --> " + " <dependency> " + " <groupId>org.springframework</groupId> " + " <artifactId>spring-context</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-aop</artifactId> " + " <version>${spring.version}</version> " + " </dependency> " + " <dependency> " + " <groupId>org.aspectj</groupId> " + " <artifactId>aspectjweaver</artifactId> " + " <version>1.9.1</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> " + " " + " <!-- 数据库驱动以及数据库连接池--> " + " <dependency> " + " <groupId>org.mariadb.jdbc</groupId> " + " <artifactId>mariadb-java-client</artifactId> " + " <version>2.3.0</version> " + " </dependency> " + " <dependency> " + " <groupId>com.mchange</groupId> " + " <artifactId>c3p0</artifactId> " + " <version>0.9.5.2</version> " + " </dependency> " + " " + " <!-- 日志框架 --> " + " <dependency> " + " <groupId>log4j</groupId> " + " <artifactId>log4j</artifactId> " + " <version>1.2.17</version> " + " </dependency> " + " " + " <!-- 通用工具 --> " + " <dependency> " + " <groupId>com.fasterxml.jackson.core</groupId> " + " <artifactId>jackson-databind</artifactId> " + " <version>2.9.7</version> " + " </dependency> " + " " + " <!-- 单元测试 --> " + " <dependency> " + " <groupId>org.springframework</groupId> " + " <artifactId>spring-test</artifactId> " + " <version>${spring.version}</version> " + " <scope>test</scope> " + " </dependency> " + " " + " <dependency> " + " <groupId>junit</groupId> " + " <artifactId>junit</artifactId> " + " <version>4.12</version> " + " <scope>test</scope> " + " </dependency> " + " </dependencies> " + " <build> " + " <finalName>contact</finalName> " + " <plugins> " + " <plugin> " + " <groupId>org.mybatis.generator</groupId> " + " <artifactId>mybatis-generator-maven-plugin</artifactId> " + " <version>1.3.7</version> " + " <dependencies> " + " <dependency> " + " <groupId>org.mariadb.jdbc</groupId> " + " <artifactId>mariadb-java-client</artifactId> " + " <version>2.3.0</version> " + " </dependency> " + " </dependencies> " + " </plugin> " + " </plugins> " + " " + " <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> " + " <plugins> " + " <plugin> " + " <artifactId>maven-clean-plugin</artifactId> " + " <version>3.0.0</version> " + " </plugin> " + " <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> " + " <plugin> " + " <artifactId>maven-resources-plugin</artifactId> " + " <version>3.0.2</version> " + " </plugin> " + " <plugin> " + " <artifactId>maven-compiler-plugin</artifactId> " + " <version>3.7.0</version> " + " </plugin> " + " <plugin> " + " <artifactId>maven-surefire-plugin</artifactId> " + " <version>2.20.1</version> " + " </plugin> " + " <plugin> " + " <artifactId>maven-war-plugin</artifactId> " + " <version>3.2.0</version> " + " </plugin> " + " <plugin> " + " <artifactId>maven-install-plugin</artifactId> " + " <version>2.5.2</version> " + " </plugin> " + " <plugin> " + " <artifactId>maven-deploy-plugin</artifactId> " + " <version>2.8.2</version> " + " </plugin> " + " </plugins> " + " </pluginManagement> " + " </build> " + "</project>"; } }
运行后