| | |
| | | import com.doumee.config.Jwt.JwtTokenUtil; |
| | | import com.doumee.core.model.LoginUserInfo; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * Controlleråºç±» |
| | |
| | | return obj != null ? (Integer) obj : null; |
| | | } |
| | | |
| | | public List<Integer> getIdList(String ids){ |
| | | if(StringUtils.isBlank(ids)){ |
| | | return null; |
| | | } |
| | | String [] idArray = ids.split(","); |
| | | List<Integer> idList = new ArrayList<>(); |
| | | for (String id : idArray) { |
| | | try { |
| | | idList.add(Integer.valueOf(id)); |
| | | }catch (Exception e){ |
| | | |
| | | } |
| | | } |
| | | return idList; |
| | | } |
| | | } |
| | |
| | | |
| | | // æ¼ç¤ºä¾åï¼æ§è¡ main æ¹æ³æ§å¶å°è¾å
¥æ¨¡å表åå车èªå¨çæå¯¹åºé¡¹ç®ç®å½ä¸ |
| | | public class CodeGenerator { |
| | | public static String packName ="com.doumee"; |
| | | public static String packName ="com.doumee"; |
| | | // 设置è¾åºç®å½ |
| | | public static String rootFileDir = "D:/code/idea/initCode/"; |
| | | public static String outputDirEntity =rootFileDir+"src/main/java/com/doumee/dao/business/model/"; // è¾åºçä»£ç æä»¶å¤¹è·¯å¾ |
| | | public static String outputDirMapper = rootFileDir+"src/main/java/com/doumee/dao/business/"; // è¾åºçä»£ç æä»¶å¤¹è·¯å¾ |
| | | public static String outputDirService =rootFileDir+"src/main/java/com/doumee/service/business/"; // è¾åºçä»£ç æä»¶å¤¹è·¯å¾ |
| | | public static String outputDirServiceImpl = rootFileDir+"src/main/java/com/doumee/service/business/impl/"; // è¾åºçä»£ç æä»¶å¤¹è·¯å¾ |
| | | public static String outputDirController =rootFileDir+"src/main/java/com/doumee/api/business/"; // è¾åºçä»£ç æä»¶å¤¹è·¯å¾ |
| | | public static String outputDirDb =rootFileDir+"db/"; // è¾åºçä»£ç æä»¶å¤¹è·¯å¾ |
| | | public static void main(String[] args) { |
| | | // é
ç½®æ°æ®åºè¿æ¥ |
| | | String url = "jdbc:mysql://192.168.0.211:3306/dmmall_full"; |
| | | String username = "root"; |
| | | String password = "Doumee@168"; |
| | | String database = "dmmall_full"; |
| | | String tableName = "car_driver"; |
| | | // è·å表信æ¯å¹¶çæä»£ç |
| | | Map<String, Object> tableInfo = getTableInfo(url, username, password, database, tableName); |
| | | generateCode(tableInfo); |
| | | List<String> tables = new ArrayList<>(); |
| | | tables.add("member"); |
| | | tables.add("labels"); |
| | | tables.add("goods"); |
| | | tables.add("goodsorder"); |
| | | tables.add("integral"); |
| | | |
| | | for(String tableName : tables){ |
| | | // è·å表信æ¯å¹¶çæä»£ç |
| | | try { |
| | | System.out.println(tableName+"===================çæå¼å§==================="); |
| | | Map<String, Object> tableInfo = getTableInfo(url, username, password, database, tableName); |
| | | generateCode(tableInfo); |
| | | System.out.println(tableName+"===================çæç»æ==================="); |
| | | }catch (Exception e){ |
| | | System.err.println(tableName+"===================çæå¼å¸¸==================="+e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | public static Map<String, Object> getTableInfo(String url, String username, String password, String database, String tableName) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | |
| | | String tableComment = (String) tableInfo.get("tableComment"); |
| | | List<Map<String, String>> columns = (List<Map<String, String>>) tableInfo.get("columns"); |
| | | |
| | | String templateDir = "D:\\code\\idea\\doumee_code\\server\\src\\main\\resources\\templates"; // æ¨¡æ¿æä»¶çè·¯å¾ |
| | | Properties p = new Properties(); |
| | | // æå®æ¨¡æ¿çå è½½ä½ç½® |
| | | p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, templateDir); |
| | | // æå®è¾å
¥ç¼ç |
| | | p.setProperty(VelocityEngine.INPUT_ENCODING, "UTF-8"); |
| | | |
| | | p.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); |
| | | p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader"); |
| | | p.setProperty("velocimacro.library", "macro_library.vm"); // 设置å®åºæä»¶å |
| | | p.setProperty("velocimacro.library.autoreload", "true"); // å
许èªå¨éæ°å è½½å®åº |
| | | p.setProperty("velocimacro.permissions.allow.inline.to.replace.global", "true"); // å
许å
è宿¿æ¢å
¨å±å® |
| | | p.setProperty("velocimacro.library", "/templates/macro_library.vm"); // 设置å®åºæä»¶å |
| | | p.setProperty("velocimacro.library.autoreload", "true"); // å
许èªå¨éæ°å è½½å®åº |
| | | |
| | | // 设置 Velocity é
ç½® |
| | | VelocityEngine velocityEngine = new VelocityEngine(); |
| | | velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); |
| | | velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); |
| | | velocityEngine.init(); |
| | | |
| | | // é
置模æ¿ç®å½ |
| | | |
| | | |
| | | // 设置è¾åºç®å½ |
| | | String outputDir = "D:\\code\\idea\\doumee_code\\server\\src\\main\\java\\com\\testcom"; // è¾åºçä»£ç æä»¶å¤¹è·¯å¾ |
| | | |
| | | String baseName = entityName.substring(0, 1).toUpperCase() + entityName.substring(1); |
| | | // å建 Velocity ä¸ä¸æ |
| | | Map<String,String> pack = new HashMap<>(); |
| | | pack.put("Base",packName); |
| | | pack.put("Entity",packName+".dao.businees.model"); |
| | | pack.put("Controller",packName+".api.businees"); |
| | | pack.put("Mapper",packName+".dao.businees"); |
| | | pack.put("Service",packName+".service.businees"); |
| | | pack.put("ServiceImpl",packName+".service.businees.impl"); |
| | | pack.put("Entity",packName+".dao.business.model"); |
| | | pack.put("Controller",packName+".api.business"); |
| | | pack.put("Mapper",packName+".dao.business"); |
| | | pack.put("Service",packName+".service.business"); |
| | | pack.put("ServiceImpl",packName+".service.business.impl"); |
| | | VelocityContext context = new VelocityContext(); |
| | | context.put("package",pack); // 设置å
è·¯å¾ |
| | | context.put("entityName", baseName); |
| | |
| | | context.put("nowDate", DateUtil.getPlusTime2(new Date())); |
| | | |
| | | // çæ Entity æä»¶ |
| | | generateFile(velocityEngine, context, "entity.vm", baseName, outputDir + "/dao/businees/model/" , ".java"); |
| | | generateFile(velocityEngine, context, "entity.vm", baseName, outputDirEntity , ".java"); |
| | | |
| | | // çæ Mapper æä»¶ |
| | | generateFile(velocityEngine, context, "mapper.vm", baseName, outputDir + "/dao/businees/" , "Mapper.java"); |
| | | generateFile(velocityEngine, context, "mapper.vm", baseName, outputDirMapper , "Mapper.java"); |
| | | |
| | | // çæ Service æ¥å£æä»¶ |
| | | generateFile(velocityEngine, context, "service.vm", baseName, outputDir + "/service/businees/" , "Service.java"); |
| | | generateFile(velocityEngine, context, "service.vm", baseName, outputDirService , "Service.java"); |
| | | |
| | | // çæ ServiceImpl æä»¶ |
| | | generateFile(velocityEngine, context, "serviceImpl.vm", baseName, outputDir + "/service/businees/impl/" , "ServiceImpl.java"); |
| | | generateFile(velocityEngine, context, "serviceImpl.vm", baseName, outputDirServiceImpl + "" , "ServiceImpl.java"); |
| | | |
| | | // çæ Controller æä»¶ |
| | | generateFile(velocityEngine, context, "controller.vm", baseName, outputDir + "/api/businees/", "Controller.java"); |
| | | generateFile(velocityEngine, context, "controller.vm", baseName, outputDirController , "Controller.java"); |
| | | generateFile(velocityEngine, context, "db.vm", baseName, outputDirDb , "Permissions.sql"); |
| | | } |
| | | |
| | | private static void generateFile(VelocityEngine velocityEngine, VelocityContext context, String templateName, String baseName,String outputFilePath,String endName) { |
| | |
| | | |
| | | // å建æä»¶è¾åºæµ |
| | | try{ |
| | | |
| | | System.out.println(outputFilePath+baseName+endName+"===================çæå¼å§==================="); |
| | | File f = new File(outputFilePath+baseName+endName); |
| | | if(f.isFile()){ |
| | | f.delete(); |
| | |
| | | template.merge(context, writer); |
| | | writer.flush(); |
| | | writer.close(); |
| | | System.out.println(outputFilePath+baseName+endName+"===================çæç»æ==================="); |
| | | } catch (IOException e) { |
| | | System.err.println(outputFilePath+baseName+endName+"===================çæå¼å¸¸==================="); |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | |
| | | @PostMapping("/exportExcel") |
| | | @RequiresPermissions("business:${entityNameLowerFull}:exportExcel") |
| | | public void exportExcel (@RequestBody PageWrap<${entityName}> pageWrap, HttpServletResponse response) { |
| | | ExcelExporter.build(${entityName}.class).export(${entityNameLower}Service.findPage(pageWrap).getRecords(), "æå°_ä½ä¸æä½åå²è¡¨", response); |
| | | ExcelExporter.build(${entityName}.class).export(${entityNameLower}Service.findPage(pageWrap).getRecords(), "${tableComment}", response); |
| | | } |
| | | |
| | | @ApiOperation("æ ¹æ®IDæ¥è¯¢") |
| ¶Ô±ÈÐÂÎļþ |
| | |
| | | INSERT INTO `SYSTEM_PERMISSION`(`CODE`, `NAME`, `REMARK`, `FIXED`, `CREATE_USER`, `CREATE_TIME`, `UPDATE_USER`, `UPDATE_TIME`, `DELETED`) VALUES ('business:${entityNameLowerFull}:create', 'æ°å»º${tableComment}', '', 0, 1, CURRENT_TIMESTAMP, NULL, NULL, 0); |
| | | INSERT INTO `SYSTEM_PERMISSION`(`CODE`, `NAME`, `REMARK`, `FIXED`, `CREATE_USER`, `CREATE_TIME`, `UPDATE_USER`, `UPDATE_TIME`, `DELETED`) VALUES ('business:${entityNameLowerFull}:delete', 'å é¤${tableComment}', '', 0, 1, CURRENT_TIMESTAMP, NULL, NULL, 0); |
| | | INSERT INTO `SYSTEM_PERMISSION`(`CODE`, `NAME`, `REMARK`, `FIXED`, `CREATE_USER`, `CREATE_TIME`, `UPDATE_USER`, `UPDATE_TIME`, `DELETED`) VALUES ('business:${entityNameLowerFull}:update', 'ä¿®æ¹${tableComment}', '', 0, 1, CURRENT_TIMESTAMP, NULL, NULL, 0); |
| | | INSERT INTO `SYSTEM_PERMISSION`(`CODE`, `NAME`, `REMARK`, `FIXED`, `CREATE_USER`, `CREATE_TIME`, `UPDATE_USER`, `UPDATE_TIME`, `DELETED`) VALUES ('business:${entityNameLowerFull}:query', 'æ¥è¯¢${tableComment}', '', 0, 1, CURRENT_TIMESTAMP, NULL, NULL, 0); |
| | | INSERT INTO `SYSTEM_PERMISSION`(`CODE`, `NAME`, `REMARK`, `FIXED`, `CREATE_USER`, `CREATE_TIME`, `UPDATE_USER`, `UPDATE_TIME`, `DELETED`) VALUES ('business:${entityNameLowerFull}:exportExcel', '导åº${tableComment}(Excel)', '', 0, 1, CURRENT_TIMESTAMP, NULL, NULL, 0); |
| | | |