package org.yeshi.utils.generater; import org.springframework.data.mongodb.core.mapping.Document; import org.yeshi.utils.FileUtil; import org.yeshi.utils.StringUtil; import org.yeshi.utils.generater.entity.ClassInfo; import org.yeshi.utils.generater.entity.admin.AdminGeneraterInfo; import org.yeshi.utils.generater.mybatis.Table; import org.yeshi.utils.generater.util.NameUtil; import org.yeshi.utils.generater.vo.ExceptionVO; import org.yeshi.utils.generater.vo.admin.controller.AdminControllerInfoVO; import org.yeshi.utils.generater.vo.admin.controller.AdminVOVO; import org.yeshi.utils.generater.vo.dao.MongoDBDaoVO; import org.yeshi.utils.generater.vo.dao.MyBatisDBDaoVO; import org.yeshi.utils.generater.vo.service.QueryVO; import org.yeshi.utils.generater.vo.service.ServiceImplInfoVO; import org.yeshi.utils.generater.vo.service.ServiceInfoVO; import org.yeshi.utils.generater.vo.xmlconfig.GenertorConfig; import org.yeshi.utils.mongo.MongodbBaseDao; import org.yeshi.utils.mybatis.BaseMapper; import java.io.File; import java.io.IOException; import java.lang.annotation.Annotation; /** * @author Administrator * @title: GeneraterManager * @description: 自动化代码生成管理器(采用解析xml配置文件的形式) * @date 2021/10/13 14:48 */ /** * Title * * @Description //TODO * @Author hxh * @Date 18:11 2021/10/13 **/ public class GeneraterManagerV2 { private static GeneraterManagerV2 instance; private String classRootDir; private GenertorConfig config; public static GeneraterManagerV2 getInstance() { if (instance == null) instance = new GeneraterManagerV2(); return instance; } public void init(String classRootDir, GenertorConfig config) { this.classRootDir = classRootDir; this.config = config; } //获取文件夹的路径 private String getDirPath(String packageName) { File f = new File(classRootDir); if (!f.exists()) { f.mkdirs(); } f = new File(f.getAbsolutePath(), packageName.replace(".", "/")); if (!f.exists()) { f.mkdirs(); } return f.getAbsolutePath(); } /** * @return void * @author hxh * @description 保存文件 * @date 10:06 2021/10/14 * @param: data * @param: path **/ private void saveFile(String data, String path) throws IOException { if (!new File(path).exists()) new File(path).createNewFile(); FileUtil.saveAsFileByte(data.getBytes("UTF-8"), path); } public void createException(ClassInfo base) throws Exception { if (config.getService().getException() == null) { throw new Exception("尚未配置异常信息"); } String exceptionName = NameUtil.getDefaultExceptionName(config.getEntity()); String path = getDirPath(config.getService().getException().getPkg()); try { ExceptionVO vo = new ExceptionVO.Builder().setEntity(config.getEntity()).setPackageName(config.getService().getException().getPkg()).setBase(base).build(); String result = GeneraterUtil.createException(vo); //保存 saveFile(result, new File(path, exceptionName + ".java").getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } } public AdminVOVO createAdminVO() throws Exception { if (StringUtil.isNullOrEmpty(config.getAdmin().getController().getAdminVOPackage())) { return null; } String name = NameUtil.getDefaultAdminVOName(config.getEntity()); String path = getDirPath(config.getAdmin().getController().getAdminVOPackage()); try { AdminVOVO vo = new AdminVOVO.Builder().setEntity(config.getEntity()).setPackageName(config.getAdmin().getController().getAdminVOPackage()).build(); String result = GeneraterUtil.createAdminVO(vo); //保存 saveFile(result, new File(path, name + ".java").getAbsolutePath()); return vo; } catch (Exception e) { e.printStackTrace(); } return null; } public void createDao() throws Exception { Annotation[] as = config.getEntity().getAnnotations(); for (Annotation a : as) { if (a instanceof Table) { createMapper(); break; } else if (a instanceof Document) { createMongoDBDao(); break; } } } /** * @return org.yeshi.utils.generater.vo.dao.MongoDBDaoVO * @author hxh * @description 自动生成Dao * @date 18:24 2021/10/13 * @param: packageName **/ public MongoDBDaoVO createMongoDBDao() throws Exception { if (config.getDao() == null) { throw new Exception("尚未配置Dao信息"); } String daoName = NameUtil.getDefaultDaoName(config.getEntity()); String path = getDirPath(config.getDao().getPkg()); MongoDBDaoVO vo = new MongoDBDaoVO.Builder() .setBaseDao(new ClassInfo(MongodbBaseDao.class.getSimpleName(), MongodbBaseDao.class.getName())) .setDao(new ClassInfo(daoName, config.getDao().getPkg() + "." + daoName)) .setEntity(config.getEntity()).setDaoData(config.getDao()).build(); String result = GeneraterUtil.createMongoDBDao(vo); //保存 saveFile(result, new File(path, daoName + ".java").getAbsolutePath()); return vo; } /** * @return org.yeshi.utils.generater.vo.dao.MyBatisDBDaoVO * @author hxh * @description 自动生成Mapper * @date 18:24 2021/10/13 * @param: packageName * @param: mapperDir **/ public MyBatisDBDaoVO createMapper() throws Exception { if (config.getDao() == null) { throw new Exception("尚未配置Dao信息"); } if (StringUtil.isNullOrEmpty(config.getDao().getMapperDir())) { throw new Exception("Mapper存放目录为空"); } String mapperName = NameUtil.getDefaultMapperName(config.getEntity()); String path = getDirPath(config.getDao().getPkg()); MyBatisDBDaoVO vo = new MyBatisDBDaoVO.Builder() .setBaseMapper(new ClassInfo(BaseMapper.class.getSimpleName(), BaseMapper.class.getName())) .setMapper(new ClassInfo(mapperName, config.getDao().getPkg() + "." + mapperName)) .setEntity(config.getEntity()) .setDaoData(config.getDao()) .build(); String result = GeneraterUtil.createMybatisDBMapper(vo); //保存 saveFile(result, new File(path, mapperName + ".java").getAbsolutePath()); if (!new File(config.getDao().getMapperDir()).exists()) new File(config.getDao().getMapperDir()).mkdirs(); result = GeneraterUtil.createMybatisDBMapperXML(vo); saveFile(result, new File(config.getDao().getMapperDir(), mapperName + ".xml").getAbsolutePath()); return vo; } /** * @return org.yeshi.utils.generater.vo.service.QueryVO * @author hxh * @description /创建查询Query * @date 18:24 2021/10/13 * @param: packageName **/ public QueryVO createQuery(String packageName) throws Exception { //创建Query String path = getDirPath(packageName); QueryVO queryVO = new QueryVO.Builder().setEntity(config.getEntity()).setPackageName(packageName).build(); String result = GeneraterUtil.createQuery(queryVO); saveFile(result, new File(path, queryVO.getClassName() + ".java").getAbsolutePath()); return queryVO; } /** * @return org.yeshi.utils.generater.vo.service.ServiceInfoVO * @author hxh * @description 自动生成服务 * @date 18:23 2021/10/13 * @param: interPackageName * @param: implPackageName * @param: query * @param: dao **/ public ServiceInfoVO createService(ClassInfo dao) throws Exception { if (config.getService() == null) { throw new Exception("尚未配置service"); } if (config.getDao() == null && dao == null) { throw new Exception("请在xml中配置Dao,或者传入Dao信息"); } if (dao == null) { if (StringUtil.isNullOrEmpty(config.getDao().getMapperDir())) { String daoName = NameUtil.getDefaultDaoName(config.getEntity()); dao = new ClassInfo(daoName, config.getDao().getPkg() + "." + daoName); } else { String daoName = NameUtil.getDefaultMapperName(config.getEntity()); dao = new ClassInfo(daoName, config.getDao().getPkg() + "." + daoName); } } if (config.getService().getException() != null) { if (config.getService().getException().getBase() != null) { createException(config.getService().getException().getBase()); } else { createException(ClassInfo.create(Exception.class)); } } QueryVO queryVO = createQuery(config.getService().getQueryPackage()); ClassInfo query = new ClassInfo(queryVO.getClassName(), queryVO.getPackageName() + "." + queryVO.getClassName()); //创建service接口 String serviceName = NameUtil.getDefaultServiceName(config.getEntity()); String path = getDirPath(config.getService().getInterPackage()); ServiceInfoVO vo = new ServiceInfoVO.Builder() .setPackageName(config.getService().getInterPackage()) .setEntity(config.getEntity()) .setQuery(query) .setService(new ClassInfo(serviceName, config.getService().getInterPackage() + "." + serviceName)) .build(); String result = GeneraterUtil.createService(vo); saveFile(result, new File(path, serviceName + ".java").getAbsolutePath()); //创建service实现 String serviceImplName = NameUtil.getDefaultServiceImplName(config.getEntity()); path = getDirPath(config.getService().getImplPackage()); ServiceImplInfoVO implVO = new ServiceImplInfoVO.Builder() .setDao(dao) .setPackageName(config.getService().getImplPackage()) .setEntity(config.getEntity()) .setQuery(query) .setService(new ClassInfo(serviceName, config.getService().getInterPackage() + "." + serviceName)) .setDaoQuery(new ClassInfo("DaoQuery", dao.getClazz() + ".DaoQuery")) .build(); result = GeneraterUtil.createServiceImpl(implVO); saveFile(result, new File(path, serviceImplName + ".java").getAbsolutePath()); return vo; } /** * @return void * @author hxh * @description 自动生成管理模块 * @date 18:23 2021/10/13 * @param: controllerPackage * @param: htmlDir * @param: query * @param: service **/ public void createAdmin(ClassInfo query, ClassInfo service) throws Exception { if (config.getAdmin() == null) { throw new Exception("尚未配置admin"); } if (service == null) { if (config.getService() == null) { throw new Exception("请传入service或者在xml中配置service"); } String serviceName = NameUtil.getDefaultServiceName(config.getEntity()); service = new ClassInfo(serviceName, config.getService().getInterPackage() + "." + serviceName); } if (query == null) { if (config.getService() == null) { throw new Exception("请传入service或者在xml中配置service"); } String queryName = NameUtil.getDefaultQueryName(config.getEntity()); query = new ClassInfo(queryName, config.getService().getQueryPackage() + "." + queryName); } AdminVOVO adminVOVO = null; if (config.getAdmin().getController().getAdminVOPackage() != null) { adminVOVO = createAdminVO(); } String path = getDirPath(config.getAdmin().getController().getPkg()); AdminGeneraterInfo generaterInfo = new AdminGeneraterInfo.Builder().setEntityClass(config.getEntity()).setAdminData(config.getAdmin()).build(); AdminControllerInfoVO vo = new AdminControllerInfoVO.Builder().setAdminVO(adminVOVO == null ? null : new ClassInfo(adminVOVO.getName(), adminVOVO.getPackageName() + "." + adminVOVO.getName())).setAdminInfo(generaterInfo).setPackageName(generaterInfo.getControllerData().getPkg()).setQueryVO(query).setService(service).build(); String controllerName = vo.getControllerName(); //生成controller String result = GeneraterUtil.createAdminController(vo); saveFile(result, new File(path, controllerName + ".java").getAbsolutePath()); if (!new File(generaterInfo.getPageDir()).exists()) { new File(generaterInfo.getPageDir()).mkdirs(); } String htmlNamePrefix = getHtmlNameFromEntityName(config.getEntity().getSimpleName()); if (generaterInfo.getShowDataList() != null && generaterInfo.getShowDataList().size() > 0) { result = GeneraterUtil.createAdminPageForList(generaterInfo); saveFile(result, new File(generaterInfo.getPageDir(), generaterInfo.getListFileName() != null ? generaterInfo.getListFileName() : htmlNamePrefix + "_list.html").getAbsolutePath()); } if (config.getAdmin().getPages().getAdd() != null && generaterInfo.getAddFormRows() != null && generaterInfo.getAddFormRows().size() > 0) { result = GeneraterUtil.createAdminPageForAdd(generaterInfo); saveFile(result, new File(generaterInfo.getPageDir(), generaterInfo.getAddFileName() != null ? generaterInfo.getAddFileName() : htmlNamePrefix + "_add.html").getAbsolutePath()); } if (config.getAdmin().getPages().getUpdate() != null && generaterInfo.getUpdateFormRows() != null && generaterInfo.getUpdateFormRows().size() > 0) { result = GeneraterUtil.createAdminPageForUpdate(generaterInfo); saveFile(result, new File(generaterInfo.getPageDir(), generaterInfo.getUpdateFileName() != null ? generaterInfo.getUpdateFileName() : htmlNamePrefix + "_update.html").getAbsolutePath()); } } /** * @return void * @author hxh * @description 创建整个功能模块 * @date 18:19 2021/10/13 * @param: daoParams * @param: serviceParams * @param: adminParams **/ public void createWholeFunction() throws Exception { Annotation[] as = config.getEntity().getAnnotations(); int type = -1; for (Annotation a : as) { if (a instanceof Table) { type = 1; break; } else if (a instanceof Document) { type = 2; break; } } if (type == -1) { throw new Exception("实体未配置mybatis或者mongodb"); } createDao(); createService(null); createAdmin(null, null); } /** * @return java.lang.String * @author hxh * @description 通过entity的名称获取html文件的前缀名称 * @date 18:25 2021/10/13 * @param: entityName **/ private static String getHtmlNameFromEntityName(String entityName) { StringBuffer sb = new StringBuffer(); StringBuffer buffer = null; for (int i = 0; i < entityName.length(); i++) { if (entityName.charAt(i) >= 65 && entityName.charAt(i) <= 90) { //大写字母 if (buffer == null) buffer = new StringBuffer(); buffer.append(entityName.charAt(i)); } else { if (buffer != null) { sb.append("_" + buffer.toString().toLowerCase()); buffer = null; } sb.append(entityName.charAt(i)); } } if (buffer != null) { sb.append("_" + buffer.toString().toLowerCase()); } if (sb.charAt(0) == '_') sb = sb.replace(0, 1, ""); return sb.toString(); } public static void main(String[] args) throws Exception { // System.out.print(getHtmlNameFromEntityName("TTestEEntity")); // AdminVOVO vo = new AdminVOVO.Builder().setEntity(TestEntity3.class).setPackageName("test.vo.admin").build(); // String result = GeneraterUtil.createAdminVO(vo); // System.out.println(result); } }