| | |
| | | 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; |
| | | |
| | | namespace JavaCode |
| | |
| | | public partial class FrmMyBatisPlus : Form |
| | | { |
| | | #region 成员变量+构造函数 |
| | | private static List<string> excludes = new List<string>() { "gid", "objectid", "eventid", "dirid", "depid", "verid", "createtime", "createuser", "updateuser", "updatetime", "geom", "shape_leng", "shape_area" }; |
| | | private static List<string> excludes = new List<string>() { "gid", "parentid", "objectid", "eventid", "dirid", "depid", "verid", "createtime", "createuser", "updateuser", "updatetime", "geom", "shape_leng", "shape_area" }; |
| | | |
| | | string baseDir = AppDomain.CurrentDomain.BaseDirectory; |
| | | |
| | |
| | | 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 \"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); |
| | | 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} and position('pg.dropped' in a.attname) = 0 order by c.relname desc, a.attnum asc", this.txtTabNS.Text.Trim(), tabFilter); |
| | | |
| | | DataTable dt = _dbHelper.GetDataTable(sql, null); |
| | | _list = ModelHandler.FillModel<TabInfo>(dt); |
| | |
| | | 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.txtTabPrefix.Text.Trim() + name; |
| | | //string shortName = NameConvert(name, true); |
| | | string shortName = GetEntityName(name, true); |
| | | |
| | | string mapperNS = this.txtMapperNS.Text.Trim(); |
| | | string mapperName = shortName + "Mapper"; |
| | | |
| | | string entityNS = this.txtEntityNS.Text.Trim(); |
| | | string entityName = shortName + "Entity"; |
| | | |
| | | string ns = this.txtNS.Text.Trim(); |
| | | string bak = string.IsNullOrWhiteSpace(tabs[0].desc) ? shortName : tabs[0].desc.Replace("表", ""); |
| | | |
| | | //if (this.HasGeom(tabs)) return; |
| | | string javaFile = this.HasGeom(tabs) ? "GeoMapper.java" : "Mapper.java"; |
| | | string xml = File.ReadAllText(Path.Combine(baseDir, "BsTemplate\\" + javaFile)); |
| | | xml = xml |
| | | .Replace("{mapperNS}", mapperNS) |
| | | .Replace("{mapperName}", mapperName) |
| | | .Replace("{entityNS}", entityNS) |
| | | .Replace("{entityName}", entityName) |
| | | .Replace("{ns}", ns) |
| | | .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.txtTabPrefix.Text.Trim() + name; |
| | | //string shortName = NameConvert(name, true); |
| | | string shortName = GetEntityName(name, true); |
| | | |
| | | string entityNS = this.txtEntityNS.Text.Trim(); |
| | | string entityName = shortName + "Entity"; |
| | | |
| | | string ns = this.txtNS.Text.Trim(); |
| | | string bak = string.IsNullOrWhiteSpace(tabs[0].desc) ? shortName : tabs[0].desc.Replace("表", ""); |
| | | long uid = (long)Math.Floor((new Random()).NextDouble() * 1000000000000000000D); |
| | | |
| | | //if (this.HasGeom(tabs)) return; |
| | | string javaFile = this.HasGeom(tabs) ? "GeoEntity.java" : "Entity.java"; |
| | | string xml = File.ReadAllText(Path.Combine(baseDir, "BsTemplate\\" + javaFile)); |
| | | xml = xml |
| | | .Replace("{entityNS}", entityNS) |
| | | .Replace("{entityName}", entityName) |
| | | .Replace("{bak}", bak) |
| | | .Replace("{tabName}", tabName) |
| | | .Replace("{ns}", ns) |
| | | .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" || ti.col == "shape_leng" || ti.col == "shape_area") |
| | | //{ |
| | | // sb.Append("\r\n @TableField(select = false)"); |
| | | //} |
| | | if (excludes.Contains(ti.col)) continue; |
| | | if (ti.col == "class") |
| | | { |
| | | //sb.Append("\r\n @TableField(value = \"class\")"); |
| | | //ti.col = "clazz"; |
| | | //ti.col = "classes"; |
| | | MessageBox.Show(ti.tab + "." + ti.col); |
| | | return; |
| | | } |
| | | |
| | | sb.Append("\r\n"); |
| | | string colName = NameConvert(ti.col, false); |
| | | if ("references" == colName) sb.Append(" @TableField(value = \"\\\"references\\\"\")").Append("\r\n"); |
| | | sb.Append(" private " + type + " " + colName + ";\r\n"); |
| | | } |
| | | sb.Append("\r\n public " + entityName + "() {\r\n }\r\n"); |
| | | |
| | | foreach (TabInfo ti in tabs) |
| | | { |
| | | if (excludes.Contains(ti.col)) continue; |
| | | |
| | | 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 void btnGeneAll_Click(object sender, EventArgs e) |
| | | { |
| | | try |
| | |
| | | MessageBox.Show(ex.Message); |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | #region 生成 Mapper |
| | | private void GenerateMapper(string path, string name, List<TabInfo> tabs) |
| | | private void txtTabPrefix_TextChanged(object sender, EventArgs e) |
| | | { |
| | | if (!Directory.Exists(path)) Directory.CreateDirectory(path); |
| | | |
| | | string tabName = this.txtTabNS.Text.Trim() + name; |
| | | //string shortName = NameConvert(name, true); |
| | | string shortName = GetEntityName(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 javaFile = this.HasGeom(tabs) ? "GeoMapper.java" : "Mapper.java"; |
| | | string xml = File.ReadAllText(Path.Combine(baseDir, "BsTemplate\\" + javaFile)); |
| | | xml = xml |
| | | .Replace("{mapperNS}", mapperNS) |
| | | .Replace("{mapperName}", mapperName) |
| | | .Replace("{entityNS}", entityNS) |
| | | .Replace("{entityName}", entityName) |
| | | .Replace("{bak}", bak); |
| | | |
| | | string fileName = shortName + "Mapper.java"; |
| | | File.WriteAllText(Path.Combine(path, fileName), xml); |
| | | txtChanged(sender, e); |
| | | } |
| | | #endregion |
| | | |
| | | #region 生成 Entity |
| | | private void GenerateEntity(string path, string name, List<TabInfo> tabs) |
| | | private void txtNS_TextChanged(object sender, EventArgs e) |
| | | { |
| | | if (!Directory.Exists(path)) Directory.CreateDirectory(path); |
| | | txtChanged(sender, e); |
| | | } |
| | | |
| | | string tabName = this.txtTabNS.Text.Trim() + name; |
| | | //string shortName = NameConvert(name, true); |
| | | string shortName = GetEntityName(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 javaFile = this.HasGeom(tabs) ? "GeoEntity.java" : "Entity.java"; |
| | | string xml = File.ReadAllText(Path.Combine(baseDir, "BsTemplate\\" + javaFile)); |
| | | 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" || ti.col == "shape_leng" || ti.col == "shape_area") |
| | | //{ |
| | | // sb.Append("\r\n @TableField(select = false)"); |
| | | //} |
| | | if (excludes.Contains(ti.col)) continue; |
| | | if (ti.col == "class") |
| | | { |
| | | //sb.Append("\r\n @TableField(value = \"class\")"); |
| | | //ti.col = "clazz"; |
| | | //ti.col = "classes"; |
| | | MessageBox.Show(ti.tab); |
| | | return; |
| | | } |
| | | |
| | | 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) |
| | | { |
| | | if (excludes.Contains(ti.col)) continue; |
| | | |
| | | 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()); |
| | | private void txtChanged(object sender, EventArgs e) |
| | | { |
| | | string pre = txtTabPrefix.Text.Replace(".", "").Trim(); |
| | | string ns = txtNS.Text.Trim(); |
| | | txtMapperNS.Text = "com.lf.server.mapper.bd".Replace("lf", ns).Replace("bd", pre); |
| | | txtEntityNS.Text = "com.lf.server.entity.bd".Replace("lf", ns).Replace("bd", pre); |
| | | } |
| | | #endregion |
| | | |
| | |
| | | { |
| | | string[] strs = name.Split(new char[] { '_' }); |
| | | |
| | | string str = ""; |
| | | for (int i = 0, c = strs.Length; i < c; i++) |
| | | { |
| | | if (i == 0 && !firstUpper) |
| | | if (0 == i && !firstUpper) |
| | | { |
| | | str += strs[i]; |
| | | continue; |
| | | } |
| | | |
| | | str += strs[i].Length == 1 ? strs[i] : ToUpperFirst(strs[i]); |
| | | strs[i] = char.IsDigit(strs[i][0]) ? "_" + strs[i] : ToUpperFirst(strs[i]); |
| | | } |
| | | |
| | | return str; |
| | | return string.Join("", strs); |
| | | } |
| | | |
| | | public static string ToUpperFirst(string str) |
| | |
| | | { |
| | | switch (ti.type) |
| | | { |
| | | case "timestamptz": |
| | | case "timestamp": |
| | | return "Timestamp"; |
| | | case "date": |
| | | return "LocalDate"; |
| | | case "time": |
| | | return "LocalTime"; |
| | | case "float4": // float |
| | | return "Float"; |
| | | case "float8": // double |
| | |
| | | case "int2": |
| | | case "int4": // int |
| | | return "Integer"; |
| | | default: |
| | | default: // text, varchar |
| | | return "String"; |
| | | } |
| | | } |