CodeSmith简介
为什么要用代码生成器
²代码生成器生成代码可以减少人为手误书写的bug
²可以提高代码开发效率,简化开发流程
²可以使输出文档、代码规范化
代码生成模板编写
生成模板采用asp.net的语法编写,语法简单易懂,对熟悉 asp.net 开发人员很友好。即使没有做个 asp.net 开发也可以简单学习后进行模板编写。
1. 编写模板的基础需要引入数据表,在模板中可以很简单的引入一些生成代码的配置项
1.<%@ CodeTemplate Language="C#" TargetLanguage="Java" Src="" Inherits="" Debug="False" Description="" ResponseEncoding="UTF-8" %>
2.<%@ Property Name="package" Type="System.String" Default="Che168.Model" Optional="False" Category="Strings" Description="实体类命名空间" %>
3.<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Optional="False" Category="Configs" Description="源表" %>
4.<%@ Property Name="SourceTable2" Type="SchemaExplorer.TableSchema" Optional="True" Category="Configs" Description="字段备注来源表" %>
5.<%@ Property Name="SourceTable3" Type="SchemaExplorer.TableSchema" Optional="True" Category="Configs" Description="字段备注来源表" %>
6.<%@ Property Name="SourceTable4" Type="SchemaExplorer.TableSchema" Optional="True" Category="Configs" Description="字段备注来源表" %>
7.<%@ Property Name="IsSpringBoot" Type="System.Boolean" Category="Configs" Description="是否是SpringBoot bean,如果是,则生成api说明备注" %>
1.package <%=package%>import java.util.*;
2. <%if(IsSpringBoot){ %>
3.import io.swagger.annotations.*;
4.<%} %>
5.
6./**
7.* <%=SourceTable.Description%>bean
8.*/
9.public class <%= SourceTable.Name%>
10.{
11. <% foreach (ColumnSchema column in SourceTable.Columns) { %>
12. /**<%= column.Description %>*/
13. <%if(IsSpringBoot){ %>
14. @ApiModelProperty(value = "<%= column.Description %>", name = "<%= column.Name.ToLower() %>")
15. <%} %>
16. <%= GetJavaType(column.DataType) %> <%= column.Name.ToLower() %> ;
17. <% } %>
18.
19. <% foreach (ColumnSchema column in SourceTable.Columns) { %>
20.
21. public void set<%=column.Name.ToLower().ToPascalName() %> (<%= GetJavaType(column.DataType) + " "+column.Name.ToLower() %> ) { this.<%= column.Name.ToLower()+" = "+ column.Name.ToLower()%>; }
22.
23. public <%= GetJavaType(column.DataType) %> get<%=column.Name.ToLower().ToPascalName() %> () { return this.<%= column.Name.ToLower() %>;}
24. <% } %>
25.}
26.
27.<script runat="template">
28./// <summary>
29. /// 由DbType获取对应的Java 的 Type
30. /// </summary>
31. /// <param name="dbtype"></param>
32. /// <returns></returns>
33. public static string GetJavaType(System.Data.DbType dbtype)
34. {
35. switch (dbtype)
36. {
37. case DbType.Int16:
38. case DbType.UInt16:
39. case DbType.Int32:
40. case DbType.UInt32: return "Integer";
41. case DbType.Int64:
42. case DbType.UInt64: return "long";
43. case DbType.Byte: return "Integer";
44. case DbType.Date:
45. case DbType.DateTime:
46. case DbType.DateTime2: return "Date";
47. case DbType.String:
48. case DbType.StringFixedLength:
49. case DbType.AnsiString:
50. case DbType.AnsiStringFixedLength: return "String";
51. case DbType.Decimal: return "BigDecimal";
52. case DbType.Double:
53. //case DbType.Currency: return "Double";
54. case DbType.Currency: return "BigDecimal";
55. default: return "String" ;
56. }
57. }
58.</script>
3. 数据类型转换
1.<script runat="template">
2./// <summary>
3. /// 由DbType获取对应的Java 的 Type
4. /// </summary>
5. /// <param name="dbtype"></param>
6. /// <returns></returns>
7. public static string GetJavaType(System.Data.DbType dbtype)
8. {
9. switch (dbtype)
10. {
11. case DbType.Int16:
12. case DbType.UInt16:
13. case DbType.Int32:
14. case DbType.UInt32: return "Integer";
15. case DbType.Int64:
16. case DbType.UInt64: return "long";
17. case DbType.Byte: return "Integer";
18. case DbType.Date:
19. case DbType.DateTime:
20. case DbType.DateTime2: return "Date";
21. case DbType.String:
22. case DbType.StringFixedLength:
23. case DbType.AnsiString:
24. case DbType.AnsiStringFixedLength: return "String";
25. case DbType.Decimal: return "BigDecimal";
26. case DbType.Double:
27. //case DbType.Currency: return "Double";
28. case DbType.Currency: return "BigDecimal";
29. default: return "String" ;
30. }
31. }
32.</script>
4. 如何提取公共方法
书写完模板我们编译后,模板就可以使用了,生成代码流程图如下:
经验分享
总结
-
官网地址 https://www.codesmithtools.com/
作者|李丙龙
标签:case,基于,return,DbType,生成,CodeSmith,效率,模板 From: https://www.cnblogs.com/88223100/p/Use-CodeSmith-to-improve-development-efficiency.html