From 1871d2b624e1c33d185fd5239799d3941f5e36f8 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 20 十月 2022 14:38:22 +0800
Subject: [PATCH] 1

---
 JavaCode/BsTemplate/Entity.java     |    1 
 JavaCode/BsTemplate/Mapper.java     |    9 -
 JavaCode/FrmMyBatisPlus.Designer.cs |   89 ++++++++++--
 JavaCode/FrmMyBatisPlus.cs          |  276 ++++++++++++++++++++++++++++++++++++++
 JavaCode/JavaCode.csproj            |    1 
 5 files changed, 345 insertions(+), 31 deletions(-)

diff --git a/JavaCode/BsTemplate/Entity.java b/JavaCode/BsTemplate/Entity.java
index 1eaf19f..218da43 100644
--- a/JavaCode/BsTemplate/Entity.java
+++ b/JavaCode/BsTemplate/Entity.java
@@ -15,7 +15,6 @@
  */
 @Data
 @AllArgsConstructor
-@NoArgsConstructor
 @TableName("{tabName}")
 public class {entityName} implements Serializable {
     private static final long serialVersionUID = {uid}L;
diff --git a/JavaCode/BsTemplate/Mapper.java b/JavaCode/BsTemplate/Mapper.java
index 774d0d6..2e931d9 100644
--- a/JavaCode/BsTemplate/Mapper.java
+++ b/JavaCode/BsTemplate/Mapper.java
@@ -14,12 +14,5 @@
 @Mapper
 @Repository
 public interface {mapperName} extends BaseMapper<{entityName}> {
-    /**
-     * 鏌ヨ涓�鏉�
-     *
-     * @param {id}
-     * @return
-     */
-    @Select("{selectSql}")
-    {entityName} selectOne(@Param("{id}") Integer {id});
+{wkt}
 }
diff --git a/JavaCode/FrmMyBatisPlus.Designer.cs b/JavaCode/FrmMyBatisPlus.Designer.cs
index 950f38a..90a7e0d 100644
--- a/JavaCode/FrmMyBatisPlus.Designer.cs
+++ b/JavaCode/FrmMyBatisPlus.Designer.cs
@@ -37,10 +37,14 @@
             this.label3 = new System.Windows.Forms.Label();
             this.txtMapperNS = new System.Windows.Forms.TextBox();
             this.label2 = new System.Windows.Forms.Label();
-            this.btnGenerate = new System.Windows.Forms.Button();
+            this.btnGeneAll = new System.Windows.Forms.Button();
             this.btnAll = new System.Windows.Forms.Button();
             this.btnAnti = new System.Windows.Forms.Button();
             this.btnNone = new System.Windows.Forms.Button();
+            this.btnGeneEntity = new System.Windows.Forms.Button();
+            this.btnGeneMapper = new System.Windows.Forms.Button();
+            this.txtTabNS = new System.Windows.Forms.TextBox();
+            this.label1 = new System.Windows.Forms.Label();
             ((System.ComponentModel.ISupportInitialize)(this.dgvTab)).BeginInit();
             this.SuspendLayout();
             // 
@@ -105,17 +109,17 @@
             // txtEntityNS
             // 
             this.txtEntityNS.Font = new System.Drawing.Font("瀹嬩綋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.txtEntityNS.Location = new System.Drawing.Point(525, 61);
+            this.txtEntityNS.Location = new System.Drawing.Point(525, 89);
             this.txtEntityNS.Name = "txtEntityNS";
             this.txtEntityNS.Size = new System.Drawing.Size(312, 26);
             this.txtEntityNS.TabIndex = 30;
-            this.txtEntityNS.Text = "com.lf.server.entity.db";
+            this.txtEntityNS.Text = "com.lf.server.entity.bd";
             // 
             // label3
             // 
             this.label3.AutoSize = true;
             this.label3.Font = new System.Drawing.Font("瀹嬩綋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.label3.Location = new System.Drawing.Point(385, 66);
+            this.label3.Location = new System.Drawing.Point(385, 94);
             this.label3.Name = "label3";
             this.label3.Size = new System.Drawing.Size(136, 16);
             this.label3.TabIndex = 29;
@@ -124,32 +128,32 @@
             // txtMapperNS
             // 
             this.txtMapperNS.Font = new System.Drawing.Font("瀹嬩綋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.txtMapperNS.Location = new System.Drawing.Point(525, 15);
+            this.txtMapperNS.Location = new System.Drawing.Point(525, 53);
             this.txtMapperNS.Name = "txtMapperNS";
             this.txtMapperNS.Size = new System.Drawing.Size(312, 26);
             this.txtMapperNS.TabIndex = 32;
-            this.txtMapperNS.Text = "com.lf.server.mapper.db";
+            this.txtMapperNS.Text = "com.lf.server.mapper.bd";
             // 
             // label2
             // 
             this.label2.AutoSize = true;
             this.label2.Font = new System.Drawing.Font("瀹嬩綋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.label2.Location = new System.Drawing.Point(385, 20);
+            this.label2.Location = new System.Drawing.Point(385, 58);
             this.label2.Name = "label2";
             this.label2.Size = new System.Drawing.Size(136, 16);
             this.label2.TabIndex = 31;
             this.label2.Text = "Mapper鍚嶇О绌洪棿锛�";
             // 
-            // btnGenerate
+            // btnGeneAll
             // 
-            this.btnGenerate.Font = new System.Drawing.Font("瀹嬩綋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.btnGenerate.Location = new System.Drawing.Point(525, 115);
-            this.btnGenerate.Name = "btnGenerate";
-            this.btnGenerate.Size = new System.Drawing.Size(312, 25);
-            this.btnGenerate.TabIndex = 33;
-            this.btnGenerate.Text = "鐢� 鎴� 鏂� 浠�";
-            this.btnGenerate.UseVisualStyleBackColor = true;
-            this.btnGenerate.Click += new System.EventHandler(this.btnGenerate_Click);
+            this.btnGeneAll.Font = new System.Drawing.Font("瀹嬩綋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.btnGeneAll.Location = new System.Drawing.Point(525, 208);
+            this.btnGeneAll.Name = "btnGeneAll";
+            this.btnGeneAll.Size = new System.Drawing.Size(312, 25);
+            this.btnGeneAll.TabIndex = 33;
+            this.btnGeneAll.Text = "鐢� 鎴� 鎵� 鏈� 鏂� 浠�";
+            this.btnGeneAll.UseVisualStyleBackColor = true;
+            this.btnGeneAll.Click += new System.EventHandler(this.btnGeneAll_Click);
             // 
             // btnAll
             // 
@@ -184,15 +188,60 @@
             this.btnNone.UseVisualStyleBackColor = true;
             this.btnNone.Click += new System.EventHandler(this.btnNone_Click);
             // 
+            // btnGeneEntity
+            // 
+            this.btnGeneEntity.Font = new System.Drawing.Font("瀹嬩綋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.btnGeneEntity.Location = new System.Drawing.Point(525, 169);
+            this.btnGeneEntity.Name = "btnGeneEntity";
+            this.btnGeneEntity.Size = new System.Drawing.Size(312, 25);
+            this.btnGeneEntity.TabIndex = 38;
+            this.btnGeneEntity.Text = "鐢� 鎴� Entity.java";
+            this.btnGeneEntity.UseVisualStyleBackColor = true;
+            this.btnGeneEntity.Click += new System.EventHandler(this.btnGeneEntity_Click);
+            // 
+            // btnGeneMapper
+            // 
+            this.btnGeneMapper.Font = new System.Drawing.Font("瀹嬩綋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.btnGeneMapper.Location = new System.Drawing.Point(525, 128);
+            this.btnGeneMapper.Name = "btnGeneMapper";
+            this.btnGeneMapper.Size = new System.Drawing.Size(312, 25);
+            this.btnGeneMapper.TabIndex = 37;
+            this.btnGeneMapper.Text = "鐢� 鎴� Mapper.java";
+            this.btnGeneMapper.UseVisualStyleBackColor = true;
+            this.btnGeneMapper.Click += new System.EventHandler(this.btnGeneMapper_Click);
+            // 
+            // txtTabNS
+            // 
+            this.txtTabNS.Font = new System.Drawing.Font("瀹嬩綋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.txtTabNS.Location = new System.Drawing.Point(525, 15);
+            this.txtTabNS.Name = "txtTabNS";
+            this.txtTabNS.Size = new System.Drawing.Size(312, 26);
+            this.txtTabNS.TabIndex = 40;
+            this.txtTabNS.Text = "bd.";
+            // 
+            // label1
+            // 
+            this.label1.AutoSize = true;
+            this.label1.Font = new System.Drawing.Font("瀹嬩綋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.label1.Location = new System.Drawing.Point(433, 20);
+            this.label1.Name = "label1";
+            this.label1.Size = new System.Drawing.Size(88, 16);
+            this.label1.TabIndex = 39;
+            this.label1.Text = "琛ㄥ悕鍓嶇紑锛�";
+            // 
             // FrmMyBatisPlus
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(852, 628);
+            this.Controls.Add(this.txtTabNS);
+            this.Controls.Add(this.label1);
+            this.Controls.Add(this.btnGeneEntity);
+            this.Controls.Add(this.btnGeneMapper);
             this.Controls.Add(this.btnNone);
             this.Controls.Add(this.btnAnti);
             this.Controls.Add(this.btnAll);
-            this.Controls.Add(this.btnGenerate);
+            this.Controls.Add(this.btnGeneAll);
             this.Controls.Add(this.txtMapperNS);
             this.Controls.Add(this.label2);
             this.Controls.Add(this.txtEntityNS);
@@ -223,9 +272,13 @@
         private System.Windows.Forms.Label label3;
         private System.Windows.Forms.TextBox txtMapperNS;
         private System.Windows.Forms.Label label2;
-        private System.Windows.Forms.Button btnGenerate;
+        private System.Windows.Forms.Button btnGeneAll;
         private System.Windows.Forms.Button btnAll;
         private System.Windows.Forms.Button btnAnti;
         private System.Windows.Forms.Button btnNone;
+        private System.Windows.Forms.Button btnGeneEntity;
+        private System.Windows.Forms.Button btnGeneMapper;
+        private System.Windows.Forms.TextBox txtTabNS;
+        private System.Windows.Forms.Label label1;
     }
 }
\ No newline at end of file
diff --git a/JavaCode/FrmMyBatisPlus.cs b/JavaCode/FrmMyBatisPlus.cs
index 9958b53..4abbf6c 100644
--- a/JavaCode/FrmMyBatisPlus.cs
+++ b/JavaCode/FrmMyBatisPlus.cs
@@ -3,9 +3,12 @@
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
+using System.Diagnostics;
 using System.Drawing;
+using System.IO;
 using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 
@@ -31,7 +34,8 @@
         }
         #endregion
 
-        // 135502,69701  29257,20582
+
+        #region 鏌ヨ琛ㄧ粨鏋勶細135502,69701  29257,20582
         private void btnReadTab_Click(object sender, EventArgs e)
         {
             try
@@ -39,7 +43,7 @@
                 string tabName = this.txtTabPre.Text.Trim();
                 string tabFilter = string.IsNullOrEmpty(tabName) ? "" : string.Format("and c.relname like '{0}%'", tabName);
 
-                string sql = string.Format("select c.relname as \"tab\", cast(obj_description(c.oid) as varchar) as \"desc\", a.attnum as \"num\", a.attname as \"col\",concat_ws('', t.typname, SUBSTRING(format_type(a.atttypid, a.atttypmod) from '(.*)')) as \"type\", d.description as \"bak\" from pg_attribute a left join pg_description d on d.objoid = a.attrelid and d.objsubid = a.attnum left join pg_class c on a.attrelid = c.oid left join pg_type t on a.atttypid = t.oid where a.attnum >= 0 and reltype>0 and relnamespace in ({0}) {1} order by c.relname desc, a.attnum asc", this.txtNS.Text.Trim(), tabFilter);
+                string sql = string.Format("select c.relname \"tab\", cast(obj_description(c.oid) as varchar) \"desc\", a.attnum \"num\", a.attname \"col\", t.typname \"type\", d.description \"bak\" from pg_attribute a left join pg_description d on d.objoid = a.attrelid and d.objsubid = a.attnum left join pg_class c on a.attrelid = c.oid left join pg_type t on a.atttypid = t.oid where a.attnum >= 0 and reltype>0 and relnamespace in ({0}) {1} order by c.relname desc, a.attnum asc", this.txtNS.Text.Trim(), tabFilter);
 
                 DataTable dt = _dbHelper.GetDataTable(sql, null);
                 _list = ModelHandler.FillModel<TabInfo>(dt);
@@ -102,10 +106,273 @@
                 this.dgvTab.CurrentCell = this.dgvTab.Rows[1].Cells[0];
             }
         }
+        #endregion
 
-        private void btnGenerate_Click(object sender, EventArgs e)
+        #region 鎸夐挳浜嬩欢
+        private void btnGeneMapper_Click(object sender, EventArgs e)
         {
-            //
+            try
+            {
+                string path = Path.Combine(baseDir, "BsGenerate\\Mapper");
+
+                List<string> names = GetTabList();
+                foreach (string name in names)
+                {
+                    List<TabInfo> tabs = GetTabInfo(name);
+                    GenerateMapper(path, name, tabs);
+                }
+
+                OpenFolder(path);
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show(ex.Message);
+            }
+        }
+
+        private void btnGeneEntity_Click(object sender, EventArgs e)
+        {
+            try
+            {
+                string path = Path.Combine(baseDir, "BsGenerate\\Entity");
+
+                List<string> names = GetTabList();
+                foreach (string name in names)
+                {
+                    List<TabInfo> tabs = GetTabInfo(name);
+                    GenerateEntity(path, name, tabs);
+                }
+
+                OpenFolder(path);
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show(ex.Message);
+            }
+        }
+
+        private void btnGeneAll_Click(object sender, EventArgs e)
+        {
+            try
+            {
+                string mapperPath = Path.Combine(baseDir, "BsGenerate\\Mapper");
+                string entityPath = Path.Combine(baseDir, "BsGenerate\\Entity");
+
+                List<string> names = GetTabList();
+                foreach (string name in names)
+                {
+                    List<TabInfo> tabs = GetTabInfo(name);
+                    GenerateMapper(mapperPath, name, tabs);
+                    GenerateEntity(entityPath, name, tabs);
+                }
+
+                string path = Path.Combine(baseDir, "BsGenerate");
+                OpenFolder(path);
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show(ex.Message);
+            }
+        }
+        #endregion
+
+        #region 鐢熸垚 Mapper
+        private void GenerateMapper(string path, string name, List<TabInfo> tabs)
+        {
+            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
+
+            string tabName = this.txtTabNS.Text.Trim() + name;
+            string shortName = NameConvert(name, true);
+
+            string mapperNS = this.txtMapperNS.Text.Trim();
+            string mapperName = shortName + "Mapper";
+
+            string entityNS = this.txtEntityNS.Text.Trim();
+            string entityName = shortName + "Entity";
+
+            string bak = string.IsNullOrWhiteSpace(tabs[0].desc) ? shortName : tabs[0].desc.Replace("琛�", "");
+
+            string wkt = this.HasGeom(tabs) ?
+                    "    /**\r\n" +
+                    "     * 鏍规嵁ID鏌ヨWKT\r\n" +
+                    "     *\r\n" +
+                    "     * @param gid\r\n" +
+                    "     * @return\r\n" +
+                    "     */\r\n" +
+                    "    @Select(\"select st_astext(geom) geom from " + tabName + " where gid = #{gid}\")\r\n" +
+                    "    String selectWktById(@Param(\"gid\") Integer gid);" : "";
+
+            string xml = File.ReadAllText(Path.Combine(baseDir, "BsTemplate\\Mapper.java"));
+            xml = xml
+                .Replace("{mapperNS}", mapperNS)
+                .Replace("{mapperName}", mapperName)
+                .Replace("{entityNS}", entityNS)
+                .Replace("{entityName}", entityName)
+                .Replace("{wkt}", wkt)
+                .Replace("{bak}", bak);
+
+            string fileName = shortName + "Mapper.java";
+            File.WriteAllText(Path.Combine(path, fileName), xml);
+        }
+        #endregion
+
+        #region 鐢熸垚 Entity
+        private void GenerateEntity(string path, string name, List<TabInfo> tabs)
+        {
+            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
+
+            string tabName = this.txtTabNS.Text.Trim() + name;
+            string shortName = NameConvert(name, true);
+
+            string entityNS = this.txtEntityNS.Text.Trim();
+            string entityName = shortName + "Entity";
+
+            string bak = string.IsNullOrWhiteSpace(tabs[0].desc) ? shortName : tabs[0].desc.Replace("琛�", "");
+            long uid = (long)Math.Floor((new Random()).NextDouble() * 1000000000000000000D);
+
+            string xml = File.ReadAllText(Path.Combine(baseDir, "BsTemplate\\Entity.java"));
+            xml = xml
+                .Replace("{entityNS}", entityNS)
+                .Replace("{entityName}", entityName)
+                .Replace("{bak}", bak)
+                .Replace("{tabName}", tabName)
+                .Replace("{uid}", uid.ToString());
+
+            StringBuilder sb = new StringBuilder();
+            foreach (TabInfo ti in tabs)
+            {
+                string type = GetJavaType(ti);
+                if (ti.col == "gid") sb.Append("\r\n    @TableId(type = IdType.AUTO)");
+                if (ti.col == "geom") sb.Append("\r\n    @TableField(select = false)");
+                if (ti.col == "class")
+                {
+                    sb.Append("\r\n    @TableField(value = \"class\")");
+                    ti.col = "clazz";
+                }
+
+                string colName = NameConvert(ti.col, false);
+                sb.Append("\r\n    private " + type + " " + colName + ";\r\n");
+            }
+            sb.Append("\r\n    public " + entityName + "() {\r\n    }\r\n");
+
+            foreach (TabInfo ti in tabs)
+            {
+                string type = GetJavaType(ti);
+                string col1 = NameConvert(ti.col, true);
+                string col2 = NameConvert(ti.col, false);
+
+                sb.Append("\r\n    public " + type + " get" + col1 + "() {\r\n        return " + col2 + ";\r\n    }\r\n");
+                sb.Append("\r\n    public void set" + col1 + "(" + type + " " + col2 + ") {\r\n        this." + col2 + " = " + col2 + ";\r\n    }\r\n");
+            }
+            sb.Append("}\r\n");
+            sb.Insert(0, xml);
+
+            string fileName = shortName + "Entity.java";
+            File.WriteAllText(Path.Combine(path, fileName), sb.ToString());
+        }
+        #endregion
+
+        #region 鍏叡鏂规硶
+        private List<string> GetTabList()
+        {
+            List<string> list = new List<string>();
+            for (int i = 0, c = this.tabList.SelectedItems.Count; i < c; i++)
+            {
+                string item = this.tabList.SelectedItems[i] as String;
+                if (!string.IsNullOrEmpty(item))
+                {
+                    list.Add(item);
+                }
+            }
+
+            return list;
+        }
+
+        private List<TabInfo> GetTabInfo(string name)
+        {
+            List<TabInfo> tabs = (from p in _list where p.tab == name orderby p.num select p).ToList<TabInfo>();
+
+            return tabs;
+        }
+
+        private static string NameConvert(string name, bool firstUpper)
+        {
+            string[] strs = name.Split(new char[] { '_' });
+
+            string str = "";
+            for (int i = 0, c = strs.Length; i < c; i++)
+            {
+                if (i == 0 && !firstUpper)
+                {
+                    str += strs[i];
+                    continue;
+                }
+                str += ToUpperFirst(strs[i]);
+            }
+
+            return str;
+        }
+
+        public static string ToUpperFirst(string str)
+        {
+            return Regex.Replace(str, @"^\w", t => t.Value.ToUpper());
+        }
+
+        private string GetJavaType(TabInfo ti)
+        {
+            switch (ti.type)
+            {
+                case "timestamp":
+                    return "Timestamp";
+                case "float4": // float
+                    return "Float";
+                case "float8": // double
+                    return "Double";
+                case "bool": // boolean
+                    return "Boolean";
+                case "numeric":
+                    return "BigDecimal";
+                case "int8": // long
+                    return "Long";
+                case "int2":
+                case "int4": // int
+                    return "Integer";
+                default:
+                    return "String";
+            }
+        }
+
+        private bool HasGeom(List<TabInfo> tabs)
+        {
+            foreach (TabInfo ti in tabs)
+            {
+                if (ti.col == "geom") return true;
+            }
+
+            return false;
+        }
+
+        private static void OpenFolder(string folderPath)
+        {
+            if (string.IsNullOrEmpty(folderPath)) return;
+
+            Process process = new Process();
+            ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
+            psi.Arguments = folderPath;
+            process.StartInfo = psi;
+
+            try
+            {
+                process.Start();
+            }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
+            finally
+            {
+                process.Close();
+            }
         }
 
         private String QueryPrimaryKey(string tab)
@@ -116,5 +383,6 @@
 
             return obj == null ? null : obj.ToString();
         }
+        #endregion
     }
 }
diff --git a/JavaCode/JavaCode.csproj b/JavaCode/JavaCode.csproj
index 95aa489..f7ab109 100644
--- a/JavaCode/JavaCode.csproj
+++ b/JavaCode/JavaCode.csproj
@@ -82,6 +82,7 @@
     </EmbeddedResource>
     <EmbeddedResource Include="FrmSys.resx">
       <DependentUpon>FrmSys.cs</DependentUpon>
+      <SubType>Designer</SubType>
     </EmbeddedResource>
     <EmbeddedResource Include="FrmMyBatisPlus.resx">
       <DependentUpon>FrmMyBatisPlus.cs</DependentUpon>

--
Gitblit v1.9.3