添加 mybatis-plus 代码生成 2023年3月22日16:50:09

master
zhangmeng 2023-03-22 16:50:30 +08:00
parent 3ce02ad0da
commit 9b547817d3
21 changed files with 500 additions and 0 deletions

View File

@ -113,6 +113,15 @@
> 部分代码借鉴 开源项目: boot-security gitee 地址: https://gitee.com/zhang.w/boot-security
#### 5.2 mybatis-plus 代码生成
> 使用 mybatis-plus 生成,配置可视化
![](./src/main/resources/static/redame/img_35.png)
![](./src/main/resources/static/redame/img_36.png)
### 6. 网络工具
#### 6.1 websocket 工具

13
pom.xml
View File

@ -300,6 +300,19 @@
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.5.2</version>
</dependency>
</dependencies>
<build>

View File

@ -550,4 +550,8 @@ public class CodecToolsController {
public void telephone_menu_item(ActionEvent event) {
load_small_tools(8);
}
public void mybatis_plus_gen_menu_item(ActionEvent event) {
load_sql_tools(1);
}
}

View File

@ -475,4 +475,8 @@ public class HomeController implements Serializable {
public void telephone_menu_item(ActionEvent event) {
load_small_tools(8);
}
public void mybatis_plus_gen_menu_item(ActionEvent event) {
load_sql_tools(1);
}
}

View File

@ -486,4 +486,8 @@ public class HttpToolsController {
public void telephone_menu_item(ActionEvent event) {
load_small_tools(8);
}
public void mybatis_plus_gen_menu_item(ActionEvent event) {
load_sql_tools(1);
}
}

View File

@ -0,0 +1,361 @@
package com.zhangmeng.tools.controller;
import cn.hutool.core.text.StrFormatter;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.zhangmeng.tools.dto.BeanField;
import com.zhangmeng.tools.dto.GenerateDetail;
import com.zhangmeng.tools.utils.AlertUtils;
import com.zhangmeng.tools.utils.TemplateUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.util.Callback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.CollectionUtils;
import javax.sound.sampled.Port;
import java.math.BigDecimal;
import java.util.*;
/**
* @author :
* @version : 1.0
* @date : 2023-03-22 14:03
*/
@Slf4j
public class MybatisPlusGenerator {
@FXML
public TextField url;
@FXML
public TextField username;
@FXML
public TextField password;
@FXML
public TextField author;
@FXML
public TextField out_dir;
@FXML
public TextField prefix;
@FXML
public TextField mapper_xml_path;
@FXML
public TextField model_name;
@FXML
public TextField parent_package;
@FXML
public Button generator;
@FXML
private TextField port;
@FXML
private ComboBox<String> comboBox_data_base;
@FXML
private TextField data_base;
@FXML
public ListView<String> listView;
private final ObservableList<String> data_base_list = FXCollections.observableArrayList();
private final ObservableList<String> table_list = FXCollections.observableArrayList();
private JdbcTemplate jdbcTemplate;
@FXML
public void initialize() {
if (url.getText().length() == 0) {
url.setText("localhost");
}
if (username.getText().length() == 0) {
username.setText("root");
}
if (port.getText().length() == 0) {
port.setText("3306");
}
if (password.getText().length() == 0) {
password.setText("root");
}
if (author.getText().length() == 0) {
author.setText("zhangmeng");
}
if (out_dir.getText().length() == 0) {
out_dir.setText("D:\\mybatis_gen");
}
if (prefix.getText().length() == 0) {
prefix.setText("dashidao,t");
}
if (mapper_xml_path.getText().length() == 0) {
mapper_xml_path.setText("D:\\mybatis_gen\\mapper");
}
if (model_name.getText().length() == 0) {
model_name.setText("server");
}
if (parent_package.getText().length() == 0) {
parent_package.setText("com.zhangmeng");
}
comboBox_data_base.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
log.info("选择的数据库名为:{}", newValue);
data_base.setText(newValue);
update_select_data_base();
}
});
listView.setItems(table_list);
listView.setFixedCellSize(40);
listView.getSelectionModel().select(0);
listView.setCellFactory(new Callback<>() {
@Override
public ListCell<String> call(ListView<String> param) {
return new ListCell<>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
HBox hBox = new HBox();
hBox.setAlignment(Pos.CENTER);
hBox.getChildren().add(new Label(item));
this.setGraphic(hBox);
}
}
};
}
});
generator.setOnAction(event -> {
String ur1 = getUrl(url.getText(), Integer.parseInt(port.getText()), data_base.getText());
DataSourceInfo dataSourceInfo = new DataSourceInfo();
dataSourceInfo.setUrl(ur1);
dataSourceInfo.setUsername(username.getText());
dataSourceInfo.setPassword(password.getText());
dataSourceInfo.setAuthor(author.getText());
dataSourceInfo.setOut_dir(out_dir.getText());
dataSourceInfo.setParent_package_name(parent_package.getText());
dataSourceInfo.setParent_module_name(model_name.getText());
dataSourceInfo.setMapper_xml_path(mapper_xml_path.getText());
String text = prefix.getText();
String[] prefixes = text.split(",");
dataSourceInfo.setTable_prefixes(prefixes);
List<String> tables = new ArrayList<>(table_list);
dataSourceInfo.setTables(tables);
generator(dataSourceInfo);
AlertUtils.alert_msg("生成成功!");
});
}
@FXML
public void get_all_data_base() {
log.info("获取所有数据表.....");
data_base_list.clear();
getJdbcTemplate();
List<Map<String, Object>> maps = this.jdbcTemplate.queryForList("show databases");
for (Map<String, Object> map : maps) {
log.info("map->{}", map.toString());
data_base_list.add(map.get("Database").toString());
}
comboBox_data_base.setItems(data_base_list);
comboBox_data_base.getSelectionModel().select(0);
data_base.setText(comboBox_data_base.getSelectionModel().getSelectedItem());
}
@FXML
public void fetch_table() {
table_list.clear();
getJdbcTemplate();
List<Map<String, Object>> maps = this.jdbcTemplate.queryForList("SELECT table_name, table_type, TABLE_COMMENT FROM information_schema.tables WHERE table_schema = (select database()) ORDER BY table_name DESC");
for (Map<String, Object> map : maps) {
String table_name = (String) map.get("table_name");
log.info("table_name:{}", table_name);
table_list.add(table_name);
}
}
private void update_select_data_base() {
String text = data_base.getText();
getJdbcTemplate();
log.info("update_select_data_base:{}", text);
this.jdbcTemplate.execute("use `" + text + "`");
}
public String getUrl(String ip, int port, String data_base) {
return StrFormatter.format("jdbc:mysql://{}:{}/{}?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true", ip, port, data_base);
}
public void generator(DataSourceInfo dataSourceInfo) {
FastAutoGenerator.create(dataSourceInfo.getUrl(), dataSourceInfo.getUsername(), dataSourceInfo.getPassword()).globalConfig(builder -> {
builder.author(dataSourceInfo.author) // 设置作者
// .enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir(dataSourceInfo.getOut_dir()); // 指定输出目录
}).packageConfig(builder -> {
builder.parent(dataSourceInfo.getParent_package_name()) // 设置父包名
.moduleName(dataSourceInfo.getParent_module_name()) // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, dataSourceInfo.getMapper_xml_path())); // 设置mapperXml生成路径
}).strategyConfig(builder -> {
builder.addInclude(dataSourceInfo.getTables()) // 设置需要生成的表名
.addTablePrefix(dataSourceInfo.getTable_prefixes()); // 设置过滤表前缀
}).templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板默认的是Velocity引擎模板
.execute();
}
public JdbcTemplate getJdbcTemplate() {
if (jdbcTemplate == null) {
jdbcTemplate = new JdbcTemplate();
DruidDataSource datasource = new DruidDataSource();
String con = "jdbc:mysql://" + url.getText() + ":" + port.getText() + "/" + data_base.getText() + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true";
String driverClassName = "com.mysql.jdbc.Driver";
datasource.setUrl(con);
datasource.setUsername(username.getText());
datasource.setPassword(password.getText());
datasource.setDriverClassName(driverClassName);
jdbcTemplate.setDataSource(datasource);
}
return jdbcTemplate;
}
public static class DataSourceInfo {
public DataSourceInfo() {
}
public DataSourceInfo(String url, String username, String password, String author, String out_dir, String parent_package_name, String parent_module_name, String mapper_xml_path, List<String> tables, String[] table_prefixes) {
this.url = url;
this.username = username;
this.password = password;
this.author = author;
this.out_dir = out_dir;
this.parent_package_name = parent_package_name;
this.parent_module_name = parent_module_name;
this.mapper_xml_path = mapper_xml_path;
this.tables = tables;
this.table_prefixes = table_prefixes;
}
private String url;
private String username;
private String password;
private String author;
private String out_dir;
private String parent_package_name;
private String parent_module_name;//设置父包模块名
private String mapper_xml_path;//设置mapperXml生成路径;
private List<String> tables;//表名
private String[] table_prefixes;
public String[] getTable_prefixes() {
return table_prefixes;
}
public void setTable_prefixes(String[] table_prefixes) {
this.table_prefixes = table_prefixes;
}
public List<String> getTables() {
return tables;
}
public void setTables(List<String> tables) {
this.tables = tables;
}
public String getMapper_xml_path() {
return mapper_xml_path;
}
public void setMapper_xml_path(String mapper_xml_path) {
this.mapper_xml_path = mapper_xml_path;
}
public String getParent_module_name() {
return parent_module_name;
}
public void setParent_module_name(String parent_module_name) {
this.parent_module_name = parent_module_name;
}
public String getParent_package_name() {
return parent_package_name;
}
public void setParent_package_name(String parent_package_name) {
this.parent_package_name = parent_package_name;
}
public String getOut_dir() {
return out_dir;
}
public void setOut_dir(String out_dir) {
this.out_dir = out_dir;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
}

View File

@ -508,4 +508,8 @@ public class PlayerController {
public void telephone_menu_item(ActionEvent event) {
load_small_tools(8);
}
public void mybatis_plus_gen_menu_item(ActionEvent event) {
load_sql_tools(1);
}
}

View File

@ -511,4 +511,9 @@ public class ServerToolsController {
public void telephone_menu_item(ActionEvent event) {
load_small_tools(8);
}
public void mybatis_plus_gen_menu_item(ActionEvent event) {
load_sql_tools(1);
}
}

View File

@ -697,4 +697,9 @@ public class SmallToolsController {
}
telephone(flag);
}
public void mybatis_plus_gen_menu_item(ActionEvent event) {
load_mysql_tools(1);
}
}

View File

@ -65,6 +65,7 @@ public class SqlToolsController {
private AnchorPane root;
private AnchorPane mysql_code_gen;
private AnchorPane spring_security;
private AnchorPane mybatis_plus_gen;
public static final String color_cell = "#f4f4f4";
@ -214,6 +215,13 @@ public class SqlToolsController {
}
mysql_code_gen(flag);
}
if (newValue.getIndex() == 1) {
if (mybatis_plus_gen != null){
flag = true;
}
mybatis_plus_gen(flag);
}
}
});
}
@ -221,6 +229,7 @@ public class SqlToolsController {
public static Image getImage(ResourcesUtils.SqlTools player){
return switch (player){
case MySql_Code_Generate -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5));
case MyBatis_plus_Generate -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5));
};
}
@ -295,6 +304,23 @@ public class SqlToolsController {
common_method();
}
private void mybatis_plus_gen(boolean flag){
//默认选择第一个
listView.getSelectionModel().select(1);
if (!flag){
try {
root = FXMLLoader.load(ResourcesUtils.getResource("mybatis-plus-generator"));
} catch (IOException e) {
e.printStackTrace();
}
mybatis_plus_gen = root;
}else {
root = mybatis_plus_gen;
}
common_method();
}
private void common_method() {
splitPane.getItems().remove(1);
splitPane.getItems().add(1, root);
@ -431,4 +457,12 @@ public class SqlToolsController {
public void telephone_menu_item(ActionEvent event) {
load_small_tools(8);
}
public void mybatis_plus_gen_menu_item(ActionEvent event) {
boolean flag =false;
if (mybatis_plus_gen != null){
flag = true;
}
mybatis_plus_gen(flag);
}
}

View File

@ -182,6 +182,7 @@ public class ResourcesUtils {
public enum SqlTools{
MySql_Code_Generate("mysql 代码生成",0),
MyBatis_plus_Generate("mybatis-plus 代码生成",1),
;
SqlTools(String title, int index) {

View File

@ -52,6 +52,7 @@
<Menu mnemonicParsing="false" text="sql工具">
<items>
<MenuItem mnemonicParsing="false" text="mysql代码生成" onAction="#sql_code_gen_menu_item"/>
<MenuItem mnemonicParsing="false" text="mybatis-plus 代码生成" onAction="#mybatis_plus_gen_menu_item"/>
</items>
</Menu>

View File

@ -54,6 +54,7 @@
<Menu mnemonicParsing="false" text="sql工具">
<items>
<MenuItem mnemonicParsing="false" text="mysql代码生成" onAction="#sql_code_gen_menu_item"/>
<MenuItem mnemonicParsing="false" text="mybatis-plus 代码生成" onAction="#mybatis_plus_gen_menu_item"/>
</items>
</Menu>

View File

@ -54,6 +54,7 @@
<Menu mnemonicParsing="false" text="sql工具">
<items>
<MenuItem mnemonicParsing="false" text="mysql代码生成" onAction="#sql_code_gen_menu_item"/>
<MenuItem mnemonicParsing="false" text="mybatis-plus 代码生成" onAction="#mybatis_plus_gen_menu_item"/>
</items>
</Menu>

View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>
<AnchorPane prefHeight="649.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.tools.controller.MybatisPlusGenerator">
<children>
<Label layoutX="74.0" layoutY="192.0" text="作者:" />
<Label layoutX="289.0" layoutY="54.0" text="password:" />
<Label layoutX="64.0" layoutY="54.0" text="username:" />
<Label layoutX="406.0" layoutY="192.0" text="输出目录:" />
<Label layoutX="55.0" layoutY="346.0" text="设置父包名:" />
<Label layoutX="38.0" layoutY="293.0" text="设置mapperXml生成路径:" />
<Label layoutX="37.0" layoutY="236.0" text="设置过滤表前缀:" />
<TextField fx:id="username" layoutX="139.0" layoutY="50.0" prefHeight="25.0" prefWidth="129.0" />
<TextField fx:id="password" layoutX="367.0" layoutY="50.0" prefHeight="25.0" prefWidth="129.0" />
<TextField fx:id="author" layoutX="139.0" layoutY="188.0" prefHeight="25.0" prefWidth="240.0" />
<TextField fx:id="out_dir" layoutX="470.0" layoutY="188.0" prefHeight="25.0" prefWidth="240.0" />
<TextField fx:id="prefix" layoutX="139.0" layoutY="232.0" prefHeight="25.0" prefWidth="240.0" />
<TextField fx:id="mapper_xml_path" layoutX="195.0" layoutY="289.0" prefHeight="25.0" prefWidth="516.0" />
<TextField fx:id="model_name" layoutX="496.0" layoutY="342.0" prefHeight="25.0" prefWidth="98.0" />
<TextField fx:id="parent_package" layoutX="139.0" layoutY="342.0" prefHeight="25.0" prefWidth="240.0" />
<Label layoutX="396.0" layoutY="346.0" text="设置父包模块名:" />
<Label layoutX="80.0" layoutY="93.0" text="ip:" />
<TextField fx:id="url" layoutX="139.0" layoutY="89.0" prefHeight="25.0" prefWidth="129.0" />
<Label layoutX="304.0" layoutY="93.0" text="port:" />
<TextField fx:id="port" layoutX="367.0" layoutY="89.0" prefHeight="25.0" prefWidth="129.0" />
<TextField fx:id="data_base" layoutX="862.0" layoutY="50.0" prefHeight="25.0" prefWidth="161.0" />
<Label layoutX="527.0" layoutY="54.0" text="数据库列表:" />
<ComboBox fx:id="comboBox_data_base" layoutX="600.0" layoutY="50.0" prefWidth="150.0" />
<Button layoutX="562.0" layoutY="89.0" mnemonicParsing="false" onAction="#get_all_data_base" prefHeight="25.0" prefWidth="129.0" text="获取数据库列表" />
<Label layoutX="795.0" layoutY="54.0" text="数据库名:" />
<Label layoutX="750.0" layoutY="192.0" text="列表:" />
<Separator layoutX="52.0" layoutY="145.0" prefWidth="200.0" AnchorPane.leftAnchor="124.0" AnchorPane.rightAnchor="0.0" />
<Separator layoutX="52.0" layoutY="31.0" prefWidth="200.0" AnchorPane.leftAnchor="101.0" AnchorPane.rightAnchor="0.0" />
<Label layoutX="49.0" layoutY="23.0" text="数据源配置" />
<Label layoutX="49.0" layoutY="137.0" text="mybatis-plus" />
<Button layoutX="842.0" layoutY="89.0" mnemonicParsing="false" onAction="#fetch_table" text="获取列表" />
<ListView fx:id="listView" layoutX="795.0" layoutY="188.0" prefHeight="409.0" prefWidth="347.0" AnchorPane.bottomAnchor="52.0" AnchorPane.leftAnchor="795.0" AnchorPane.rightAnchor="58.0" AnchorPane.topAnchor="188.0" />
<Button fx:id="generator" layoutX="332.0" layoutY="460.0" mnemonicParsing="false" text="生成" />
<Text layoutX="406.0" layoutY="249.0" strokeType="OUTSIDE" strokeWidth="0.0" text="(多个字段用,隔开)" />
</children>
</AnchorPane>

View File

@ -52,6 +52,7 @@
<Menu mnemonicParsing="false" text="sql工具">
<items>
<MenuItem mnemonicParsing="false" text="mysql代码生成" onAction="#sql_code_gen_menu_item"/>
<MenuItem mnemonicParsing="false" text="mybatis-plus 代码生成" onAction="#mybatis_plus_gen_menu_item"/>
</items>
</Menu>

View File

@ -54,6 +54,7 @@
<Menu mnemonicParsing="false" text="sql工具">
<items>
<MenuItem mnemonicParsing="false" text="mysql代码生成" onAction="#sql_code_gen_menu_item"/>
<MenuItem mnemonicParsing="false" text="mybatis-plus 代码生成" onAction="#mybatis_plus_gen_menu_item"/>
</items>
</Menu>

View File

@ -52,6 +52,7 @@
<Menu mnemonicParsing="false" text="sql工具">
<items>
<MenuItem mnemonicParsing="false" text="mysql代码生成" onAction="#sql_code_gen_menu_item"/>
<MenuItem mnemonicParsing="false" text="mybatis-plus 代码生成" onAction="#mybatis_plus_gen_menu_item"/>
</items>
</Menu>

View File

@ -54,6 +54,7 @@
<Menu mnemonicParsing="false" text="sql工具">
<items>
<MenuItem mnemonicParsing="false" text="mysql代码生成" onAction="#sql_code_gen_menu_item"/>
<MenuItem mnemonicParsing="false" text="mybatis-plus 代码生成" onAction="#mybatis_plus_gen_menu_item"/>
</items>
</Menu>

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB