本身作的時候沒有找到詳細的JAVA模版 大多都是C#的,因此本身記錄下,還有不少不懂的,往後慢慢琢磨吧java
以Entity實例爲例子,直接貼代碼,把本身認爲比較鬧心的地方畫個圈圈標記出來sql
首先 設計模版
建立一個cst文件叫作entity.cst,數據庫
<%@ Template Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" %><%--聲明模版--%>函數
<%--須要加載的組件--%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer"%>ui
<%--參數集--%>
<%@ Property Name="NameSpace" Type="System.String" Default="ProjectManage" Category="Text" Description="Namespace for this class" %>
<%@ Property Name="ClassName" Type="System.String" Default="Project" Category="Other" Description="" %>
<%@ Property Name="ObjectName" Type="System.String" Default="projext" Category="Other" %>
<%@ Property Name="TypeName" Type="System.String" Default="projext" Category="Other" %>
<%@ Property Name="TableName" Type="System.String" Default="projext" Category="Other" %>
<%@ Property Name="BaseName" Type="System.String" Default="projext" Category="Other" %>
<%@ Property Name="Table" DeepLoad="True" Type="TableSchema" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, and stored procedures shouldbe based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, theEntire Database will then be generated."%>this
<%@ Assembly Src="Utility.cs" %><%--自定義方法,用於處理數據庫 數據表的下劃線,tb,首字母大寫--%>
<%@ Import Namespace="Common" %>
<%-- 啓用 MAP方式 System-JavaAlias 將數據類型轉換成JAVA數據類型 轉換成C#要啓用System-CSharpAlias --%>
<%@ Map Name="JavaAlias" Src="System-JavaAlias.csmap" Description="Oracle to Java Type Map" %>spa
<%@ Import Namespace="System.IO" %>設計
package <%= NameSpace%>.entity;
// default packageorm
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;對象
/**
* <%= NameSpace%> entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name="<%= TableName%>"<%--文件名--%>
,catalog="<%= BaseName%>"<%--數據庫名--%>
)
public class <%= ClassName%> implements java.io.Serializable {
<%foreach(ColumnSchema col in Table.Columns){ %><%--獲取表的字段 聲明字段--%>
<% if (col.Name == "CreatedTime" || col.Name == "UpdatedTime" || col.Name == "TS") { %>
private Timestamp <%= StringUtil.ToCamelCase(col.Name) %>;
<% } %>
<% else{%>
private <%= JavaAlias[col.SystemType.FullName]%> <%= StringUtil.ToCamelCase(col.Name) %>;
<%}%><%--因爲時間類型由datatime轉換成Timestamp老是不成功 萬不得已作的特殊處理--%>
<% } %>
<%foreach(ColumnSchema col in Table.Columns){ %><%--獲取表的字段 聲明get set方法--%>
<% if (col.Name == "CreatedTime" || col.Name == "UpdatedTime" || col.Name == "TS") { %>
@Column(name="<%= StringUtil.ToCamelCase(col.Name) %>")
public Timestamp get<%= StringUtil.ToPascalCase(col.Name) %>() {
return this.<%= StringUtil.ToCamelCase(col.Name)%>;
}
public void set<%= StringUtil.ToPascalCase(col.Name) %>( Timestamp <%= StringUtil.ToCamelCase(col.Name)%>) {
this.<%= StringUtil.ToCamelCase(col.Name)%> = <%= StringUtil.ToCamelCase(col.Name)%>;
}
<% } %>
<% else{%>
<%= col.IsPrimaryKeyMember ? "@Id @GeneratedValue" : ""%> <%--判斷若是是主鍵 返回該註解(用於在JAVA中映射)--%>
<% if(col.SystemType.FullName == "System.String"){%> <%--判斷是否爲string類型 若是是 返回長度 返回該註解(用於在JAVA中映射)--%>
<% if(col.AllowDBNull==false){%> <%--判斷是否容許爲空 若是不容許 false 返回該註解(用於在JAVA中映射)--%>
@Column(name="<%= col.Name %>",length="<%= col.Size %>",nulllable="<%= col.AllowDBNull%>")
<%} %>
<%else{%>
@Column(name="<%= col.Name %>",length="<%= col.Size %>")
<%} %>
<%} %>
<% else{ %>
<% if(col.AllowDBNull==false){%>
@Column(name="<%= col.Name %>",nulllable="<%= col.AllowDBNull%>")
<%} %>
<%else{%>
@Column(name="<%= col.Name %>")
<%} %>
<%}%>
public <%= JavaAlias[col.SystemType.FullName]%> get<%= StringUtil.ToPascalCase(col.Name) %>() {
return this.<%= StringUtil.ToCamelCase(col.Name)%>;
}
public void set<%= StringUtil.ToPascalCase(col.Name) %>( <%= JavaAlias[col.SystemType.FullName]%> <%= StringUtil.ToCamelCase(col.Name)%>) {
this.<%= StringUtil.ToCamelCase(col.Name)%> = <%= StringUtil.ToCamelCase(col.Name)%>;<%}%>
}
<% } %>
}
建立自定義函數Utility.cs
using System;
using System.Text;
using CodeSmith.Engine;
using SchemaExplorer;
using System.ComponentModel;
using System.Data;
namespace Common
{
/**//// <summary>
/// TemplateRule
/// </summary>
public static class Utility
{
public static string GetClassName(string tableName)
{
string className = tableName.Replace("_", "");
string[] array = tableName.Split('_');
if (array.Length > 1)
{
/*if (array[0] == array[1])
{
className = array[0];
}
else if (array[0] == "Dict" || array[0] == "Wiki" || array[0] == "Ask" || array[0] == "Forum" || array[0] == "Class")
{
className = array[1];
}*/
if (array.Length == 2)
{
className = StringUtil.ToPascalCase(array[1]);
}
else if (array.Length == 3)
{
className = StringUtil.ToPascalCase(array[1]) + StringUtil.ToPascalCase(array[2]);
}
else if (array.Length == 4)
{
className = StringUtil.ToPascalCase(array[1]) + StringUtil.ToPascalCase(array[2]) + StringUtil.ToPascalCase(array[3]);
}
}
return className;
}
public static string GetTypeName(string typeName)
{
typeName = StringUtil.ToPascalCase(typeName);
return typeName;
}
public static string GetObjectName(string tableName)
{
string className = tableName.Replace("_", "");
string[] array = tableName.Split('_');
if (array.Length > 1)
{
if (array.Length == 2)
{
className = array[1];
}
else if (array.Length == 3)
{
className = array[1] + array[2];
}
else if (array.Length == 4)
{
className =array[1] + array[2] + array[3];
}
}
return className;
}
public static string GetProjectName(string tableName)
{
string[] array = tableName.Split('_');
if (array.Length == 2)
{
if (array[0] == "Wiki")
{
return "Wiki";
}
else if (array[0] == "Dict")
{
return "Dict";
}
else if (array[0] == "Ask")
{
return "Ask";
}
else if (array[0] == "Forum")
{
return "Forum";
}
else if (array[0] == "Class")
{
return "Class";
}
}
return "Dialect";
}
}
生成模版 建立cst文件 make_entity.cst
<%--聲明模版--%>
<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%>
<%--註冊提供用戶自定義鏈接的屬性--%>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated." %>
<%--註冊設計模版--%>
<%@ Register Name="EntityTemplate" Template="entity.cst" MergeProperties="Flase" ExcludeProperties=""%>
<%@ Assembly Src="Utility.cs" %><%--自定義方法,用於處理數據庫 數據表的下劃線,tb,首字母大寫--%>
<%@ Import Namespace="Common" %>
<%@ Import Namespace="System.IO" %>
<%--須要加載的組件--%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<script runat="template">
//定義輸出目錄
private string Directory = String.Empty;
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] //聲明屬性
public string OutputDirectory
{
get
{
return Directory;
}
set
{
if (value.EndsWith("\\")) value = value.Substring(0, value.Length -1);
Directory = value;
}
}
</script>
<script runat="template">
private void MakeTemplate()
{
CodeTemplate template = new AuthenServiceTemplate();//獲得一個設計模版對象
string type="entity";
string elementName="{0}{1}";//文件名
foreach(TableSchema table in this.SourceDatabase.Tables)//獲取數據庫中的每一張表
{
string[] array = this.SourceDatabase.Database.Name.Split('_');//去下劃線
string className = Utility.GetClassName(table.Name);//字段格式轉換
string objectName=Utility.GetObjectName(table.Name);
string typeName=Utility.GetTypeName(type);
string projectName = array[1]+".";
string FileDirectory = OutputDirectory+ "\\"+ string.Format(elementName,className,(".java"));//輸出路徑及文件格式
//爲涉及模版對象傳入參數
template.SetProperty("SourceTable", table);
template.SetProperty("NameSpace", projectName);
template.SetProperty("ClassName", className);
template.SetProperty("TableName", table.Name);
template.SetProperty("ObjectName", objectName);
template.SetProperty("BaseName", this.SourceDatabase.Database.Name);
//生成模板
template.SetProperty("Table",table);
//文件輸出
template.RenderToFile(FileDirectory,true);
Debug.WriteLine(FileDirectory +" 建立成功.");
}
}
</script>
<%
this.MakeTemplate();//調用
%>
最後綁定數據庫,制定數據輸出路徑Generate一下,就能夠了
數據類型轉換的時候一度不想作了,真的煩,各類類型不一致,還找不到統一轉換的方法,嘗試一個一個作判斷,腦子裏面就一個想法,去死去死去死好了,不過弄到如今
差很少了心情就就愉快了,路漫漫其修遠兮~吾將上下左右東南西北而求索......