做旅行攻略的网站好,萝岗企业网站建设,小程序模板多少钱一套,买转发链接配置文件生成器-秒杀SSM的xml整合 思路#xff1a; 通过简单的配置#xff0c;直接生成对应配置文件。 maven坐标 dependencies!-- 配置文件生成 --dependencygroupIdorg.freemarker/groupIdartifactIdfreemarker…配置文件生成器-秒杀SSM的xml整合 思路 通过简单的配置直接生成对应配置文件。 maven坐标 dependencies!-- 配置文件生成 --dependencygroupIdorg.freemarker/groupIdartifactIdfreemarker/artifactIdversion2.3.31/version/dependency!--数据库驱动--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.27/version/dependency!--数据库连接池--dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.2.1/version/dependency!-- jspapi--dependencygroupIdjavax.servlet.jsp/groupIdartifactIdjsp-api/artifactIdversion2.2/version/dependency!-- jstl标签库 --dependencygroupIdjavax.servlet/groupIdartifactIdjstl/artifactIdversion1.2/version/dependency!--Mybatis--dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.2/version/dependency!--mybatis、spring整合--dependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion2.0.6/version/dependency!--Springmvc--dependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion5.2.12.RELEASE/version/dependency!-- mvc、实体类序列化成json。默认jackson --dependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion2.12.5/version/dependency!-- lombok插件 --dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.30/versionscopecompile/scope/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion5.1.9.RELEASE/version/dependency/dependenciesbuild!-- 必选生成ssm配置文件时务必把以下内容注释 。项目运行过程mybatis如果报错再打开、否则保留注释即可。 --!-- resources--!-- lt;!ndash; 静态资源过滤问题解决ndash;gt;--!-- resource--!-- directorysrc/main/java/directory--!-- includes--!-- include**/*.properties/include--!-- include**/*.xml/include--!-- /includes--!-- filteringfalse/filtering--!-- /resource--!-- resource--!-- directorysrc/main/resources/directory--!-- includes--!-- include**/*.properties/include--!-- include**/*.xml/include--!-- /includes--!-- filteringfalse/filtering--!-- /resource--!-- /resources--/build一、生成器ftl模板文件 模板文件全部放在resource目录下的template文件夹中 。 mybatis-config-template.ftl 模板文件。
?xml version1.0 encodingUTF-8?
!DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
configurationsettings!-- 打印sql日志 --setting namelogImpl valueSTDOUT_LOGGING //settings!-- 设置别名 --typeAliasespackage name${packageName}//typeAliases!-- 其他的MyBatis配置 --/configurationspringmvc-config-template.xml.ftl 模板文件
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:mvchttp://www.springframework.org/schema/mvcxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 配置Controller包扫描 --context:component-scan base-package${basePackage}/!-- 配置视图解析器 --bean classorg.springframework.web.servlet.view.InternalResourceViewResolverproperty nameprefix value${viewResolverPrefix}/property namesuffix value${viewResolverSuffix}//bean!--配置静态资源的访问映射此配置中的文件将不被前端控制器拦截 --mvc:resources location/static/ mapping/static/** /!-- 开启SpringMVC的注解 --mvc:annotation-driven//beansspring-mybatis-config-template.ftl 模板文件
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd!-- 扫描dao和service包 --context:component-scan base-package${basePackage}/!-- 数据源配置根据实际数据库连接信息修改 --bean iddataSource classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName value${dbDriverClassName}/property nameurl value${dbUrl}/property nameusername value${dbUsername}/property namepassword value${dbPassword}//bean!-- MyBatis的SqlSessionFactory配置 --bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSource/property namemapperLocations value${mapperLocations}/!--注入mybatis配置文件--property nameconfigLocation value${mybatisConfigLocation}//bean!-- MyBatis的MapperScannerConfigurer配置 --bean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage value${daoBasePackage}/property namesqlSessionFactoryBeanName valuesqlSessionFactory//bean!-- 事务管理器配置 --bean idtransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource//bean/beans
web-config-template.ftl 模板文件
?xml version1.0 encodingUTF-8?
web-app version3.1 xmlnshttp://xmlns.jcp.org/xml/ns/javaee xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd!-- Spring的核心监听器 --listenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listener!-- Spring的核心配置文件 --context-paramparam-namecontextConfigLocation/param-nameparam-value${springMyBatisConfigPath}/param-value/context-param!-- Spring MVC的DispatcherServlet --servletservlet-nameDispatcherServlet/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-value${springMvcConfigPath}/param-value/init-paramload-on-startup1/load-on-startup/servletservlet-mappingservlet-nameDispatcherServlet/servlet-nameurl-pattern//url-pattern/servlet-mapping!-- 其他的Web配置 --!-- 添加UTF-8编码的全局过滤器 --filterfilter-nameencodingFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-classinit-paramparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-paraminit-paramparam-nameforceEncoding/param-nameparam-valuetrue/param-value/init-param/filterfilter-mappingfilter-nameencodingFilter/filter-nameurl-pattern/*/url-pattern/filter-mapping/web-app
二、生成器代码
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/*** 最屌Java实习生 -chen fei* 2023/10/6*/
public class SSMWebXmlGenerator {// SSM 全局参数private static final String BASE_PACKAGE com.itheima;private static final String OUTPUT_DIR src/main/resources/ssm; // 配置文件生成目录// MyBatis 配置参数private static final String MYBATIS_CONFIG_LOCATIONS classpath:ssm/mybatis-config.xml;private static final String MYBATIS_OUTPUT_PATH OUTPUT_DIR /mybatis-config.xml; // 设置mybaits配置文件名字private static final String POJO_PACKAGE BASE_PACKAGE .pojo; // typeAliases 设置别名参数// Spring MVC 配置参数private static final String SPRING_MVC_OUTPUT_PATH OUTPUT_DIR /springmvc-config.xml; //springmvc配置文件名字private static final String CONTROLLER_PACKAGE BASE_PACKAGE .controller; // 配置controller包位置以便可以扫描到里面的beanprivate static final String VIEW_RESOLVER_PREFIX /WEB-INF/jsp/; //视图解析器前缀private static final String VIEW_RESOLVER_SUFFIX .jsp;//视图解析器后缀// Spring 配置参数private static final String SPRING_OUTPUT_PATH OUTPUT_DIR /spring-mybatis-config.xml; //spring配置文件名字private static final String DAO_PACKAGE BASE_PACKAGE .dao; // dao层的全包名private static final String SERVICE_PACKAGE BASE_PACKAGE .service; //service层的全包名private static final String DB_DRIVER_CLASS_NAME com.mysql.cj.jdbc.Driver; //mysql驱动默认8.*private static final String DB_URL jdbc:mysql://localhost:3306/cnmsb; //urlprivate static final String DB_USERNAME root; //账号private static final String DB_PASSWORD root; // 密码private static final String MYBATIS_MAPPER_LOCATIONS classpath*:mapper/*.xml; //配置mybatis中xml的位置// web.xml 配置参数private static final String WEB_XML_OUTPUT_PATH src/main/webapp/WEB-INF/web.xml; //生成路径private static final String SPRING_MYBATIS_CONFIG_PATH classpath:ssm/spring-mybatis-config.xml; //spring配置文件名private static final String SPRING_MVC_CONFIG_PATH classpath:ssm/springmvc-config.xml; //springmvc文件名public static void main(String[] args) throws IOException, TemplateException {try {
// 先生成所需目录createDirectoriesIfNotExists();
// 生成mybaits配置文件generateMyBatisConfig();
// mvc配置文件generateSpringMVCConfig();
// spring配置文件generateSpringConfig();
// web.xml配置文件。generateWebXml();} catch (IOException | TemplateException e) {e.printStackTrace();System.err.println(生成配置文件时出错 e.getMessage());System.out.println(请注意生成配置文件时、请删除或注释pom文件中build标签。会影响生成);}}private static void createDirectoriesIfNotExists() {// Create base package directoriesString[] subPackages {pojo, dao, service, controller, utils};for (String subPackage : subPackages) {String packagePath BASE_PACKAGE.replace(., /) / subPackage;File packageDir new File(src/main/java/ packagePath);if (!packageDir.exists()) {if (packageDir.mkdirs()) {System.out.println(Created package directory: packagePath);} else {System.err.println(Failed to create package directory: packagePath);}}}// Check and create VIEW_RESOLVER_PREFIX directoryFile viewResolverDir new File(src/main/webapp VIEW_RESOLVER_PREFIX);if (!viewResolverDir.exists()) {if (viewResolverDir.mkdirs()) {System.out.println(Created VIEW_RESOLVER_PREFIX directory: viewResolverDir.getPath());} else {System.err.println(Failed to create VIEW_RESOLVER_PREFIX directory: viewResolverDir.getPath());}}// Check and create mapper directory in resourcesFile resourcesDir new File(src/main/resources);File mapperDir new File(resourcesDir, mapper);if (!mapperDir.exists()) {if (mapperDir.mkdirs()) {System.out.println(Created mapper directory in resources.);} else {System.err.println(Failed to create mapper directory in resources.);}}File resourcesDir2 new File(src/main/resources);File mapperDir2 new File(resourcesDir2, static);if (!mapperDir2.exists()) {if (mapperDir2.mkdirs()) {System.out.println(Created static directory in resources.);} else {System.err.println(Failed to create static directory in resources.);}}}private static void generateMyBatisConfig() throws IOException, TemplateException {// 配置 FreeMarkerConfiguration configuration new Configuration(Configuration.VERSION_2_3_31);configuration.setClassForTemplateLoading(SSMWebXmlGenerator.class, /);configuration.setDefaultEncoding(UTF-8);// 加载 MyBatis 配置模板文件Template template configuration.getTemplate(template/mybatis-config-template.ftl);MapString, String dataModel new HashMap();// 设置 MyBatis 包名参数dataModel.put(packageName, POJO_PACKAGE);generateFile(MYBATIS_OUTPUT_PATH, template, dataModel);}private static void generateSpringMVCConfig() throws IOException, TemplateException {// 配置 FreeMarkerConfiguration configuration new Configuration(Configuration.VERSION_2_3_31);configuration.setClassForTemplateLoading(SSMWebXmlGenerator.class, /);configuration.setDefaultEncoding(UTF-8);// 加载 Spring MVC 配置模板文件Template template configuration.getTemplate(template/springmvc-config-template.xml.ftl);MapString, String dataModel new HashMap();// 设置 Spring MVC 包名参数和视图解析器参数dataModel.put(basePackage, CONTROLLER_PACKAGE);dataModel.put(viewResolverPrefix, VIEW_RESOLVER_PREFIX);dataModel.put(viewResolverSuffix, VIEW_RESOLVER_SUFFIX);generateFile(SPRING_MVC_OUTPUT_PATH, template, dataModel);}private static void generateSpringConfig() throws IOException, TemplateException {// 配置 FreeMarkerConfiguration configuration new Configuration(Configuration.VERSION_2_3_31);configuration.setClassForTemplateLoading(SSMWebXmlGenerator.class, /);configuration.setDefaultEncoding(UTF-8);// 加载 Spring 配置模板文件Template template configuration.getTemplate(template/spring-mybatis-config-template.ftl);MapString, String dataModel new HashMap();// 设置 Spring 包名参数和数据库连接参数dataModel.put(basePackage, DAO_PACKAGE , SERVICE_PACKAGE);dataModel.put(dbDriverClassName, DB_DRIVER_CLASS_NAME);dataModel.put(dbUrl, DB_URL);dataModel.put(dbUsername, DB_USERNAME);dataModel.put(dbPassword, DB_PASSWORD);dataModel.put(mapperLocations, MYBATIS_MAPPER_LOCATIONS);dataModel.put(mybatisConfigLocation, MYBATIS_CONFIG_LOCATIONS);dataModel.put(daoBasePackage, DAO_PACKAGE);generateFile(SPRING_OUTPUT_PATH, template, dataModel);}private static void generateWebXml() throws IOException, TemplateException {// 配置 FreeMarkerConfiguration configuration new Configuration(Configuration.VERSION_2_3_31);configuration.setClassForTemplateLoading(SSMWebXmlGenerator.class, /);configuration.setDefaultEncoding(UTF-8);// 加载 web.xml 配置模板文件Template template configuration.getTemplate(template/web-config-template.ftl);MapString, String dataModel new HashMap();// 设置 web.xml 配置参数引用之前生成的配置文件路径dataModel.put(springMyBatisConfigPath, SPRING_MYBATIS_CONFIG_PATH);dataModel.put(springMvcConfigPath, SPRING_MVC_CONFIG_PATH);generateFile(WEB_XML_OUTPUT_PATH, template, dataModel);}private static Configuration createConfiguration() {// 创建 FreeMarker 配置Configuration configuration new Configuration(Configuration.VERSION_2_3_31);configuration.setClassForTemplateLoading(SSMWebXmlGenerator.class, /);configuration.setDefaultEncoding(UTF-8);return configuration;}private static void generateFile(String outputPath, Template template, MapString, String dataModel)throws IOException, TemplateException {// 检查父目录是否存在如果不存在则创建File parentDir new File(outputPath).getParentFile();if (!parentDir.exists()) {parentDir.mkdirs();}// 写入文件FileWriter writer new FileWriter(new File(outputPath));template.process(dataModel, writer);writer.close();System.out.println(配置文件生成成功 outputPath);}
}