日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

一些记录

發(fā)布時(shí)間:2025/4/9 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一些记录 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

代碼生成器

根據(jù)Mybatis generator(MBG)結(jié)合freemarker寫(xiě)了一個(gè)代碼生成器,可以直接生成Controller,Service,Dao,bean,以及對(duì)應(yīng)的mapper.xml文件。
準(zhǔn)備材料: 幾個(gè)模板,mybatis-generator-core-1.3.2.jar。
注意事項(xiàng):各個(gè)公司的文件結(jié)構(gòu)可能不盡相同,所以請(qǐng)根據(jù)實(shí)際情況自行采用。

public class MapperGeneratorTest {/****/private static final String BASEPATH = "C:\\tmp\\generator\\";// 獲取項(xiàng)目路徑private static final String ROOTPATH = System.getProperty("user.dir") + "/src/main";private final String WEBPATH = "這里寫(xiě)上你生成文件的上一級(jí)目錄";/*** 一鍵生成*/@Testpublic void generateTest() {// 要生成的beanString[] entities = new String[]{"value"};// 對(duì)應(yīng)的表String[] tables = new String[]{"value"};// Mapper.java文件路徑String path = ROOTPATH + WEBPATH + "*/dao/mapper/?Mapper.java";// baseMapper全限定名String baseMapperQfName = "這里寫(xiě)上baseMapper的全限定名";generate(entities, tables, path, baseMapperQfName);}/*** 生成Controller,Service, Mapper,xml,bean及其實(shí)現(xiàn)** @param entities bean數(shù)組* @param tables 生成的表數(shù)組* @param mapperPath *的位置代表entity, ?代表對(duì)于的大寫(xiě)格式* @param baseMapperQfName baseMapper全限定名*/private void generate(String[] entities, String[] tables, String mapperPath, String baseMapperQfName) {for (int i = 0; i < entities.length; i++) {System.err.println("==================開(kāi)始執(zhí)行第" + (i + 1) + "次=====================");String entity = entities[i];// 生成好對(duì)應(yīng)的xml文件generateForOne(entity, tables[i]);// 如果basePath不存在的話則創(chuàng)建File file = new File(BASEPATH);if (!file.exists()) {file.mkdirs();}// 首先寫(xiě)好要運(yùn)行的bat文件writeBat(entity, BASEPATH + entity + ".bat");String path = mapperPath.replace("*", entity).replace("?", StringUtil.capitalize(entity));processMapper(path, entity, baseMapperQfName, !new File(path).exists());generateCtrSerDao(entity);System.err.println("==================結(jié)束執(zhí)行第" + (i + 1) + "次=====================");}}/*** 執(zhí)行bat命令** @param batPath bat文件路徑*/private Process runCmd(String batPath) {Process process = null;try {process = Runtime.getRuntime().exec("cmd /c C: && start " + batPath); // InputStream input = process.getInputStream(); // BufferedReader reader = new BufferedReader(new InputStreamReader(input)); // String szline; // while ((szline = reader.readLine())!= null) { // System.out.println(szline); // } // reader.close();process.waitFor();if (process.exitValue() == 0) {System.err.println("執(zhí)行bat命令成功");}// 執(zhí)行成功之后再destroyprocess.destroy();// 執(zhí)行完畢,關(guān)閉本窗口Runtime.getRuntime().exec("cmd.exe /C start wmic process where name='cmd.exe' call terminate");} catch (Exception e) {e.printStackTrace();}return process;}/*** 將命令寫(xiě)入bat文件** @param entityName 實(shí)體名字* @param path 生成bat的路徑*/private void writeBat(String entityName, String path) {String cmd = "java -jar C:\\tmp\\mybatis-generator-core-1.3.2.jar -configfile C:\\tmp\\xml\\" + entityName + ".xml -overwrite";FileOutputStream fileOutputStream = null;try {fileOutputStream = new FileOutputStream(new File(path));fileOutputStream.write(cmd.getBytes());} catch (IOException e) {e.printStackTrace();} finally {StreamUtil.close(fileOutputStream);}}// 根據(jù)模板生成對(duì)應(yīng)的xmlprivate void generateForOne(String entityName, String tableName) {String outputPath = "C:\\tmp\\xml";if (!new File(outputPath).exists()) {boolean createDirectoryFlag = new File(outputPath).mkdirs();if (!createDirectoryFlag) {System.err.println("========Danger: mkdirs() error!=========");}}// 獲取對(duì)應(yīng)的版本配置Configuration configuration = new Configuration(Configuration.VERSION_2_3_21);String ftlPath = ROOTPATH + "\\resources\\freemarker";File ftlFilePath = new File(ftlPath);try {// 設(shè)置模板文件上級(jí)路徑configuration.setDirectoryForTemplateLoading(ftlFilePath);configuration.setDefaultEncoding("UTF-8");// 設(shè)置異常處理器configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);// 構(gòu)建數(shù)據(jù)模型Map<String, Object> dataMap = new HashMap<>();dataMap.put("entity", entityName);dataMap.put("domain", StringUtil.capitalize(entityName));// 解決不規(guī)范命名問(wèn)題dataMap.put("table", tableName);// 獲取模板Template generatorTemplate = configuration.getTemplate("mapper-generator.ftl");// 設(shè)置輸出路徑FileOutputStream fileOutputStream = new FileOutputStream(outputPath + File.separator + entityName + ".xml");OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);generatorTemplate.process(dataMap, outputStreamWriter);} catch (IOException | TemplateException e) {e.printStackTrace();}}/*** 處理mapper.java文件** @param path mapper.java所在路徑* @param entityName* @param baseMapperQfName* @param flag 是否是第一次生成*/private void processMapper(String path, String entityName, String baseMapperQfName, boolean flag) {String backFileName = path + "_backup";if (!flag) {// 首先備份原來(lái)的mapper文件FileUtil.copyFile(path, backFileName);}Process process = runCmd(BASEPATH + entityName + ".bat");try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}String mapperContent = getMapperContent(path, entityName, baseMapperQfName, flag); // System.err.println("生成的Mapper.java文件內(nèi)容為: \n" + mapperContent);// 將修改后的mapper寫(xiě)入文件FileUtil.writeToFile(path, mapperContent);File backFile = new File(backFileName);if (backFile.exists()) {boolean delete = backFile.delete();if (!delete) {System.err.println("刪除" + backFileName + "失敗!");}}}/*** 第一次生成, 返回自動(dòng)產(chǎn)生Mapper文件中的內(nèi)容,* 非首次生成, 保存原來(lái)Mapper.java文件中的方法, 如果未繼承BaseMapper, 則處理為繼承BaseMapper** @param path mapper.java 路徑* @param entityName beanName* @param baseMapperQfName baseMapper全限定名* @param flag 是否是第一次生成* @return 返回生成的Mapper中的String內(nèi)容*/private String getMapperContent(String path, String entityName, String baseMapperQfName, boolean flag) {// Mapper.java文件路徑String domainName = StringUtil.capitalize(entityName);String url = path + (flag ? "" : "_backup");String separator = System.getProperty("line.separator");// 存儲(chǔ)改動(dòng)過(guò)后的stringStringBuilder buffer = new StringBuilder();FileInputStream fis = null;InputStreamReader reader = null;BufferedReader bufferedReader = null;try {fis = new FileInputStream(new File(url));reader = new InputStreamReader(fis);bufferedReader = new BufferedReader(reader);String str;while ((str = bufferedReader.readLine()) != null) {// 已經(jīng)import了BaseMapper跳過(guò)if (str.contains("mapper.BaseMapper")) continue;// 如果是第一次生成的話if (str.contains("interface")) {buffer.append("import ").append(baseMapperQfName).append(";");// 插入系統(tǒng)的換行符buffer.append(separator);// 格式處理, import 和interface之間添加空行buffer.append(separator);// 處理存在Mapper文件, 但是沒(méi)有抽取繼承的情況if (!str.contains("extends BaseMapper")) {String changedText = str.replace(domainName + "Mapper", domainName + "Mapper extends BaseMapper<" + domainName + ">");buffer.append(changedText);}if (flag) {buffer.append(separator);buffer.append("}");break;}}buffer.append(str);buffer.append(separator);}} catch (IOException e) {e.printStackTrace();} finally {StreamUtil.close(bufferedReader, reader, fis);}return buffer.toString();}/*** 生成controller,service,dao** @param entityName beanName*/private void generateCtrSerDao(String entityName) {// 所有模板String[] templateNames = new String[]{"BeanController.ftl", "IBeanService.ftl", "BeanServiceImpl.ftl", "IBeanDao.ftl", "BeanDaoImpl.ftl"};String[] paths = new String[]{"controller/", "service", "service/impl", "dao", "dao/impl"};String[] names = new String[]{"Controller", "Service", "ServiceImpl", "Dao", "DaoImpl"};String domainName = StringUtil.capitalize(entityName);// 獲取對(duì)應(yīng)的版本配置Configuration configuration = new Configuration(Configuration.VERSION_2_3_21);String ftlPath = ROOTPATH + "\\resources\\freemarker";File ftlFilePath = new File(ftlPath);FileOutputStream fileOutputStream = null;OutputStreamWriter outputStreamWriter = null;try {// 設(shè)置模板文件上級(jí)路徑configuration.setDirectoryForTemplateLoading(ftlFilePath);configuration.setDefaultEncoding("UTF-8");// 設(shè)置異常處理器configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);// 構(gòu)建數(shù)據(jù)模型Map<String, Object> dataMap = new HashMap<>();dataMap.put("entity", entityName);dataMap.put("domain", domainName);for (int i = 0; i < templateNames.length; i++) {String currFtl = templateNames[i];// 獲取模板Template generatorTemplate = configuration.getTemplate(currFtl);// 設(shè)置輸出路徑String outputPath = ROOTPATH + WEBPATH + entityName + "/" + paths[i] + "/" + (i % 2 == 0 ? (domainName + names[i]) : ("I" + domainName + names[i])) + ".java";File file = new File(outputPath);// 判斷一下是否對(duì)應(yīng)的文件是否已經(jīng)存在,存在的話,不進(jìn)行替換if (file.exists()) {System.err.println("[" + outputPath + "]" + "已經(jīng)存在, 不替換");continue;}File parentFile = file.getParentFile();if (!parentFile.exists()) {boolean createDirectoryFlag = parentFile.mkdirs();if (!createDirectoryFlag) {System.err.println("========Danger: mkdirs() error!=========");}}fileOutputStream = new FileOutputStream(outputPath);outputStreamWriter = new OutputStreamWriter(fileOutputStream);generatorTemplate.process(dataMap, outputStreamWriter);System.err.println("-----" + outputPath + "生成成功" + "-----");}} catch (IOException | TemplateException e) {e.printStackTrace();} finally {// 關(guān)閉流StreamUtil.close(outputStreamWriter, fileOutputStream);}}}

時(shí)間展示處理

這里僅處理兩種不同的日期顯示格式,如果有需要執(zhí)行配置即可

public class JsonObjectMapper extends ObjectMapper {private static final long serialVersionUID = 1L;public JsonObjectMapper() {super();// 空值處理為空串 this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {@Overridepublic void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException {jg.writeString("");}});//設(shè)置JSON時(shí)間格式SimpleModule module = new SimpleModule();module.addSerializer(Date.class, new JsonSerializer<Date>() {@Overridepublic void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String zero = "00:00:00";if (zero.equals(sdf.format(value).split(" ")[1])) {sdf = new SimpleDateFormat("yyyy-MM-dd");}jgen.writeString(sdf.format(value));}});this.registerModule(module);}}

同時(shí)在spingmvc配置文件中配置:

<mvc:annotation-driven><mvc:message-converters><bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="objectMapper"><bean class="這里寫(xiě)JsonObjectMapper全限定名"></bean></property></bean></mvc:message-converters></mvc:annotation-driven>

轉(zhuǎn)載于:https://www.cnblogs.com/studentytj/p/9328654.html

總結(jié)

以上是生活随笔為你收集整理的一些记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产一级不卡毛片 | www.香蕉视频在线观看 | 九九爱国产 | 在线观看欧美视频 | 色眯眯影院 | 男男车车的车车网站w98免费 | 97人妻精品一区二区免费 | 国产3p在线播放 | 啪啪免费小视频 | 成人毛片大全 | 96超碰在线| 黄色a一级视频 | a级片免费视频 | 四虎影院一区二区 | 欧美一线天 | 国产一区二区三区 | 亚洲男女一区二区三区 | 91精品国产综合久久精品 | 顶臀精品视频www | 97久久久久久 | 羞羞的视频在线观看 | 日韩欧美国产精品 | 亚洲啊啊| 少妇人妻真实偷人精品视频 | 亚洲国产精品福利 | 99精品视频一区二区 | 天天射寡妇射 | 九九黄色片| 精品国产无码在线观看 | 欧美日韩1| 成人三级视频 | 日韩黄大片 | 你懂的视频网站 | 欧美激情一区二区视频 | 久久久久久黄色 | 欧美性生交大片免费看app麻豆 | 欧美日韩久久 | 国家队动漫免费观看在线观看晨光 | 欧美大片高清 | 中文字幕在线亚洲 | 色诱久久av| 日韩av综合在线 | 免费一级做a爰片久久毛片潮 | 日韩大片在线免费观看 | 日韩美女免费视频 | 欧美日韩国产精品综合 | 熊出没之冬日乐翻天免费高清观看 | av首页在线| 99精品欧美一区二区三区综合在线 | 国产一区在线不卡 | 欧美精品三级 | 久久久久久亚洲精品中文字幕 | 国产真实交换夫妇视频 | 精品久久人人 | 欲色综合| caoprom超碰 | 一久久久久 | 久久久999国产 | 免费看亚洲 | 免费性爱视频 | 国产午夜麻豆影院在线观看 | 青青草成人网 | 成人爽站w47pw | 久久九九热视频 | 欧美日韩123区 | 久久丫丫 | 国产一二三在线视频 | 下面一进一出好爽视频 | 日韩午夜精品视频 | www在线| 肉大榛一进一出免费视频 | 黄色录象片 | 欧洲精品在线观看 | 99re在线视频 | 天天操天天射天天 | 成人勉费视频 | 亚洲美女一区二区三区 | 欧美性生活视频 | 色先锋av资源 | 国产偷拍一区二区 | 国产又猛又粗 | 久草婷婷| 亚洲一区在线免费 | 日本成人在线播放 | 综合五月婷婷 | 亚洲国产天堂av | 草草地址线路①屁屁影院成人 | 亚洲av午夜精品一区二区三区 | 人人插人人 | 欧美v日韩| 亚洲精品福利视频 | 日本一区二区黄色 | wwwxxxx国产 | 日本美女动态图 | 青青草自拍偷拍 | 福利一区二区在线观看 | 无套日出白浆 | wwwjavhd| 老湿影院av |