spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.jdbc.Driver url: username: password: jackson: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss resources: static-locations: classpath:/static/,classpath:/views/ freemarker: template-loader-path: classpath:/genCodeTemplateV1/ charset: UTF-8 content-type: text/html
@Autowired private FreeMarkerConfigurer freeMarkerConfigurer; @Bean public Configuration getFreeMarkerConfiguration(){ return freeMarkerConfigurer.getConfiguration(); }
private Map<String, Object> productMapperByTemplate(Map<String, Object> data) throws Exception { ProductCodeDTO productDTO = (ProductCodeDTO)data.get("productCodeDTO"); Map dataOfTemplate = this.productTemplate(productDTO); String formatedTabName = productDTO.getDtoClassName().substring(0, productDTO.getDtoClassName().indexOf("DTO")); String templateMapperName = "template_Mapper.java";//模板名称 String fileDir = productDTO.getTargetDir() + productDTO.getMapperPackageName().replaceAll("\.", "/"); String fileNamePath = productDTO.getTargetDir() + productDTO.getMapperPackageName().replaceAll("\.", "/") + "/" + productDTO.getMapperClassName() + ".java"; Template template = this.getFreeMarkerConfiguration().getTemplate(templateMapperName);//读取模板内容 FileUtils.forceMkdir(new File(fileDir + "/"));//创建文件路径 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileNamePath), "UTF-8"); template.process(dataOfTemplate, out); //写入文件 out.flush(); out.close(); productDTO.setFormated_tab_name(formatedTabName); productDTO.setLower_tab_name(productDTO.getTableName().toLowerCase()); data.put("productCodeDTO", productDTO); dataOfTemplate.putAll(data); return dataOfTemplate; }