From 6cf5472a08860b6fcfd62f03eb68810161d9cde6 Mon Sep 17 00:00:00 2001 From: wanghao Date: Tue, 19 Mar 2024 16:40:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(all):=E6=B7=BB=E5=8A=A0=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=8A=A8=E6=80=81=E8=A1=A8=E5=90=8D=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD,=E6=B7=BB=E5=8A=A0=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E7=94=9F=E6=88=90=E9=AA=8C=E8=AF=81=E7=B1=BB=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=80=89=E6=8B=A9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DatabasetoEntityclass/Form1.Designer.cs | 80 ++++++++++++++- DatabasetoEntityclass/Form1.cs | 99 ++++++++++++++++++- .../Template/ModelFile.template | 10 ++ DatabasetoEntityclass/Util/DbUtil.cs | 30 ++++-- 4 files changed, 204 insertions(+), 15 deletions(-) diff --git a/DatabasetoEntityclass/Form1.Designer.cs b/DatabasetoEntityclass/Form1.Designer.cs index ff79308..f53ef75 100644 --- a/DatabasetoEntityclass/Form1.Designer.cs +++ b/DatabasetoEntityclass/Form1.Designer.cs @@ -46,6 +46,12 @@ this.label5 = new System.Windows.Forms.Label(); this.isGenOperationColumn = new System.Windows.Forms.CheckBox(); this.label6 = new System.Windows.Forms.Label(); + this.txt_database = new System.Windows.Forms.ComboBox(); + this.databaseLable = new System.Windows.Forms.Label(); + this.label7 = new System.Windows.Forms.Label(); + this.isGeneratorValidateClass = new System.Windows.Forms.CheckBox(); + this.label8 = new System.Windows.Forms.Label(); + this.isGeneratorDynamicTableName = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // // modelGen @@ -61,7 +67,7 @@ // label2 // this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(357, 16); + this.label2.Location = new System.Drawing.Point(530, 16); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(41, 12); this.label2.TabIndex = 4; @@ -106,11 +112,12 @@ // txt_tablename // this.txt_tablename.FormattingEnabled = true; - this.txt_tablename.Location = new System.Drawing.Point(404, 12); + this.txt_tablename.Location = new System.Drawing.Point(577, 12); this.txt_tablename.Name = "txt_tablename"; this.txt_tablename.Size = new System.Drawing.Size(218, 20); this.txt_tablename.TabIndex = 72; this.txt_tablename.Text = "请选择"; + this.txt_tablename.SelectionChangeCommitted += new System.EventHandler(this.txt_tablename_SelectionChangeCommitted); // // textBox1 // @@ -214,11 +221,72 @@ this.label6.TabIndex = 83; this.label6.Text = "是否生成操作列(生成2和3):"; // + // txt_database + // + this.txt_database.FormattingEnabled = true; + this.txt_database.Location = new System.Drawing.Point(188, 13); + this.txt_database.Name = "txt_database"; + this.txt_database.Size = new System.Drawing.Size(218, 20); + this.txt_database.TabIndex = 85; + this.txt_database.Text = "请选择"; + this.txt_database.SelectionChangeCommitted += new System.EventHandler(this.txt_database_SelectionChangeCommitted); + // + // databaseLable + // + this.databaseLable.AutoSize = true; + this.databaseLable.Location = new System.Drawing.Point(141, 17); + this.databaseLable.Name = "databaseLable"; + this.databaseLable.Size = new System.Drawing.Size(41, 12); + this.databaseLable.TabIndex = 84; + this.databaseLable.Text = "表名:"; + // + // label7 + // + this.label7.AutoSize = true; + this.label7.Location = new System.Drawing.Point(54, 134); + this.label7.Name = "label7"; + this.label7.Size = new System.Drawing.Size(101, 12); + this.label7.TabIndex = 87; + this.label7.Text = "是否生成验证类:"; + // + // isGeneratorValidateClass + // + this.isGeneratorValidateClass.AutoSize = true; + this.isGeneratorValidateClass.Location = new System.Drawing.Point(161, 134); + this.isGeneratorValidateClass.Name = "isGeneratorValidateClass"; + this.isGeneratorValidateClass.Size = new System.Drawing.Size(15, 14); + this.isGeneratorValidateClass.TabIndex = 86; + this.isGeneratorValidateClass.UseVisualStyleBackColor = true; + // + // label8 + // + this.label8.AutoSize = true; + this.label8.Location = new System.Drawing.Point(54, 167); + this.label8.Name = "label8"; + this.label8.Size = new System.Drawing.Size(113, 12); + this.label8.TabIndex = 89; + this.label8.Text = "是否生成动态表名:"; + // + // isGeneratorDynamicTableName + // + this.isGeneratorDynamicTableName.AutoSize = true; + this.isGeneratorDynamicTableName.Location = new System.Drawing.Point(173, 167); + this.isGeneratorDynamicTableName.Name = "isGeneratorDynamicTableName"; + this.isGeneratorDynamicTableName.Size = new System.Drawing.Size(15, 14); + this.isGeneratorDynamicTableName.TabIndex = 88; + this.isGeneratorDynamicTableName.UseVisualStyleBackColor = true; + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1084, 778); + this.ClientSize = new System.Drawing.Size(1065, 778); + this.Controls.Add(this.label8); + this.Controls.Add(this.isGeneratorDynamicTableName); + this.Controls.Add(this.label7); + this.Controls.Add(this.isGeneratorValidateClass); + this.Controls.Add(this.txt_database); + this.Controls.Add(this.databaseLable); this.Controls.Add(this.label6); this.Controls.Add(this.isGenOperationColumn); this.Controls.Add(this.label5); @@ -265,6 +333,12 @@ private System.Windows.Forms.Label label5; private System.Windows.Forms.CheckBox isGenOperationColumn; private System.Windows.Forms.Label label6; + private System.Windows.Forms.ComboBox txt_database; + private System.Windows.Forms.Label databaseLable; + private System.Windows.Forms.Label label7; + private System.Windows.Forms.CheckBox isGeneratorValidateClass; + private System.Windows.Forms.Label label8; + private System.Windows.Forms.CheckBox isGeneratorDynamicTableName; } } diff --git a/DatabasetoEntityclass/Form1.cs b/DatabasetoEntityclass/Form1.cs index f625df6..d17ff42 100644 --- a/DatabasetoEntityclass/Form1.cs +++ b/DatabasetoEntityclass/Form1.cs @@ -4,6 +4,7 @@ using System.IO; using System.Windows.Forms; using GenerateClass.Util; using JinianNet.JNTemplate; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace GenerateClass { @@ -14,12 +15,28 @@ namespace GenerateClass InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) + { + loadControl(); + } + private string tableName; + + #region 加载控件 + private void loadControl() + { + loadTableNameControl(); + loadDataBaseNameControl(); + } + /// + /// 加载表名控件 + /// + private void loadTableNameControl() { this.txt_tablename.Items.AddRange(DbUtil.GetTableName().ToArray()); - this.txt_tablename.TextUpdate += (childSender, childE) => { + this.txt_tablename.TextUpdate += (childSender, childE) => + { string str = this.txt_tablename.Text; //获取输入内容 List sList = DbUtil.GetTableName(str); //存放数据库查询结果 - //提前下拉,以显示搜索结果(必须要在添加项之前下拉,否则会将第一项自动添加到编辑框内 覆盖掉输入的内容) + //提前下拉,以显示搜索结果(必须要在添加项之前下拉,否则会将第一项自动添加到编辑框内 覆盖掉输入的内容) this.txt_tablename.DroppedDown = true; //显示下拉列表,但是显示后鼠标指针就不见了 Cursor.Current = Cursors.Default; //将指针显示出来 //在表中已录入名字中寻找包含输入内容的项 有则添加到comboBox项中 @@ -27,24 +44,76 @@ namespace GenerateClass this.txt_tablename.Items.AddRange(sList.ToArray()); this.txt_tablename.Select(this.txt_tablename.Text.Length, 0); }; - } + /// + /// 加载数据库名控件 + /// + private void loadDataBaseNameControl() + { + this.txt_database.Items.AddRange(DbUtil.GetDataBaseName().ToArray()); + this.txt_database.TextUpdate += (childSender, childE) => + { + string str = this.txt_database.Text; //获取输入内容 + List sList = DbUtil.GetDataBaseName(str); //存放数据库查询结果 + //提前下拉,以显示搜索结果(必须要在添加项之前下拉,否则会将第一项自动添加到编辑框内 覆盖掉输入的内容) + this.txt_database.DroppedDown = true; //显示下拉列表,但是显示后鼠标指针就不见了 + Cursor.Current = Cursors.Default; //将指针显示出来 + //在表中已录入名字中寻找包含输入内容的项 有则添加到comboBox项中 + this.txt_database.Items.Clear(); + this.txt_database.Items.AddRange(sList.ToArray()); + this.txt_database.Select(this.txt_database.Text.Length, 0); + this.txt_tablename.Items.Clear(); + }; + this.txt_database.Text = DbHelperMySQL.DbName; + } + #endregion private void modelGen_Click(object sender, EventArgs e) { + if (validateTableName()) + { + return; + } fillModelTemplate(); } private void dgvGen1_Click(object sender, EventArgs e) { + if (validateTableName()) + { + return; + } fillDgv1Template(); } private void dgvGen2_Click(object sender, EventArgs e) { + if (validateTableName()) + { + return; + } fillDgv2Template(); } private void dgvGen3_Click(object sender, EventArgs e) { + if (validateTableName()) + { + return; + } fillDgv3Template(); } + private bool validateTableName() + { + string tbname = txt_tablename.Text.Trim();//要生成的表名 + if (string.IsNullOrEmpty(tbname)) + { + MessageBox.Show("请选择表"); + return true; + } + if (tableName != tbname) + { + MessageBox.Show("请选择正确的表"); + return true; + } + return false; + } private void fillModelTemplate() { string tbname = txt_tablename.Text.Trim();//要生成的表名 @@ -59,6 +128,9 @@ namespace GenerateClass template.Set("sql_table_name", tbname); template.Set("table_name", DbUtil.GenerateClassName(tbname)); template.Set("filedInfos", filedInfos); + template.Set("dataBaseName", DbHelperMySQL.DbName); + template.Set("isGeneratorValidateClass", isGeneratorValidateClass.Checked); + template.Set("isGeneratorDynamicTableName", isGeneratorDynamicTableName.Checked); template.SetStaticType("string", typeof(string)); var result = template.Render(); this.txt_ret.Text = result; @@ -82,6 +154,7 @@ namespace GenerateClass template.Set("dgv_name", dgvName); template.Set("filedInfos", filedInfos); template.Set("timeFiledStr", DbUtil.joinList(timeFileds)); + template.Set("dataBaseName", DbHelperMySQL.DbName); template.SetStaticType("string", typeof(string)); var result = template.Render(); this.textBox1.Text = result; @@ -102,6 +175,7 @@ namespace GenerateClass template.Set("dgv_name", dgvName); template.Set("isGenOperationColumn", isGenOperationColumn.Checked); template.Set("timeFiledStr", DbUtil.joinList(timeFileds)); + template.Set("dataBaseName", DbHelperMySQL.DbName); template.SetStaticType("string", typeof(string)); var result = template.Render(); this.textBox1.Text = result; @@ -124,10 +198,29 @@ namespace GenerateClass template.Set("filedInfos", filedInfos); template.Set("isGenOperationColumn", isGenOperationColumn.Checked); template.Set("timeFiledStr", DbUtil.joinList(timeFileds)); + template.Set("dataBaseName", DbHelperMySQL.DbName); template.SetStaticType("string", typeof(string)); var result = template.Render(); this.textBox1.Text = result; } } + + private void txt_tablename_SelectionChangeCommitted(object sender, EventArgs e) + { + // 获取选中的值 + string selectedValue = txt_tablename.SelectedItem.ToString(); + tableName = selectedValue; + } + + private void txt_database_SelectionChangeCommitted(object sender, EventArgs e) + { + this.txt_tablename.Items.Clear(); + // 获取选中的值 + string selectedValue = txt_database.SelectedItem.ToString(); + DbHelperMySQL.DbName = selectedValue; + this.txt_tablename.SelectedIndex = -1; + this.txt_tablename.Text = ""; + this.txt_tablename.Items.AddRange(DbUtil.GetTableName().ToArray()); + } } } diff --git a/DatabasetoEntityclass/Template/ModelFile.template b/DatabasetoEntityclass/Template/ModelFile.template index e1fd474..00b8ac1 100644 --- a/DatabasetoEntityclass/Template/ModelFile.template +++ b/DatabasetoEntityclass/Template/ModelFile.template @@ -27,7 +27,16 @@ $end public ${filedInfo["type_name"]} ${filedInfo["field_name"]} { get; set; } $end +$if(isGeneratorDynamicTableName) + static ${table_name}(){ + ${table_name}.DynamicTableName += () => + { + return $"${dataBaseName}.{_tableName}"; + }; + } +$end } +$if(isGeneratorValidateClass) /// /// 验证类 采用FluentValidation /// 项目地址 https://github.com/FluentValidation/FluentValidation @@ -44,4 +53,5 @@ $end $end } } +$end } \ No newline at end of file diff --git a/DatabasetoEntityclass/Util/DbUtil.cs b/DatabasetoEntityclass/Util/DbUtil.cs index 7b0db06..2cc802c 100644 --- a/DatabasetoEntityclass/Util/DbUtil.cs +++ b/DatabasetoEntityclass/Util/DbUtil.cs @@ -65,7 +65,8 @@ namespace GenerateClass.Util public static List getTimeFields(List> filedInfos) { var list = new List(); - filedInfos.ForEach(info => { + filedInfos.ForEach(info => + { if (info["type_name"] == "DateTime") list.Add(info["sql_field_name"]); }); return list; @@ -100,7 +101,14 @@ namespace GenerateClass.Util var dt = DbHelperMySQL.Query(String.Format(sql, tabname, DbHelperMySQL.DbName)); return dt; } + public static string GetTableComment(string tableName) + { + string sql = $"SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{DbHelperMySQL.DbName}' AND TABLE_NAME ='{tableName}';"; + var dt = DbHelperMySQL.Query(sql); + if (dt.Rows.Count < 1) return tableName; + return dt.Rows[0]["TABLE_COMMENT"].ToString(); + } public static List GetTableName(string tableName) { @@ -115,14 +123,6 @@ namespace GenerateClass.Util return sList; //return dt; } - public static string GetTableComment(string tableName) - { - string sql = $"SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{DbHelperMySQL.DbName}' AND TABLE_NAME ='{tableName}';"; - var dt = DbHelperMySQL.Query(sql); - - if (dt.Rows.Count < 1) return tableName; - return dt.Rows[0]["TABLE_COMMENT"].ToString(); - } public static List GetTableName() { @@ -136,6 +136,18 @@ namespace GenerateClass.Util } return sList; } + public static List GetDataBaseName(string dataBaseName = null) + { + string sql = $"SHOW DATABASES {(string.IsNullOrEmpty(dataBaseName) ? "" : $"LIKE '%{dataBaseName}%'")} ;"; + var dt = DbHelperMySQL.Query(sql); + //查询数据库表中所有已录入人员 + List sList = new List(); //存放数据库查询结果 + for (int i = 0; i < dt.Rows.Count; i++) + { + sList.Add(dt.Rows[i][$"Database{(string.IsNullOrEmpty(dataBaseName) ? "" : $" (%{dataBaseName}%)")}"].ToString()); + } + return sList; + } public static Dictionary DbTypeDic { get; } = new Dictionary(StringComparer.OrdinalIgnoreCase) {