using JavaCode.cs;
|
using System;
|
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;
|
|
namespace JavaCode
|
{
|
public partial class FrmMyBatisPlus : Form
|
{
|
#region 成员变量+构造函数
|
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
|
|
private PostgreHelper _dbHelper;
|
|
private List<TabInfo> _list;
|
|
public FrmMyBatisPlus()
|
{
|
InitializeComponent();
|
}
|
|
private void FrmMyBatisPlus_Load(object sender, EventArgs e)
|
{
|
_dbHelper = new PostgreHelper(DbEnum.langfang);
|
}
|
#endregion
|
|
|
#region 查询表结构:135502,69701 29257,20582
|
private void btnReadTab_Click(object sender, EventArgs e)
|
{
|
try
|
{
|
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);
|
|
DataTable dt = _dbHelper.GetDataTable(sql, null);
|
_list = ModelHandler.FillModel<TabInfo>(dt);
|
if (_list == null || _list.Count == 0)
|
{
|
MessageBox.Show("没有查询到数据!");
|
return;
|
}
|
|
List<string> tabList = new List<string>();
|
foreach (TabInfo ti in _list)
|
{
|
if (!tabList.Contains(ti.tab))
|
{
|
tabList.Add(ti.tab);
|
}
|
}
|
this.tabList.DataSource = tabList;
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show(ex.Message);
|
}
|
}
|
|
private void btnAll_Click(object sender, EventArgs e)
|
{
|
for (int i = 0; i < this.tabList.Items.Count; i++)
|
{
|
this.tabList.SetSelected(i, true);
|
}
|
}
|
|
private void btnAnti_Click(object sender, EventArgs e)
|
{
|
for (int i = 0; i < this.tabList.Items.Count; i++)
|
{
|
bool flag = this.tabList.GetSelected(i);
|
this.tabList.SetSelected(i, !flag);
|
}
|
}
|
|
private void btnNone_Click(object sender, EventArgs e)
|
{
|
for (int i = 0; i < this.tabList.Items.Count; i++)
|
{
|
this.tabList.SetSelected(i, false);
|
}
|
}
|
|
private void tabList_SelectedIndexChanged(object sender, EventArgs e)
|
{
|
string tab = this.tabList.SelectedItem as string;
|
List<TabInfo> tabs = (from p in _list where p.tab == tab orderby p.num select p).ToList<TabInfo>();
|
|
this.dgvTab.DataSource = new BindingList<TabInfo>(tabs);
|
if (tabs != null && tabs.Count > 1)
|
{
|
this.dgvTab.Rows[1].Selected = true;
|
this.dgvTab.CurrentCell = this.dgvTab.Rows[1].Cells[0];
|
}
|
}
|
#endregion
|
|
#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)
|
{
|
string sql = string.Format("select pg_attribute.attname as colname from pg_constraint inner join pg_class on pg_constraint.conrelid = pg_class.oid inner join pg_attribute on pg_attribute.attrelid = pg_class.oid and pg_attribute.attnum = pg_constraint.conkey[1] inner join pg_type on pg_type.oid = pg_attribute.atttypid where pg_class.relname = '{0}' and pg_constraint.contype='p'", tab);
|
|
object obj = _dbHelper.GetScalar(sql, null);
|
|
return obj == null ? null : obj.ToString();
|
}
|
#endregion
|
}
|
}
|