`
dawuafang
  • 浏览: 1107772 次
文章分类
社区版块
存档分类
最新评论

基于模板的通用代码生成器LKGenerator(三)-模板示例

 
阅读更多

本代码生成工具提供的是一个生成代码的平台,各种不同形式的模板都可以在其上使用用于生成代码,模板的书写您需要遵循一定的规则(这些规则是velocity语法和本工具提供的变量,您可在上一篇中找到这些变量),只要您的模板符合这些规则就能用于生成代码.类比一下,淘宝为商家提供的是一个买卖交易的平台,各种各样的产品都可以在上面交易,买家和买家在交易时需要遵循一定的交易规则,只要大家都遵从这些规则就能很好的进行交易.当然把本工具比作淘宝显然不太恰当,本代码生成器远远比不上淘宝带给社会的价值,但真心希望本工具能给程序员带来一点点方便,也算她的一点点价值体现吧.

本代码生成工具之所以说具有通用性,在于它能生成各种语言的代码,模板是什么语言生成的代码就是什么语言.下面就举几个不同的模板示例分别生成代码.如果您看了上一篇文章您就应该知道,我们首先需要手写或用工具生成一个java文件,并将其编译为class.如下:

import java.util.*;
import com.tgb.lk.annotation.*;

@AutoBean(alias = "Student",table="t_student")
public class Student {


  @AutoField(alias = "序号", column = "id", isKey = true , isRequired = true , type="String", length=36)
  @ExcelVOAttribute(name = "id", column = "A")
  private String id;

  @AutoField(alias = "姓名", column = "name", isRequired = true, length = 255)
  @ExcelVOAttribute(name = "name", column = "B")
  private String name;

  @AutoField(alias = "性别", column = "sex", combo = {"男","女"}, length = 255)
  @ExcelVOAttribute(name = "sex", column = "C")
  private String sex;

  @AutoField(alias = "年龄", column = "age", type = "Integer")
  @ExcelVOAttribute(name = "age", column = "D")
  private int age;

  @AutoField(alias = "生日", column = "birthday", type = "Date")
  @ExcelVOAttribute(name = "birthday", column = "E")
  private Date birthday;

  @AutoField(alias = "班级ID", column = "clazz_id", length = 36)
  @ExcelVOAttribute(name = "clazz_id", column = "F")
  private String clazzId;

  @AutoField(alias = "是否班长", column = "is_monitor")
  @ExcelVOAttribute(name = "is_monitor", column = "G")
  private Boolean isMonitor;

  @AutoField(alias = "创建", column = "create_time", isRequired = true, type = "Date")
  @ExcelVOAttribute(name = "create_time", column = "H")
  private Date createTime;

  @AutoField(alias = "修改时间", column = "update_time", isRequired = true, type = "Date")
  @ExcelVOAttribute(name = "update_time", column = "I")
  private Date updateTime;



  public String getId() {
    return id;
  }
  public void setId(String id){
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name){
    this.name = name;
  }
  public String getSex() {
    return sex;
  }
  public void setSex(String sex){
    this.sex = sex;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age){
    this.age = age;
  }
  public Date getBirthday() {
    return birthday;
  }
  public void setBirthday(Date birthday){
    this.birthday = birthday;
  }
  public String getClazzId() {
    return clazzId;
  }
  public void setClazzId(String clazzId){
    this.clazzId = clazzId;
  }
  public Boolean getIsMonitor() {
    return isMonitor;
  }
  public void setIsMonitor(Boolean isMonitor){
    this.isMonitor = isMonitor;
  }
  public Date getCreateTime() {
    return createTime;
  }
  public void setCreateTime(Date createTime){
    this.createTime = createTime;
  }
  public Date getUpdateTime() {
    return updateTime;
  }
  public void setUpdateTime(Date updateTime){
    this.updateTime = updateTime;
  }
}


以下模板您只需放到D:\.LKGenerator\templates-var模板根路径下即可运行查看使用效果.

(1) java的模板(${bean.Uname}.java):

package ${base-package}.model;

import java.util.*;
import javax.persistence.*;
import com.tgb.lk.util.base.model.BaseTimeModel;
import com.tgb.lk.util.excel.ExcelVOAttribute;

@Entity
@Table(name = "t_${bean}")
public class ${bean.Uname} extends BaseTimeModel{
	
#set ( $arr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] )

#foreach ($field in ${bean.fields})
#if(${field.isRequired})
	@Column(name = "${field}", nullable = false)
#else
	@Column(name = "${field}")
#end
	@ExcelVOAttribute(name = "${field.alias}", column = "$arr[$velocityCount]")
	private ${field.type} ${field};

#end

	public ${bean.Uname}() {
	}
	
#foreach ($field in ${bean.fields})
	public ${field.type} get${field.Uname}() {
		return ${field};
	}

	public void set${field.Uname}(${field.type} ${field}) {
		this.${field} = ${field};
	}
#end

}
生成的代码(Student.java):

package com.tgb.lk.model;

import java.util.*;
import javax.persistence.*;
import com.tgb.lk.util.base.model.BaseTimeModel;
import com.tgb.lk.util.excel.ExcelVOAttribute;

@Entity
@Table(name = "t_student")
public class Student extends BaseTimeModel{
	

	@Column(name = "id", nullable = false)
	@ExcelVOAttribute(name = "序号", column = "B")
	private String id;

	@Column(name = "name", nullable = false)
	@ExcelVOAttribute(name = "姓名", column = "C")
	private String name;

	@Column(name = "sex")
	@ExcelVOAttribute(name = "性别", column = "D")
	private String sex;

	@Column(name = "age")
	@ExcelVOAttribute(name = "年龄", column = "E")
	private Integer age;

	@Column(name = "birthday")
	@ExcelVOAttribute(name = "生日", column = "F")
	private Date birthday;

	@Column(name = "isMonitor")
	@ExcelVOAttribute(name = "是否班长", column = "G")
	private Boolean isMonitor;

	@Column(name = "createTime", nullable = false)
	@ExcelVOAttribute(name = "创建时间", column = "H")
	private Date createTime;

	@Column(name = "updateTime", nullable = false)
	@ExcelVOAttribute(name = "修改时间", column = "I")
	private Date updateTime;


	public Student() {
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public Boolean getIsMonitor() {
		return isMonitor;
	}

	public void setIsMonitor(Boolean isMonitor) {
		this.isMonitor = isMonitor;
	}
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

}

(2) C#模板(${bean}.cs)

using System;
namespace ${base-package}.Model
{
	/// <summary>
	/// ${bean}:实体类(属性说明自动提取数据库字段的描述信息)
	/// </summary>
	[Serializable]
	public partial class ${bean}
	{
		public ${bean}()
		{}
		#region Model
#foreach($field in ${bean.fields})
#if(${field.type}=="String")
		private string#if(${field.isRequired}==false)?#end _${field};
#end
#if(${field.type}=="Date")
		private DateTime#if(${field.isRequired}==false)?#end _${field};
#end
#if(${field.type}=="Integer")
		private int#if(${field.isRequired}==false)?#end _${field};
#end
#if(${field.type}=="Boolean")
		private bool#if(${field.isRequired}==false)?#end _${field};
#end
#end
#foreach($field in ${bean.fields})
#if(${field.type}=="String")
		/// <summary>
		/// ${field.alias}
		/// </summary>
		public string#if(${field.isRequired}==false)?#end ${field}
		{
			set{ _${field}=value;}
			get{return _${field};}
		}
#end
#if(${field.type}=="Date")
		/// <summary>
		/// ${field.alias}
		/// </summary>
		public DateTime#if(${field.isRequired}==false)?#end ${field}
		{
			set{ _${field}=value;}
			get{return _${field};}
		}
#end
#if(${field.type}=="Integer")
		/// <summary>
		/// ${field.alias}
		/// </summary>
		public int#if(${field.isRequired}==false)?#end ${field}
		{
			set{ _${field}=value;}
			get{return _${field};}
		}
#end
#if(${field.type}=="Boolean")
		/// <summary>
		/// ${field.alias}
		/// </summary>
		public bool ${field}
		{
			set{ _${field}=value;}
			get{return _${field};}
		}
#end
#end
		#endregion Model

	}
}
生成的C#文件(student.cs)

using System;
namespace com.tgb.lk.Model
{
	/// <summary>
	/// student:实体类(属性说明自动提取数据库字段的描述信息)
	/// </summary>
	[Serializable]
	public partial class student
	{
		public student()
		{}
		#region Model
		private string _id;
		private string _name;
		private string? _sex;
		private int? _age;
		private DateTime? _birthday;
		private string? _clazzId;
		private bool? _isMonitor;
		private DateTime _createTime;
		private DateTime _updateTime;
		/// <summary>
		/// 序号
		/// </summary>
		public string id
		{
			set{ _id=value;}
			get{return _id;}
		}
		/// <summary>
		/// 姓名
		/// </summary>
		public string name
		{
			set{ _name=value;}
			get{return _name;}
		}
		/// <summary>
		/// 性别
		/// </summary>
		public string? sex
		{
			set{ _sex=value;}
			get{return _sex;}
		}
		/// <summary>
		/// 年龄
		/// </summary>
		public int? age
		{
			set{ _age=value;}
			get{return _age;}
		}
		/// <summary>
		/// 生日
		/// </summary>
		public DateTime? birthday
		{
			set{ _birthday=value;}
			get{return _birthday;}
		}
		/// <summary>
		/// 班级ID
		/// </summary>
		public string? clazzId
		{
			set{ _clazzId=value;}
			get{return _clazzId;}
		}
		/// <summary>
		/// 是否班长
		/// </summary>
		public bool isMonitor
		{
			set{ _isMonitor=value;}
			get{return _isMonitor;}
		}
		/// <summary>
		/// 创建
		/// </summary>
		public DateTime createTime
		{
			set{ _createTime=value;}
			get{return _createTime;}
		}
		/// <summary>
		/// 修改时间
		/// </summary>
		public DateTime updateTime
		{
			set{ _updateTime=value;}
			get{return _updateTime;}
		}
		#endregion Model

	}
}

(3) xml模板(spring-service.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

#foreach ($bean in $beans)
	<bean id="${bean}Service" class="${base-package}.service.impl.${bean.Uname}ServiceImpl"
		parent="baseHibernateService">
		<property name="${bean}Dao" ref="${bean}Dao" />
	</bean>

#end
</beans>

生成的文件(spring-service.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="studentService" class="com.tgb.lk.service.impl.StudentServiceImpl"
		parent="baseHibernateService">
		<property name="studentDao" ref="studentDao" />
	</bean>

</beans>

(4) jsp模板:

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@include file="/WEB-INF/jsp/common/common.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>添加${bean.alias}</title>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

		<style type="text/css">

</style>
	</head>
	<body>
		<form id="form1" method="post" action="${bean}_add">
			<input name="id" class="mini-hidden" />
			<div class="divFormContent">
				<table style="table-layout: fixed;">
				#set($count=0) 
				#foreach ($field in ${bean.fields})
					#if($count%2==0)
					<tr>
					#end
						<td class="tdLabel">
							${field.alias}:
						</td>
					#if(${field.type} == "Date")
						<td class="tdInput">
							<input id="${bean}.${field}" name="${bean}.${field}" 
								class="mini-datepicker" required="${field.isRequired}" />
						</td>
					#elseif(${field.type} == "Integer" || ${field.type} == "int")
							<td class="tdInput">
								<input id="${bean}.${field}" name="${bean}.${field}" class="mini-spinner" 
									vtype="int" minValue="0" maxValue="200" required="${field.isRequired}" />
							</td>
					#elseif(${field.type} == "Boolean" || ${field.type} == "boolean")
							<td class="tdInput">
								<select id="${bean}.${field}" name="${bean}.${field}" 
									class="mini-radiobuttonlist" value="false"/>
									<option value="true">
										是
									</option>
									<option value="false">
										否
									</option>
								</select>
							</td>	
					#else
						#if(${field.comboLength} >0 )
							<td class="tdInput">
								<select id="${bean}.${field}" name="${bean}.${field}" class="mini-combobox">
								#if(${field.isRequired} != true)
									<option value="">
										未知
									</option>
								#end
								#foreach ($str in ${field.combo})
									<option value="${str}">
										${str}
									</option>
								#end
								</select>
							</td>
						#else
							<td class="tdInput">
								<input id="${bean}.${field}" name="${bean}.${field}" 
									class="mini-textbox" required="${field.isRequired}" />
							</td>
						#end
					#end
					#if($count%2!=0)
						</tr>
					#end
				#set($count = $count + 1) 
				#end 
				
				#if($count%2!=0)
						<td class="tdLabel"></td>
						<td class="tdInput"></td>
					</tr>
				#end

				</table>
			</div>
			<div class="divBtn">
				<a class="mini-button btnOK" onclick="onOk">确定</a>
				<a class="mini-button btnCancel" onclick="onCancel">取消</a>
			</div>
		</form>
		<%@include file="/WEB-INF/jsp/common/pub_part_add.jsp"%>
	</body>
</html>

生成的jsp代码(student-add.jsp):

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@include file="/WEB-INF/jsp/common/common.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>添加Student</title>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

		<style type="text/css">

</style>
	</head>
	<body>
		<form id="form1" method="post" action="student_add">
			<input name="id" class="mini-hidden" />
			<div class="divFormContent">
				<table style="table-layout: fixed;">
																		<tr>
											<td class="tdLabel">
							序号:
						</td>
																		<td class="tdInput">
								<input id="student.id" name="student.id" 
									class="mini-textbox" required="true" />
							</td>
																															<td class="tdLabel">
							姓名:
						</td>
																		<td class="tdInput">
								<input id="student.name" name="student.name" 
									class="mini-textbox" required="true" />
							</td>
																						</tr>
																			<tr>
											<td class="tdLabel">
							性别:
						</td>
																		<td class="tdInput">
								<select id="student.sex" name="student.sex" class="mini-combobox">
																	<option value="">
										未知
									</option>
																									<option value="男">
										男
									</option>
																	<option value="女">
										女
									</option>
																</select>
							</td>
																															<td class="tdLabel">
							年龄:
						</td>
												<td class="tdInput">
								<input id="student.age" name="student.age" class="mini-spinner" 
									vtype="int" minValue="0" maxValue="200" required="false" />
							</td>
																</tr>
																			<tr>
											<td class="tdLabel">
							生日:
						</td>
											<td class="tdInput">
							<input id="student.birthday" name="student.birthday" 
								class="mini-datepicker" required="false" />
						</td>
																									<td class="tdLabel">
							班级ID:
						</td>
																		<td class="tdInput">
								<input id="student.clazzId" name="student.clazzId" 
									class="mini-textbox" required="false" />
							</td>
																						</tr>
																			<tr>
											<td class="tdLabel">
							是否班长:
						</td>
												<td class="tdInput">
								<select id="student.isMonitor" name="student.isMonitor" 
									class="mini-radiobuttonlist" value="false"/>
									<option value="true">
										是
									</option>
									<option value="false">
										否
									</option>
								</select>
							</td>	
																									<td class="tdLabel">
							创建:
						</td>
											<td class="tdInput">
							<input id="student.createTime" name="student.createTime" 
								class="mini-datepicker" required="true" />
						</td>
																</tr>
																			<tr>
											<td class="tdLabel">
							修改时间:
						</td>
											<td class="tdInput">
							<input id="student.updateTime" name="student.updateTime" 
								class="mini-datepicker" required="true" />
						</td>
																		
										<td class="tdLabel"></td>
						<td class="tdInput"></td>
					</tr>
				
				</table>
			</div>
			<div class="divBtn">
				<a class="mini-button btnOK" onclick="onOk">确定</a>
				<a class="mini-button btnCancel" onclick="onCancel">取消</a>
			</div>
		</form>
		<%@include file="/WEB-INF/jsp/common/pub_part_add.jsp"%>
	</body>
</html>

(5) aspx模板(Add.aspx):
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeBehind="Add.aspx.cs" Inherits="${base-package}.Web.${bean}.Add" Title="增加页" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">    
    <table style="width: 100%;" cellpadding="2" cellspacing="1" class="border">
        <tr>
            <td class="tdbg">
                
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
#foreach ($field in ${bean.fields})
#if(${field.type} == "Date")
	<tr>
	<td height="25" width="30%" align="right">
		${field.alias}
	:</td>
	<td height="25" width="*" align="left">
		<asp:TextBox ID="txt${field}" runat="server" Width="70px"  onfocus="setday(this)"></asp:TextBox>
	</td></tr>
#else
	<tr>
	<td height="25" width="30%" align="right">
		${field.alias}
	:</td>
	<td height="25" width="*" align="left">
		<asp:TextBox id="txt${field}" runat="server" Width="200px"></asp:TextBox>
	</td></tr>
#end
#end
</table>
<script src="/js/calendar1.js" type="text/javascript"></script>

            </td>
        </tr>
        <tr>
            <td class="tdbg" align="center" valign="bottom">
                <asp:Button ID="btnSave" runat="server" Text="保存"
                    OnClick="btnSave_Click" class="inputbutton" onmouseover="this.className='inputbutton_hover'"
                    onmouseout="this.className='inputbutton'"></asp:Button>
                <asp:Button ID="btnCancle" runat="server" Text="取消"
                    OnClick="btnCancle_Click" class="inputbutton" onmouseover="this.className='inputbutton_hover'"
                    onmouseout="this.className='inputbutton'"></asp:Button>
            </td>
        </tr>
    </table>
    <br />
</asp:Content>
<%--<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceCheckright" runat="server">
</asp:Content>--%>

生成的文件:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeBehind="Add.aspx.cs" Inherits="com.tgb.lk.Web.student.Add" Title="增加页" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">    
    <table style="width: 100%;" cellpadding="2" cellspacing="1" class="border">
        <tr>
            <td class="tdbg">
                
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
	<tr>
	<td height="25" width="30%" align="right">
		序号
	:</td>
	<td height="25" width="*" align="left">
		<asp:TextBox id="txtid" runat="server" Width="200px"></asp:TextBox>
	</td></tr>
	<tr>
	<td height="25" width="30%" align="right">
		姓名
	:</td>
	<td height="25" width="*" align="left">
		<asp:TextBox id="txtname" runat="server" Width="200px"></asp:TextBox>
	</td></tr>
	<tr>
	<td height="25" width="30%" align="right">
		性别
	:</td>
	<td height="25" width="*" align="left">
		<asp:TextBox id="txtsex" runat="server" Width="200px"></asp:TextBox>
	</td></tr>
	<tr>
	<td height="25" width="30%" align="right">
		年龄
	:</td>
	<td height="25" width="*" align="left">
		<asp:TextBox id="txtage" runat="server" Width="200px"></asp:TextBox>
	</td></tr>
	<tr>
	<td height="25" width="30%" align="right">
		生日
	:</td>
	<td height="25" width="*" align="left">
		<asp:TextBox ID="txtbirthday" runat="server" Width="70px"  onfocus="setday(this)"></asp:TextBox>
	</td></tr>
	<tr>
	<td height="25" width="30%" align="right">
		班级ID
	:</td>
	<td height="25" width="*" align="left">
		<asp:TextBox id="txtclazzId" runat="server" Width="200px"></asp:TextBox>
	</td></tr>
	<tr>
	<td height="25" width="30%" align="right">
		是否班长
	:</td>
	<td height="25" width="*" align="left">
		<asp:TextBox id="txtisMonitor" runat="server" Width="200px"></asp:TextBox>
	</td></tr>
	<tr>
	<td height="25" width="30%" align="right">
		创建
	:</td>
	<td height="25" width="*" align="left">
		<asp:TextBox ID="txtcreateTime" runat="server" Width="70px"  onfocus="setday(this)"></asp:TextBox>
	</td></tr>
	<tr>
	<td height="25" width="30%" align="right">
		修改时间
	:</td>
	<td height="25" width="*" align="left">
		<asp:TextBox ID="txtupdateTime" runat="server" Width="70px"  onfocus="setday(this)"></asp:TextBox>
	</td></tr>
</table>
<script src="/js/calendar1.js" type="text/javascript"></script>

            </td>
        </tr>
        <tr>
            <td class="tdbg" align="center" valign="bottom">
                <asp:Button ID="btnSave" runat="server" Text="保存"
                    OnClick="btnSave_Click" class="inputbutton" onmouseover="this.className='inputbutton_hover'"
                    onmouseout="this.className='inputbutton'"></asp:Button>
                <asp:Button ID="btnCancle" runat="server" Text="取消"
                    OnClick="btnCancle_Click" class="inputbutton" onmouseover="this.className='inputbutton_hover'"
                    onmouseout="this.className='inputbutton'"></asp:Button>
            </td>
        </tr>
    </table>
    <br />
</asp:Content>
<%--<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceCheckright" runat="server">
</asp:Content>--%>



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics