From cd790833c15c48aa55b138ca43ba90047f584ef3 Mon Sep 17 00:00:00 2001 From: zhangmeng <1334717033@qq.com> Date: Tue, 21 Feb 2023 17:52:37 +0800 Subject: [PATCH] =?UTF-8?q?2023=E5=B9=B42=E6=9C=8821=E6=97=A517:52:03=20?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=A1=A8=E5=90=8D=E8=8E=B7=E5=8F=96=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tools/controller/HomeController.java | 21 + .../controller/MySQLCodeGenController.java | 108 ++++++ .../tools/controller/SqlToolsController.java | 299 +++++++++++++++ .../com/zhangmeng/tools/dto/BeanField.java | 69 ++++ .../zhangmeng/tools/dto/GenerateDetail.java | 29 ++ .../zhangmeng/tools/dto/GenerateInput.java | 155 ++++++++ .../com/zhangmeng/tools/utils/FileUtil.java | 311 +++++++++++++++ .../zhangmeng/tools/utils/ResourcesUtils.java | 30 ++ .../zhangmeng/tools/utils/TemplateUtil.java | 360 ++++++++++++++++++ src/main/resources/fxml/home.fxml | 6 + src/main/resources/fxml/mysql-code-gen.fxml | 14 + src/main/resources/fxml/sql-tools.fxml | 65 ++++ 12 files changed, 1467 insertions(+) create mode 100644 src/main/java/com/zhangmeng/tools/controller/MySQLCodeGenController.java create mode 100644 src/main/java/com/zhangmeng/tools/controller/SqlToolsController.java create mode 100644 src/main/java/com/zhangmeng/tools/dto/BeanField.java create mode 100644 src/main/java/com/zhangmeng/tools/dto/GenerateDetail.java create mode 100644 src/main/java/com/zhangmeng/tools/dto/GenerateInput.java create mode 100644 src/main/java/com/zhangmeng/tools/utils/FileUtil.java create mode 100644 src/main/java/com/zhangmeng/tools/utils/TemplateUtil.java create mode 100644 src/main/resources/fxml/mysql-code-gen.fxml create mode 100644 src/main/resources/fxml/sql-tools.fxml diff --git a/src/main/java/com/zhangmeng/tools/controller/HomeController.java b/src/main/java/com/zhangmeng/tools/controller/HomeController.java index 3a553fb..b4f0901 100644 --- a/src/main/java/com/zhangmeng/tools/controller/HomeController.java +++ b/src/main/java/com/zhangmeng/tools/controller/HomeController.java @@ -129,6 +129,27 @@ public class HomeController implements Serializable { load_codec_tools(3); } + @FXML + private void sql_code_gen_menu_item(){ + load_sql_tools(0); + } + + public void load_sql_tools(int index){ + AnchorPane fx = null; + try { + fx = FXMLLoader.load(ResourcesUtils.getResource("sql-tools")); + } catch (IOException e) { + e.printStackTrace(); + } + + Scene scene = new Scene(fx); + Stage stage = (Stage) splitPane.getScene().getWindow(); + stage.setScene(scene); + + ListView listView = (ListView) fx.lookup("#listView"); + listView.getSelectionModel().select(index); + } + public void load_codec_tools(int index){ AnchorPane fx = null; try { diff --git a/src/main/java/com/zhangmeng/tools/controller/MySQLCodeGenController.java b/src/main/java/com/zhangmeng/tools/controller/MySQLCodeGenController.java new file mode 100644 index 0000000..e29d798 --- /dev/null +++ b/src/main/java/com/zhangmeng/tools/controller/MySQLCodeGenController.java @@ -0,0 +1,108 @@ +package com.zhangmeng.tools.controller; + +import com.google.common.collect.Maps; +import com.zhangmeng.tools.dto.BeanField; +import com.zhangmeng.tools.dto.GenerateDetail; +import com.zhangmeng.tools.utils.TemplateUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.util.CollectionUtils; + +import java.math.BigDecimal; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Date; +import java.util.List; +import java.util.Map; + +/** + * @author : 芊芊墨客 + * @version : 1.0 + * @date : 2023-02-21 16:34 + */ +@Slf4j +public class MySQLCodeGenController { + + private JdbcTemplate jdbcTemplate; + + /** + * mysql类型与java类型部分对应关系 + */ + private static Map map = Maps.newHashMap(); + + static { + map.put("int", Integer.class.getSimpleName()); + map.put("tinyint", Integer.class.getSimpleName()); + map.put("double", Double.class.getSimpleName()); + map.put("float", Float.class.getSimpleName()); + map.put("decimal", BigDecimal.class.getSimpleName()); + map.put("date", Date.class.getSimpleName()); + map.put("timestamp", Date.class.getSimpleName()); + map.put("datetime", Date.class.getSimpleName()); + map.put("varchar", String.class.getSimpleName()); + map.put("text", String.class.getSimpleName()); + map.put("longtext", String.class.getSimpleName()); + map.put("bit", Boolean.class.getSimpleName()); + map.put("bigint", Long.class.getSimpleName()); + + } + + public JdbcTemplate getJdbcTemplate() { + if (jdbcTemplate == null) { + jdbcTemplate = new JdbcTemplate(); + } + return jdbcTemplate; + } + + private RowMapper beanFieldMapper = (rs, paramInt) -> { + BeanField beanField = new BeanField(); + beanField.setColumnName(rs.getString("column_name")); + beanField.setColumnType(rs.getString("data_type")); + beanField.setColumnComment(rs.getString("column_comment")); + beanField.setColumnDefault(rs.getString("column_default")); + return beanField; + }; + + public GenerateDetail generateByTableName(String tableName) { + GenerateDetail detail = new GenerateDetail(); + detail.setBeanName(upperFirstChar(tableName)); + List fields = listBeanField(tableName); + detail.setFields(fields); + + return detail; + } + + public List listBeanField(String tableName) { + getJdbcTemplate(); + List beanFields = jdbcTemplate.query( + "select column_name, data_type, column_comment, column_default FROM information_schema.columns WHERE table_name= ? and table_schema = (select database())", + new String[]{tableName}, beanFieldMapper); + if (CollectionUtils.isEmpty(beanFields)) { + throw new IllegalArgumentException("表" + tableName + "不存在"); + } + + beanFields.parallelStream().forEach(b -> { + b.setName(TemplateUtil.str2hump(b.getColumnName())); + String type = map.get(b.getColumnType()); + if (type == null) { + type = String.class.getSimpleName(); + } + b.setType(type); + if ("id".equals(b.getName())) { + b.setType(Long.class.getSimpleName()); + } + + b.setColumnDefault(b.getColumnDefault() == null ? "" : b.getColumnDefault()); + }); + + return beanFields; + } + + public String upperFirstChar(String string) { + String name = TemplateUtil.str2hump(string); + String firstChar = name.substring(0, 1); + name = name.replaceFirst(firstChar, firstChar.toUpperCase()); + return name; + } +} diff --git a/src/main/java/com/zhangmeng/tools/controller/SqlToolsController.java b/src/main/java/com/zhangmeng/tools/controller/SqlToolsController.java new file mode 100644 index 0000000..0d5fe24 --- /dev/null +++ b/src/main/java/com/zhangmeng/tools/controller/SqlToolsController.java @@ -0,0 +1,299 @@ +package com.zhangmeng.tools.controller; + +import com.zhangmeng.tools.utils.ImagePath; +import com.zhangmeng.tools.utils.ResourcesUtils; +import javafx.beans.property.SimpleDoubleProperty; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Label; +import javafx.scene.control.ListCell; +import javafx.scene.control.ListView; +import javafx.scene.control.SplitPane; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.HBox; +import javafx.scene.paint.Paint; +import javafx.scene.text.Font; +import javafx.stage.Stage; +import javafx.util.Callback; +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; +import java.util.Arrays; + +/** + * @author : 芊芊墨客 + * @version : 1.0 + * @date : 2023-02-21 16:23 + */ +@Slf4j +public class SqlToolsController { + + private SimpleDoubleProperty width = new SimpleDoubleProperty(0.0); + private SimpleDoubleProperty height = new SimpleDoubleProperty(0.0); + private AnchorPane root; + private AnchorPane mysql_code_gen; + private AnchorPane spring_security; + + public static final String color_cell = "#f4f4f4"; + + @FXML + private ListView listView; + + @FXML + private SplitPane splitPane; + + @FXML + public void md5_menu_item() { + + } + + @FXML + public void spring_security_menu_item() { + + } + + @FXML + public void video_menu_item() { + load_player(0); + } + + @FXML + public void music_menu_item() { + load_player(1); + } + + @FXML + public void vip_parser_menu_item() { + load_player(2); + } + + @FXML + public void hex_16(){ + load_small_tools(0); + } + + @FXML + private void hex_16_menu_item(){ + load_small_tools(0); + } + + @FXML + private void unicode_menu_item(){ + load_small_tools(1); + } + + @FXML + private void jwt_menu_item(){ + load_small_tools(2); + } + + @FXML + private void color_choose_menu_item(){ + load_small_tools(3); + } + + @FXML + public void base_62_menu_item(){ + load_codec_tools(0); + } + + @FXML + public void base_64_menu_item(){ + load_codec_tools(1); + } + + @FXML + public void base_32_menu_item(){ + load_codec_tools(2); + } + + @FXML + public void morse_coder_menu_item(){ + load_codec_tools(3); + } + + @FXML + public void sql_code_gen_menu_item(){ + boolean flag = false; + if (mysql_code_gen != null){ + flag = true; + } + mysql_code_gen(flag); + } + + public void load_codec_tools(int index){ + AnchorPane fx = null; + try { + fx = FXMLLoader.load(ResourcesUtils.getResource("codec-tools")); + } catch (IOException e) { + e.printStackTrace(); + } + + Scene scene = new Scene(fx); + Stage stage = (Stage) splitPane.getScene().getWindow(); + stage.setScene(scene); + + ListView listView = (ListView) fx.lookup("#listView"); + listView.getSelectionModel().select(index); + } + + public void load_small_tools(int index){ + AnchorPane fx = null; + try { + fx = FXMLLoader.load(ResourcesUtils.getResource("small-tools")); + } catch (IOException e) { + e.printStackTrace(); + } + + Scene scene = new Scene(fx); + Stage stage = (Stage) splitPane.getScene().getWindow(); + stage.setScene(scene); + + ListView listView = (ListView) fx.lookup("#listView"); + listView.getSelectionModel().select(index); + } + + public void load_player(int index) { + AnchorPane fx = null; + try { + fx = FXMLLoader.load(ResourcesUtils.getResource("player")); + } catch (IOException e) { + e.printStackTrace(); + } + + Scene scene = new Scene(fx); + Stage stage = (Stage) splitPane.getScene().getWindow(); + stage.setScene(scene); + + ListView listView = (ListView) fx.lookup("#listView"); + listView.getSelectionModel().select(index); + } + + @FXML + public void initialize() { + init(); + listView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { + if (newValue != null) { + boolean flag = false; + + if (newValue.getIndex() == 0) { + if (mysql_code_gen != null){ + flag = true; + } + mysql_code_gen(flag); + } + } + }); + } + + public static Image getImage(ResourcesUtils.SqlTools player){ + return switch (player){ + case MySql_Code_Generate -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5)); + }; + } + + public void init() { + ResourcesUtils.SqlTools[] values = ResourcesUtils.SqlTools.values(); + ObservableList list = FXCollections.observableArrayList(); + list.addAll(Arrays.asList(values)); + listView.setItems(list); + listView.setFixedCellSize(40); + listView.setCellFactory(new Callback<>() { + private int position; + @Override + public ListCell call(ListView playerListView) { + Label label = new Label(); + label.setPrefWidth(200); + ListCell listCell = new ListCell<>() { + @Override + protected void updateItem(ResourcesUtils.SqlTools player, boolean b) { + super.updateItem(player, b); + if (!b) { + HBox hBox = new HBox(25); + hBox.setAlignment(Pos.CENTER); + label.setText(player.getTitle()); + label.setTextFill(Paint.valueOf("#000000")); + Image im = getImage(player); + ImageView iv = new ImageView(im); + iv.setPreserveRatio(true); + iv.setFitWidth(15); + hBox.getChildren().add(iv); + + hBox.getChildren().add(label); + this.setGraphic(hBox); + } + this.setStyle("-fx-background-color: " + color_cell); + } + }; + + listCell.hoverProperty().addListener((observableValue, aBoolean, t1) -> { + if (t1 && !label.getText().equals("")) { + position = playerListView.getItems().indexOf(label.getText()); + label.setFont(new Font(16)); + playerListView.getFocusModel().focus(position); + listCell.setStyle("-fx-background-color: #369e7d"); + } else { + label.setPrefHeight(20); + label.setFont(new Font(13)); + listCell.setStyle("-fx-background-color: " + color_cell); + } + }); + + return listCell; + } + }); + + mysql_code_gen(false); + } + + private void mysql_code_gen(boolean flag) { + //默认选择第一个 + listView.getSelectionModel().select(0); + + if (!flag){ + try { + root = FXMLLoader.load(ResourcesUtils.getResource("mysql-code-gen")); + } catch (IOException e) { + e.printStackTrace(); + } + mysql_code_gen = root; + }else { + root = mysql_code_gen; + } + common_method(); + } + + private void common_method() { + splitPane.getItems().remove(1); + splitPane.getItems().add(1, root); + root.widthProperty().addListener((observable, oldValue, newValue) -> { + if (newValue != null) { + double width = splitPane.getWidth(); + SqlToolsController.this.width.set(width); + log.info("home:--->width:{}", width); + } + }); + root.heightProperty().addListener((observable, oldValue, newValue) -> { + if (newValue != null) { + double height = splitPane.getHeight(); + SqlToolsController.this.height.set(height); + log.info("home:--->height:{}", height); + } + }); + + this.width.addListener((observable, oldValue, newValue) -> { + SqlToolsController.this.root.setPrefWidth(newValue.doubleValue() - listView.getWidth()); + }); + + this.height.addListener((observable, oldValue, newValue) -> { + SqlToolsController.this.root.setPrefHeight(newValue.doubleValue() - listView.getHeight()); + }); + } + +} diff --git a/src/main/java/com/zhangmeng/tools/dto/BeanField.java b/src/main/java/com/zhangmeng/tools/dto/BeanField.java new file mode 100644 index 0000000..a284983 --- /dev/null +++ b/src/main/java/com/zhangmeng/tools/dto/BeanField.java @@ -0,0 +1,69 @@ +package com.zhangmeng.tools.dto; + +import java.io.Serializable; + +public class BeanField implements Serializable { + + private static final long serialVersionUID = 4279960350136806659L; + + private String columnName; + + private String columnType; + + private String columnComment; + + private String columnDefault; + + private String name; + + private String type; + + public String getColumnName() { + return columnName; + } + + public void setColumnName(String columnName) { + this.columnName = columnName; + } + + public String getColumnType() { + return columnType; + } + + public void setColumnType(String columnType) { + this.columnType = columnType; + } + + public String getColumnComment() { + return columnComment; + } + + public void setColumnComment(String columnComment) { + this.columnComment = columnComment; + } + + public String getColumnDefault() { + return columnDefault; + } + + public void setColumnDefault(String columnDefault) { + this.columnDefault = columnDefault; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + +} diff --git a/src/main/java/com/zhangmeng/tools/dto/GenerateDetail.java b/src/main/java/com/zhangmeng/tools/dto/GenerateDetail.java new file mode 100644 index 0000000..b3b2115 --- /dev/null +++ b/src/main/java/com/zhangmeng/tools/dto/GenerateDetail.java @@ -0,0 +1,29 @@ +package com.zhangmeng.tools.dto; + +import java.io.Serializable; +import java.util.List; + +public class GenerateDetail implements Serializable { + + private static final long serialVersionUID = -164567294469931676L; + + private String beanName; + + private List fields; + + public String getBeanName() { + return beanName; + } + + public void setBeanName(String beanName) { + this.beanName = beanName; + } + + public List getFields() { + return fields; + } + + public void setFields(List fields) { + this.fields = fields; + } +} diff --git a/src/main/java/com/zhangmeng/tools/dto/GenerateInput.java b/src/main/java/com/zhangmeng/tools/dto/GenerateInput.java new file mode 100644 index 0000000..2017fb8 --- /dev/null +++ b/src/main/java/com/zhangmeng/tools/dto/GenerateInput.java @@ -0,0 +1,155 @@ +package com.zhangmeng.tools.dto; + +import java.io.Serializable; +import java.util.List; + +public class GenerateInput implements Serializable { + + private static final long serialVersionUID = -2870071259702969061L; + + /** + * 保存路径 + */ + private String path; + + private String tableName; + + /** + * bean包名 + */ + private String beanPackageName; + + /** + * java类名 + */ + private String beanName; + /** + * dao包名 + */ + private String daoPackageName; + + /** + * dao类名 + */ + private String daoName; + /** + * controller包名 + */ + private String controllerPkgName; + /** + * controller类名 + */ + private String controllerName; + /** + * 字段名 + */ + private List columnNames; + /** + * 属性名 + */ + private List beanFieldName; + /** + * 成员变量类型 + */ + private List beanFieldType; + /** + * 默认值 + */ + private List beanFieldValue; + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public String getTableName() { + return tableName; + } + + public void setTableName(String tableName) { + this.tableName = tableName; + } + + public String getBeanPackageName() { + return beanPackageName; + } + + public void setBeanPackageName(String beanPackageName) { + this.beanPackageName = beanPackageName; + } + + public String getBeanName() { + return beanName; + } + + public void setBeanName(String beanName) { + this.beanName = beanName; + } + + public String getDaoPackageName() { + return daoPackageName; + } + + public void setDaoPackageName(String daoPackageName) { + this.daoPackageName = daoPackageName; + } + + public String getDaoName() { + return daoName; + } + + public void setDaoName(String daoName) { + this.daoName = daoName; + } + + public String getControllerPkgName() { + return controllerPkgName; + } + + public void setControllerPkgName(String controllerPkgName) { + this.controllerPkgName = controllerPkgName; + } + + public String getControllerName() { + return controllerName; + } + + public void setControllerName(String controllerName) { + this.controllerName = controllerName; + } + + public List getColumnNames() { + return columnNames; + } + + public void setColumnNames(List columnNames) { + this.columnNames = columnNames; + } + + public List getBeanFieldName() { + return beanFieldName; + } + + public void setBeanFieldName(List beanFieldName) { + this.beanFieldName = beanFieldName; + } + + public List getBeanFieldType() { + return beanFieldType; + } + + public void setBeanFieldType(List beanFieldType) { + this.beanFieldType = beanFieldType; + } + + public List getBeanFieldValue() { + return beanFieldValue; + } + + public void setBeanFieldValue(List beanFieldValue) { + this.beanFieldValue = beanFieldValue; + } +} diff --git a/src/main/java/com/zhangmeng/tools/utils/FileUtil.java b/src/main/java/com/zhangmeng/tools/utils/FileUtil.java new file mode 100644 index 0000000..92b3d85 --- /dev/null +++ b/src/main/java/com/zhangmeng/tools/utils/FileUtil.java @@ -0,0 +1,311 @@ +package com.zhangmeng.tools.utils; + +import it.sauronsoftware.jave.Encoder; +import it.sauronsoftware.jave.MultimediaInfo; +import org.apache.commons.codec.digest.DigestUtils; +import org.springframework.web.multipart.MultipartFile; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.*; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * 文件工具类 + */ +public class FileUtil { + + public static String saveFile(MultipartFile file, String pathname) { + try { + File targetFile = new File(pathname); + if (targetFile.exists()) { + return pathname; + } + + if (!targetFile.getParentFile().exists()) { + targetFile.getParentFile().mkdirs(); + } + file.transferTo(targetFile); + return pathname; + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + + public static boolean deleteFile(String pathname) { + File file = new File(pathname); + if (file.exists()) { + boolean flag = file.delete(); + + if (flag) { + File[] files = file.getParentFile().listFiles(); + if (files == null || files.length == 0) { + file.getParentFile().delete(); + } + } + + return flag; + } + + return false; + } + + public static String fileMd5(InputStream inputStream) { + try { + return DigestUtils.md5Hex(inputStream); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + + public static String getPath() { + return "/" + LocalDate.now().toString().replace("-", "/") + "/"; + } + + public static String getPath_2() { + return LocalDate.now().toString().replace("-", "/") + "/"; + } + + /** + * 将文本写入文件 + * + * @param value + * @param path + */ + public static void saveTextFile(String value, String path) { + FileWriter writer = null; + try { + File file = new File(path); + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + + writer = new FileWriter(file); + writer.write(value); + writer.flush(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (writer != null) { + writer.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public static String getText(String path) { + File file = new File(path); + if (!file.exists()) { + return null; + } + + try { + return getText(new FileInputStream(file)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + return null; + } + + public static String getText(InputStream inputStream) { + InputStreamReader isr = null; + BufferedReader bufferedReader = null; + try { + isr = new InputStreamReader(inputStream, "utf-8"); + bufferedReader = new BufferedReader(isr); + StringBuilder builder = new StringBuilder(); + String string; + while ((string = bufferedReader.readLine()) != null) { + string = string + "\n"; + builder.append(string); + } + + return builder.toString(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (bufferedReader != null) { + try { + bufferedReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (isr != null) { + try { + isr.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + return null; + } + + + /** + * 转存文件并且获取时长 + * + * @param file + * @param pathname + * @return + */ + public static Map saveFileAndgetDuration(MultipartFile file, String pathname, String type,String path_name) { + + Map map = new HashMap(); + try { + File targetFile = new File(pathname); + if (targetFile.exists()) { + map.put("pathname", pathname); + return map; + } + + if (!targetFile.getParentFile().exists()) { + targetFile.getParentFile().mkdirs(); + } + + map.put("pathname", pathname); + if (type != null && type.equals("1")) { + //获取图片的宽高 + BufferedImage bufferedImage = ImageIO.read(file.getInputStream()); + if (bufferedImage != null) { + int width = bufferedImage.getWidth(); + int height = bufferedImage.getHeight(); + map.put("imageWidth", width); + map.put("imageHeight", height); + } + } + //file.transferTo(targetFile); + FileServiceFactory.imagesUpload(pathname,path_name,file.getBytes()); + //如果是视频或者音频则进行获取时长 + if (type.equals("2") || type.equals("3")) { + Encoder encoder = new Encoder(); + MultimediaInfo m = encoder.getInfo(targetFile); + long ms = m.getDuration(); + + int ss = 1000; + int mi = ss * 60; + int hh = mi * 60; + int dd = hh * 24; + + long day = ms / dd; + long hour = (ms - day * dd) / hh; + long minute = (ms - day * dd - hour * hh) / mi; + long second = (ms - day * dd - hour * hh - minute * mi) / ss; + + String strHour = hour < 10 ? "0" + hour : "" + hour;//小时 + String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟 + String strSecond = second < 10 ? "0" + second : "" + second;//秒 + + if (type.equals("3")){ + VedioUtils.getTempPath(pathname,targetFile,map); + } + + if (strHour.equals("00")) { + map.put("duration", strMinute + ":" + strSecond); + return map; + } else { + map.put("duration", strHour + ":" + strMinute + ":" + strSecond); + return map; + } + + + } + return map; + } catch (Exception e) { + e.printStackTrace(); + map.put("msg", "保存错误"); + return map; + } + } + + /** + * 上传图像 + * @param file + * @param type + * @param accessory_path + * @param filesPath + * @return + */ + public static Map uploadphoto(MultipartFile file ,String type, String accessory_path, String filesPath) { + Map map = new HashMap(); + String pathname=null; + if (type.equals("6")) {//头像 + accessory_path = "upload/portrait"; + map.put("accessory_path",accessory_path); + }else if (type.equals("7")){ + accessory_path = "upload/platform"; + map.put("accessory_path",accessory_path); + } + String fileOrigName = file.getOriginalFilename(); + //fileOrigName = fileOrigName.substring(fileOrigName.lastIndexOf(".")); + SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyyMMddHHmmss"); + String path = dateformat1.format(new Date()) + "-" + fileOrigName; + map.put("path",path); + pathname = filesPath + "/"+ accessory_path + "/"+path; + try { + File targetFile = new File(pathname); + if (targetFile.exists()) { + map.put("pathname", pathname); + return map; + } + if (!targetFile.getParentFile().exists()) { + targetFile.getParentFile().mkdirs(); + } + if (type != null && type.equals("6")||type.equals("7")) { + //获取图片的宽高 + BufferedImage bufferedImage = ImageIO.read(file.getInputStream()); + if (bufferedImage != null) { + int width = bufferedImage.getWidth(); + int height = bufferedImage.getHeight(); + map.put("imageWidth", width); + map.put("imageHeight", height); + } + } + //file.transferTo(targetFile); + FileServiceFactory.imagesUpload(pathname,accessory_path + "/"+path,file.getBytes()); + return map; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + + /** + * 判断文件大小 + * @param len 文件长度 + * @param size 限制大小 + * @param unit 限制单位(B,K,M,G) + * @return + */ + public static boolean checkFileSize(Long len, int size, String unit) { + double fileSize = 0; + if ("B".equals(unit.toUpperCase())) { + fileSize = (double) len; + } else if ("K".equals(unit.toUpperCase())) { + fileSize = (double) len / 1024; + } else if ("M".equals(unit.toUpperCase())) { + fileSize = (double) len / 1048576; + } else if ("G".equals(unit.toUpperCase())) { + fileSize = (double) len / 1073741824; + } + if (fileSize > size) { + return false; + } + return true; + } +} diff --git a/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java b/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java index 7203779..6c826b6 100644 --- a/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java +++ b/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java @@ -143,4 +143,34 @@ public class ResourcesUtils { private String title; private int index; } + + public enum SqlTools{ + + MySql_Code_Generate("mysql 代码生成",0), + ; + + SqlTools(String title, int index) { + this.title = title; + this.index = index; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } + + private String title; + private int index; + } } diff --git a/src/main/java/com/zhangmeng/tools/utils/TemplateUtil.java b/src/main/java/com/zhangmeng/tools/utils/TemplateUtil.java new file mode 100644 index 0000000..e1c1ba2 --- /dev/null +++ b/src/main/java/com/zhangmeng/tools/utils/TemplateUtil.java @@ -0,0 +1,360 @@ +package com.zhangmeng.tools.utils; + +import com.google.common.collect.Lists; +import com.zhangmeng.tools.dto.GenerateInput; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.math.BigDecimal; +import java.util.Date; +import java.util.List; + +public class TemplateUtil { + + private static final Logger log = LoggerFactory.getLogger("adminLogger"); + + public static String getTemplete(String fileName) { + return FileUtil.getText(TemplateUtil.class.getClassLoader().getResourceAsStream("generate/" + fileName)); + } + + public static void saveJava(GenerateInput input) { + String path = input.getPath(); + String beanPackageName = input.getBeanPackageName(); + String beanName = input.getBeanName(); + List beanFieldName = input.getBeanFieldName(); + List beanFieldType = input.getBeanFieldType(); + List beanFieldValue = input.getBeanFieldValue(); + + String text = getTemplete("java.txt"); + text = text.replace("{beanPackageName}", beanPackageName).replace("{beanName}", beanName); + + String imports = ""; + if (beanFieldType.contains(BigDecimal.class.getSimpleName())) { + imports += "import " + BigDecimal.class.getName() + ";\n"; + } + if (beanFieldType.contains(Date.class.getSimpleName())) { + imports += "import " + Date.class.getName() + ";"; + } + + text = text.replace("{import}", imports); + String filelds = getFields(beanFieldName, beanFieldType, beanFieldValue); + text = text.replace("{filelds}", filelds); + text = text.replace("{getset}", getset(beanFieldName, beanFieldType)); + + FileUtil.saveTextFile(text, path + File.separator + getPackagePath(beanPackageName) + beanName + ".java"); + log.debug("生成java model:{}模板", beanName); + } + + private static String getFields(List beanFieldName, List beanFieldType, + List beanFieldValue) { + StringBuffer buffer = new StringBuffer(); + int size = beanFieldName.size(); + for (int i = 0; i < size; i++) { + String name = beanFieldName.get(i); + if ("id".equals(name) || "createTime".equals(name) || "updateTime".equals(name)) { + continue; + } + String type = beanFieldType.get(i); + buffer.append("\tprivate ").append(type).append(" ").append(name); + // 默认值 +// String value = beanFieldValue.get(i); +// if (!StringUtils.isEmpty(value)) { +// buffer.append(" = "); +// if (type.equals(String.class.getSimpleName())) { +// value = "\"" + value + "\""; +// } else if (type.equals(Double.class.getSimpleName())) { +// value = value + "D"; +// } else if (type.equals(Float.class.getSimpleName())) { +// value = value + "F"; +// } else if (type.equals(BigDecimal.class.getSimpleName())) { +// value = "new BigDecimal(" + value + ")"; +// } +// +// buffer.append(value); +// } + buffer.append(";\n"); + } + + return buffer.toString(); + } + + private static String getset(List beanFieldName, List beanFieldType) { + StringBuffer buffer = new StringBuffer(); + int size = beanFieldName.size(); + for (int i = 0; i < size; i++) { + String name = beanFieldName.get(i); + if ("id".equals(name) || "createTime".equals(name) || "updateTime".equals(name)) { + continue; + } + + String type = beanFieldType.get(i); + buffer.append("\tpublic ").append(type).append(" get") + .append(StringUtils.substring(name, 0, 1).toUpperCase() + name.substring(1, name.length())) + .append("() {\n"); + buffer.append("\t\treturn ").append(name).append(";\n"); + buffer.append("\t}\n"); + buffer.append("\tpublic void set") + .append(StringUtils.substring(name, 0, 1).toUpperCase() + name.substring(1, name.length())) + .append("(").append(type).append(" ").append(name).append(") {\n"); + buffer.append("\t\tthis.").append(name).append(" = ").append(name).append(";\n"); + buffer.append("\t}\n"); + } + + return buffer.toString(); + } + + public static void saveJavaDao(GenerateInput input) { + String path = input.getPath(); + String tableName = input.getTableName(); + String beanPackageName = input.getBeanPackageName(); + String beanName = input.getBeanName(); + String daoPackageName = input.getDaoPackageName(); + String daoName = input.getDaoName(); + + String text = getTemplete("dao.txt"); + text = text.replace("{daoPackageName}", daoPackageName); + text = text.replace("{beanPackageName}", beanPackageName); + text = text.replace("{daoName}", daoName); + text = text.replace("{table_name}", tableName); + text = text.replace("{beanName}", beanName); + text = text.replace("{beanParamName}", lowerFirstChar(beanName)); + + String insertColumns = getInsertColumns(input.getColumnNames()); + text = text.replace("{insert_columns}", insertColumns); + String insertValues = getInsertValues(input.getColumnNames(), input.getBeanFieldName()); + text = text.replace("{insert_values}", insertValues); + FileUtil.saveTextFile(text, path + File.separator + getPackagePath(daoPackageName) + daoName + ".java"); + log.debug("生成java dao:{}模板", beanName); + + text = getTemplete("mapper.xml"); + text = text.replace("{daoPackageName}", daoPackageName); + text = text.replace("{daoName}", daoName); + text = text.replace("{table_name}", tableName); + text = text.replace("{beanName}", beanName); + String sets = getUpdateSets(input.getColumnNames(), input.getBeanFieldName()); + text = text.replace("{update_sets}", sets); + String where = getWhere(input.getColumnNames(), input.getBeanFieldName()); + text = text.replace("{where}", where); + FileUtil.saveTextFile(text, path + File.separator + beanName + "Mapper.xml"); + } + + private static String getInsertValues(List columnNames, List beanFieldName) { + StringBuffer buffer = new StringBuffer(); + int size = columnNames.size(); + for (int i = 0; i < size; i++) { + String column = columnNames.get(i); + if (!"id".equals(column)) { + buffer.append("#{").append(beanFieldName.get(i)).append("}, "); + } + } + + String sets = StringUtils.substringBeforeLast(buffer.toString(), ","); + return sets; + } + + private static String getInsertColumns(List columnNames) { + StringBuffer buffer = new StringBuffer(); + int size = columnNames.size(); + for (int i = 0; i < size; i++) { + String column = columnNames.get(i); + if (!"id".equals(column)) { + buffer.append(column).append(", "); + } + } + + String insertColumns = StringUtils.substringBeforeLast(buffer.toString(), ","); + return insertColumns; + } + + private static String getUpdateSets(List columnNames, List beanFieldName) { + StringBuffer buffer = new StringBuffer(); + int size = columnNames.size(); + for (int i = 0; i < size; i++) { + String column = columnNames.get(i); + if (!"id".equals(column)) { + buffer.append("\t\t\t\n"); + buffer.append("\t\t\t\t" + column).append(" = ").append("#{").append(beanFieldName.get(i)) + .append("}, \n"); + buffer.append("\t\t\t\n"); + } + } + + return buffer.toString(); + } + + private static String getWhere(List columnNames, List beanFieldName) { + StringBuffer buffer = new StringBuffer(); + int size = columnNames.size(); + for (int i = 0; i < size; i++) { + String column = columnNames.get(i); + buffer.append("\t\t\t\n"); + buffer.append("\t\t\t\tand " + column).append(" = ").append("#{params.").append(beanFieldName.get(i)) + .append("} \n"); + buffer.append("\t\t\t\n"); + } + + return buffer.toString(); + } + + /** + * 变量名 + * + * @param beanName + * @return + */ + public static String lowerFirstChar(String beanName) { + String name = str2hump(beanName); + String firstChar = name.substring(0, 1); + name = name.replaceFirst(firstChar, firstChar.toLowerCase()); + + return name; + } + + /** + * 字符串转为驼峰 + * + * @param str + * @return + */ + public static String str2hump(String str) { + StringBuffer buffer = new StringBuffer(); + if (str != null && str.length() > 0) { + if (str.contains("_")) { + String[] chars = str.split("_"); + int size = chars.length; + if (size > 0) { + List list = Lists.newArrayList(); + for (String s : chars) { + if (s != null && s.trim().length() > 0) { + list.add(s); + } + } + + size = list.size(); + if (size > 0) { + buffer.append(list.get(0)); + for (int i = 1; i < size; i++) { + String s = list.get(i); + buffer.append(s.substring(0, 1).toUpperCase()); + if (s.length() > 1) { + buffer.append(s.substring(1)); + } + } + } + } + } else { + buffer.append(str); + } + } + + return buffer.toString(); + } + + + private static String getPackagePath(String packageName) { + String packagePath = packageName.replace(".", "/"); + if (!packagePath.endsWith("/")) { + packagePath = packagePath + "/"; + } + + return packagePath; + } + + public static void saveController(GenerateInput input) { + String path = input.getPath(); + String beanPackageName = input.getBeanPackageName(); + String beanName = input.getBeanName(); + String daoPackageName = input.getDaoPackageName(); + String daoName = input.getDaoName(); + + String text = getTemplete("controller.txt"); + text = text.replace("{daoPackageName}", daoPackageName); + text = text.replace("{beanPackageName}", beanPackageName); + text = text.replace("{daoName}", daoName); + text = text.replace("{daoParamName}", lowerFirstChar(daoName)); + text = text.replace("{beanName}", beanName); + text = text.replace("{beanParamName}", lowerFirstChar(beanName)); + text = text.replace("{controllerPkgName}", input.getControllerPkgName()); + text = text.replace("{controllerName}", input.getControllerName()); + + FileUtil.saveTextFile(text, path + File.separator + getPackagePath(input.getControllerPkgName()) + + input.getControllerName() + ".java"); + log.debug("生成controller:{}模板", beanName); + } + + public static void saveHtmlList(GenerateInput input) { + String path = input.getPath(); + String beanName = input.getBeanName(); + String beanParamName = lowerFirstChar(beanName); + + String text = getTemplete("htmlList.txt"); + text = text.replace("{beanParamName}", beanParamName); + text = text.replace("{beanName}", beanName); + List beanFieldNames = input.getBeanFieldName(); + text = text.replace("{columnsDatas}", getHtmlColumnsDatas(beanFieldNames)); + text = text.replace("{ths}", getHtmlThs(beanFieldNames)); + + FileUtil.saveTextFile(text, path + File.separator + beanParamName + "List.html"); + log.debug("生成查询页面:{}模板", beanName); + + text = getTemplete("htmlAdd.txt"); + text = text.replace("{beanParamName}", beanParamName); + text = text.replace("{addDivs}", getAddDivs(beanFieldNames)); + FileUtil.saveTextFile(text, path + File.separator + "add" + beanName + ".html"); + log.debug("生成添加页面:{}模板", beanName); + + text = getTemplete("htmlUpdate.txt"); + text = text.replace("{beanParamName}", beanParamName); + text = text.replace("{addDivs}", getAddDivs(beanFieldNames)); + text = text.replace("{initData}", getInitData(beanFieldNames)); + FileUtil.saveTextFile(text, path + File.separator + "update" + beanName + ".html"); + log.debug("生成修改页面:{}模板", beanName); + } + + private static CharSequence getInitData(List beanFieldNames) { + StringBuilder builder = new StringBuilder(); + beanFieldNames.forEach(b -> { + builder.append("\t\t\t\t\t\t$('#" + b + "').val(data." + b + ");\n"); + }); + + return builder.toString(); + } + + private static String getAddDivs(List beanFieldNames) { + StringBuilder builder = new StringBuilder(); + beanFieldNames.forEach(b -> { + if (!"id".equals(b) && !"createTime".equals(b) && !"updateTime".equals(b)) { + builder.append("\t\t\t
\n"); + builder.append("\t\t\t\t\n"); + builder.append("\t\t\t\t
\n"); + builder.append("\t\t\t\t\t\n"); + builder.append("\t\t\t\t
\n"); + builder.append("\t\t\t
\n"); + } + }); + return builder.toString(); + } + + private static String getHtmlThs(List beanFieldNames) { + StringBuilder builder = new StringBuilder(); + beanFieldNames.forEach(b -> { + builder.append("\t\t\t\t\t\t\t\t\t{beanFieldName}\n".replace("{beanFieldName}", b)); + }); + return builder.toString(); + } + + private static String getHtmlColumnsDatas(List beanFieldNames) { + StringBuilder builder = new StringBuilder(); + beanFieldNames.forEach(b -> { + builder.append("\t\t\t\t{\"data\" : \"{beanFieldName}\", \"defaultContent\" : \"\"},\n" + .replace("{beanFieldName}", b)); + }); + + builder.append(""); + return builder.toString(); + } + +} diff --git a/src/main/resources/fxml/home.fxml b/src/main/resources/fxml/home.fxml index 562a8d7..f51b8b8 100644 --- a/src/main/resources/fxml/home.fxml +++ b/src/main/resources/fxml/home.fxml @@ -45,6 +45,12 @@ + + + + + + + + + + + + + + + + diff --git a/src/main/resources/fxml/sql-tools.fxml b/src/main/resources/fxml/sql-tools.fxml new file mode 100644 index 0000000..a32f486 --- /dev/null +++ b/src/main/resources/fxml/sql-tools.fxml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +