首先封装一下NVelocity
新建 VelocityHelper.cs
/// <summary> /// NVelocity模板工具类 VelocityHelper /// </summary> public class VelocityHelper { private VelocityEngine velocity = null; private IContext context = null; /// <summary> /// 构造函数 /// </summary> /// <param name="templatDir">模板文件夹路径</param> public VelocityHelper(string templatDir) { Init(templatDir); } /// <summary> /// 无参数构造函数 /// </summary> public VelocityHelper() { } /// <summary> /// 初始话NVelocity模块 /// </summary> public void Init(string templatDir) { //创建VelocityEngine实例对象 velocity = new VelocityEngine(); //使用设置初始化VelocityEngine ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatDir); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); //模板的缓存设置 props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true); //是否缓存 props.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)30); //缓存时间(秒) velocity.Init(props); //为模板变量赋值 context = new VelocityContext(); } /// <summary> /// 给模板变量赋值 /// </summary> /// <param name="key">模板变量</param> /// <param name="value">模板变量值</param> public void Add(string key, object value) { if (context == null) context = new VelocityContext(); context.Put(key, value); } /// <summary> /// 显示模板 /// </summary> /// <param name="templatFileName">模板文件名</param> public string Display(string templatFileName) { //从文件中读取模板 Template template = velocity.GetTemplate(templatFileName); //合并模板 using (StringWriter writer = new StringWriter()) { template.Merge(context, writer); var str = writer.ToString(); writer.Flush(); writer.Close(); //输出 return str; } } /// <summary> /// 根据模板生成文件 /// </summary> /// <param name="templatFileName"></param> /// <param name="path">保存路径</param> public void CreateFile(string templatFileName, string path, string fileName) { //从文件中读取模板 Template template = velocity.GetTemplate(templatFileName); //合并模板 StringWriter writer = new StringWriter(); template.Merge(context, writer); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } File.WriteAllText(path + "\\" + fileName, writer.ToString(), new System.Text.UTF8Encoding(false)); writer.Write(writer); writer.Flush(); writer.Close(); } }
然后使用
var tool = new Tool(); VelocityHelper vh = new VelocityHelper(); vh.Init(path + "/template/");//模板路径 vh.Add("table", item); vh.Add("tools", tool); vh.Add("tableColumns", context._db.DbMaintenance.GetColumnInfosByTableName(item.Name)); vh.Add("namespace", "Com.Javalc.Bolg"); vh.CreateFile("entity.vm", "E:\\hisWork\\Com.Javalc.Bolg/Com.Javalc.Bolg.Entity/", tool.ReplaceFirst(tool.FirstUpperCase(item.Name),"Tb_").Replace("_","")+ ".cs"); vh.CreateFile("dao.vm", "E:\\hisWork\\Com.Javalc.Bolg/Com.Javalc.Bolg.Respository/ModelRepository/", tool.ReplaceFirst(tool.FirstUpperCase(item.Name), "Tb_").Replace("_", "") + "Repository.cs");
示例模版:
using System; using Newtonsoft.Json; using SqlSugar; namespace ${namespace}.Entity { #if( "${table.Description}" != "" ) /// <summary> /// ${table.Description} /// </summary> #end [Serializable] [SugarTable("${table.Name}")] //对应数据库的ORM_School表 public partial class $tools.ReplaceFirst($tools.FirstUpperCase($table.Name),"Tb_") { #foreach($column in $tableColumns) /// <summary> #if( "${column.ColumnDescription}" != "" ) /// $tools.StrTrim($column.ColumnDescription) #end /// </summary> [JsonProperty("$tools.StrFirstUpperCase(${column.DbColumnName})")] [SugarColumn(ColumnName ="${column.DbColumnName}")] public $tools.ConvertDataType($column.DataType) $tools.StrFirstUpperCase(${column.DbColumnName}) { get; set; } #end } }
评论区