对于.NET平台上的代码生成器来说,codesmith是一个非常好的选择。

以前在学院实验室用的都是SQL server数据库,老师给的一套codesmith模板用来生成model/DAL/BLL很是方便。

不过后来放弃SQL server 投入MySQL之后,刚开始都是手写SQL,还是很痛苦的。

再后来又去找MySQL codesmith模板,这个对应的资料就不多了。不过最后还是找到了一套不错的,凑合能用。起初也懒,codesmith语法不熟,就没想过去修改一下了。

最近又要用到这套东西,于是决定还是去修改一番,更便于使用。

这个文章就主要讲一下修改过程,顺便说一下codesmith的简单语法。

先说一下操作步骤:

把模板的文件夹扔到codesmith模板文件的路径下,接着打开Codesmith,找到刚扔过去的文件夹,选择Main.cst,右键-execute-选择对应的MySQL库-选中表。

(注:codesmith连接MySQL有问题的话,

移步这里解决 CodeSmith 连接MySQL数据库报“can’t find .net framework data provider”

如下图:

然后点击Generate就能顺利生成model/dal/bll了。

生成代码结构如下:

这样操作没什么问题,顺利生成了我们要的model/dal/bll了,然后….我懒嘛。 每次都要把表一个个选一次,麻不麻烦啊。然后就想了,能不能改一下模板呢。于是便开始google相关资料了。找到了几个相关文章,参考这就开始改造了。 先看看原来的Main.cst里面写了撒。

<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8"
TargetLanguage="Text" Src="" Inherits="" Debug="False"
Description="Template description here." Output="None"%>
<%@ Register Name="Models" Template="DBMad.Models.cst" MergeProperties="False" ExcludeProperties="" %>
<%@ Register Name="DAL" Template="DBMad.DAL.cst"
MergeProperties="False" ExcludeProperties="" %>
<%@ Register Name="BLL" Template="DBMad.BLL.cst"
MergeProperties="False" ExcludeProperties="" %><%@ Property Name="SourceTable"
Type="SchemaExplorer.TableSchema" Optional="False"%>
<%@ Property Name="RootNamespace" Default="Net.Itcast.CN"
Type="System.String" Optional="False"%><%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<script runat="template">private string _outputDirectory = String.Empty;[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] [Description("The directory to output the results to.")]public string OutputDirectory {get{       return _outputDirectory;}set{if (value != null && !value.EndsWith("\\")){value += "\\";}_outputDirectory = value;} }
</script>

这一段基本就是在声明选项以及引用命名空间,表现出来的便是我们看到的下图:


<%Models model = this.Create<Models>();model.ModelsNamespace = this.RootNamespace+".Model";model.TargetTable = this.SourceTable;model.RenderToFile(this.OutputDirectory+"Model/"+model.GetFileName(),true);DAL dal = this.Create<DAL>();dal.TargetTable = this.SourceTable;dal.ModelsNamespace = model.ModelsNamespace;dal.DALClassNameSurfix = "DAL";dal.DALNamespace =this.RootNamespace+".DAL";dal.RenderToFile(this.OutputDirectory+"DAL/"+dal.GetFileName(),true);BLL bll = this.Create<BLL>();bll.ModelsNamespace = model.ModelsNamespace;bll.DALClassNameSurfix = dal.DALClassNameSurfix;bll.DALNamespace = dal.DALNamespace;bll.BLLClassNameSurfix = "BLL";bll.BLLNamespace = this.RootNamespace+".BLL";bll.TargetTable = this.SourceTable;bll.RenderToFile(this.OutputDirectory+"BLL/"+bll.GetFileName(),true);Response.Write("ok,see "+this.OutputDirectory);
%>

这一段就是我们点击Generate之后执行的代码,基本功能就是调用
DBMad.Models.cst,DBMad.DAL.cst,DBMad.BLL.cst。
因为在上面声明数据源的时候,使用了SchemaExplorer.TableSchema,导致我们选择表的时候不能多选。代码如下:

<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8"
TargetLanguage="Text" Src="" Inherits="" Debug="False"
Description="Template description here." Output="None"%>
<%@ Property Name="SourceTables"
Type="SchemaExplorer.TableSchemaCollection" Default=""
Optional="False" Category=""%>
<%@ Register Name="SE" Template="CreatSingleTable.cst"
MergeProperties="False" ExcludeProperties="" %>
<%@ Property Name="RootNamespace" Default="Net.Itcast.CN"
Type="System.String" Optional="False"%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections" %>
<script runat="template">private string _outputDirectory = String.Empty;[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] [Description("The directory to output the results to.")]public string OutputDirectory {get{       return _outputDirectory;}set{if (value != null && !value.EndsWith("\\")){value += "\\";}_outputDirectory = value;} }
</script><%
foreach(TableSchema ts in SourceTables)
{
SE s = new SE(); s.SourceTable = ts; s.RootNamespace = RootNamespace;s.OutputDirectory = OutputDirectory;s.Render(this.Response);
}
%>
<script runat="template">
</script>

前面一部分还是一样的声明,

<%
foreach(TableSchema ts in SourceTables)
{
SE s = new SE(); s.SourceTable = ts; s.RootNamespace = RootNamespace;s.OutputDirectory = OutputDirectory;s.Render(this.Response);
}
%>
<script runat="template">
</script>

这一段代码便是获取刚得到的表集合,遍历集合然后依次调用之前的单表生成模板。

到这里差不多已经完成了我要的效果,选择多表,实现一次生成所有的表对应的model/dal/bll。

这个效果基本就是我要的了,但是后来又发现,model里面的字段居然没有注释,我在建表的时候写了字段注释的呀。

打开model的cst文件之后发现,模板并没有做注释这个工作。
代码如下:

<%@ CodeTemplate Language="C#" TargetLanguage="C#"
Src="ToolsCodeTemplate.cs" Inherits="ToolsCodeTemplate"%>
<%@ Property Name="TargetTable" Type="SchemaExplorer.TableSchema"
Category="Context" Description="TargetTable that the object is
based on." %>
<%@ Property Name="ModelsNamespace" Default="Model"
Type="System.String" Category="Context" Description="TargetTable
that the object is based on." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<% PrintHeader(); %>
using System;
using System.Collections.Generic;
using System.Text;namespace <%= ModelsNamespace %>
{   [Serializable()]public class <%= GetModelClassName() %>{<% foreach (ColumnSchema column in TargetTable.Columns){%>private <%= GetPropertyType(column) %>  _<%= GetPropertyName(column) %>;         <%}%><% foreach (ColumnSchema column in TargetTable.Columns){%>public <%= GetPropertyType(column) %> <%= GetPropertyName(column) %>{get { return _<%= GetPropertyName(column) %>; }set { _<%= GetPropertyName(column) %> = value; }}<%}%>  }
}
<script runat="template">
public string GetModelClassName()
{return GetModelClassName(TargetTable);
}public override string GetFileName()
{return this.GetModelClassName(this.TargetTable) + ".cs";
}</script>

获取表中字段名使用的是GetPropertyName(column),咦,在哪实现了这个东西呢?回去翻一下文件,哦,还有一个ToolsCodeTemplate.cs文一直没管呢。

果然,GetPropertyName(column)在这里。

public string GetPropertyName(ColumnSchema column)
{return GetNameFromDBFieldName(column);
}
public string GetNameFromDBFieldName(ColumnSchema column)
{return column.Name;
}

读取列名就是这么简单,那么我们对应写一个函数读取一下列注释,然后再model里面调用一下不好了。

又查了一下资料,

    public string GetColumnComment(ColumnSchema column){return column.Description;}

嗯,理论上这样是可以的…
然而,我想多了。倒腾了好久,这个属性值都是空的…
google了一圈之后发现,原来是SchemaExplorer.MySQLSchemaProvider.dll 里面压根没实现读取列注释的实现….

不过也有对应的解决方法:

完美解决CodeSmith无法获取MySQL表及列Description说明注释的方案

把DLL替换一下就好了。

最后附上模板连接:CodeSmith-for-MySQL-Template

注:

  1. 模板会把MySQL的表名前三个字符截取掉,建议把表明设置为tbl开头,或者自行修改模板文件。
  2. 想让字段注释生效记得替换SchemaExplorer.MySQLSchemaProvider.dll(替换前记得备份!)

CodeSmith for MySQL template相关推荐

  1. CodeSmith连接mysql

    先下载:http://dev.mysql.com/downloads/connector/net/  mysql-connector-net-6.6.4.msi 并安装.在安装目录XX:\XX\MyS ...

  2. mysql template sql_SpringBoot-JdbcTemplates-MySQL

    配置 application.yml spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://lo ...

  3. 调整JVM虚拟机内存大小

    查看全文 http://www.taodudu.cc/news/show-3730693.html 相关文章: [JVM虚拟机]JVM的启动参数设置 JVM 虚拟机详解内部原理(小白必看!) JVM虚 ...

  4. 96.6. Template

    模板的导入步骤是首先点击"Choose File"按钮选择文件 然后点击Import按钮 确认导入事项,最后点击Import按钮. 完成倒入后,配置数据采集脚本,请继续阅读下面章节 ...

  5. k8s mysql数据同步_K8s——MySQL实现数据持久化

    1.搭建nfs存储 [root@docker-k8s01 ~]# yum -y install nfs-utils [root@docker-k8s01 ~]# mkdir /nfsdata/mysq ...

  6. openshift使用_OpenShift v3:使用WildFly和MySQL的Java EE 7入门

    openshift使用 OpenShift是Red Hat的开源PaaS平台. OpenShift v3 (将于今年发布)将提供使用Docker和Kubernetes运行微服务的整体体验. 以经典的R ...

  7. OpenShift v3:使用WildFly和MySQL的Java EE 7入门

    OpenShift是Red Hat的开源PaaS平台. OpenShift v3 (将于今年发布)将提供使用Docker和Kubernetes运行微服务的整体体验. 以经典的Red Hat方式,所有工 ...

  8. 生产中k8s适合mysql_在K8S集群中构建复杂的MySQL单实例数据库

    文档说明 实验环境:kubernetes Version v1.9.6 网络CNI:fannel 存储CSI: NFS Dynamic Class 前期准备 简单部署 (无法满足生产环境要求) 一.持 ...

  9. mysql6获取不到连接_codesmith6.5连接Mysql提示“找不到请求的 .Net Framework Data Provider。可能没有安装。”解决方法...

    系统 centos6.5 Mysql5.6的安装过程 1.http://dev.mysql.com/downloads/mysql/下载tar包 选择系统: Linux - Generic (glib ...

最新文章

  1. Parentheses Balance (括号平衡)---栈
  2. rⅰd的意思_Q345R(HIC)和Q345R(R-HIC)化学成分抗氢板
  3. activity中fragment 返回键不退出_分享一波阿里Android客户端面经,我竟连这都答不上来?...
  4. HuaWeiCloud_model_arts
  5. opencv 获取灰度图像
  6. iis里面的mime没有php扩展,IIS - 无后缀(无扩展名)的MIME类型配置
  7. 短信验证码“最佳实践”
  8. 红帽峰会2015所需的JBoss BPM内容指南
  9. JavaWeb 如何防止表单重复提交 - 使用Token,令牌
  10. Ubuntu中让SSH自动重连
  11. 多分类决策树 r语言_R语言——决策树模型的相关可视化
  12. linux卸载keystone服务,OpenStack —— 认证服务Keystone(二)
  13. EQMX+Nginx集群搭建
  14. 分析网络故障慢慢来!一定要抓到真凶(有关arp)
  15. mysql的insert_MySQL中INSERT的一般用法
  16. java使用bks双向认证_GitHub - wanglijun93/RxHttpUtils: Rxjava+Retrofit封装,便捷使用
  17. Python自动化开发从浅入深-进阶(script,dom,jquery 基础)
  18. 香帅的北大金融学课笔记15 -- 大师投资智慧
  19. 聊聊Java中的TLAB
  20. 如何实现用户id生成一个唯一邀请码

热门文章

  1. 数据分析师年薪58w起 人才缺口极大
  2. ES(elasticsearch) - kibana导出csv
  3. Word2Vec原理之层次Softmax算法(转)
  4. 卷积物理意义 几何意义
  5. 使用AD绘制stm32最小系统版
  6. Windows系统10中“无法枚举容器中的对象 访问被拒绝”解决方案
  7. [转] unity功能开发——实名认证
  8. NFC以及NFC支付简介
  9. [附源码]java毕业设计高校新生报到管理系统
  10. 最大流=最小割 简单证明