一、描述

去年三个月全部都是手写基础界面,经过一段时间发现基础资料是有规律可循的,实体类,接口层,接口实现层,服务层,界面全都是有共同之处,命名规则也与数据结构保持一致,项目经理也有命名的要求,就是要保证命名一致,正好与我的代码生成出来的结构以及命名一致符合。做这个代码生成器的好处就是可以一次性生成功能的所有基础类,而且不需要修改任何东西就可以完全具备增删改查的功能,而且没有任何错误!
首先这个工具分为模块进行,如果只想生成基本数据操作三层也是可以的。而且也可以利用其中一部分的代码提高开发速度,以及质量。毕竟window层定义了开发规范,这样不会导致一个团队写出五花八门的代码,为什么不一起解决一个难点,而是把时间花在本身我们已经解决的问题上,这样是不划算的。让我专注服务层的代码编写,省去开发人员写重乎代码的时间。开发程序不是流水线,我们不做重复性的工作。我们需要站在一个更高的角度写代码,眼光要放长远,,拉拉拉之类的。

二、需求分析

已知xml文件以及实体bean和dao,service层,都是一样,而且只要具备增删改查方法就行,基本增删改查hib集成,只需要写统计以及分页方法。根据写好的window层规范代码。做字符拼接就行,没有很难的算法,就是控制表格的行列注意好就行。
分析基础资料的表结构:通过sql语句就可以搜索到表结构。

select table_name,column_name,data_type,column_key,column_comment from information_schema.columns where TABLE_SCHEMA

可以看到全是字符varchar 以及主键用PRI标识,其它五个字段也都一样。

所以最终只要拼接字符串代码即可。

三、成果展示

界面显示效果如下:

查询

增加

1.实体类代码展示

package com.jyd.bms.bean;

import javax.persistence.Entity;
import java.io.Serializable;
import java.sql.Timestamp;

/**
 * @category 自动生成基础类
 * @author mjy
 */
@Entity
public class BaBusinessSource implements Serializable {
	private int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	private String businessSource;

	// 业务来源
	public String getBusinessSource() {
		return businessSource;
	}

	public void setBusinessSource(String businessSource) {
		this.businessSource = businessSource;
	}

	private String remark;

	// 备注
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	private Timestamp createDate;

	// 创建时间
	public Timestamp getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Timestamp createDate) {
		this.createDate = createDate;
	}

	private Timestamp updateDate;

	// 修改时间
	public Timestamp getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Timestamp updateDate) {
		this.updateDate = updateDate;
	}

	private String createUser;

	// 创建用户
	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	private String updateUser;

	// 修改用户
	public String getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}

}

2.xml文件展示

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-03-18 00:25:40 by GeneratedTool -->
<hibernate-mapping>
	<class name="com.jyd.bms.bean.BaBusinessSource" table="ba_business_source">
		<id name="id" type="int">
			<column name="business_source_id" />
			<generator class="identity" />
		</id>
		<property name="businessSource" type="java.lang.String">
			<column name="business_source" />
		</property>
		<property name="remark" type="java.lang.String">
			<column name="remark" />
		</property>
		<property name="createDate" type="java.sql.Timestamp">
			<column name="create_date" />
		</property>
		<property name="updateDate" type="java.sql.Timestamp">
			<column name="update_date" />
		</property>
		<property name="createUser" type="java.lang.String">
			<column name="create_user" />
		</property>
		<property name="updateUser" type="java.lang.String">
			<column name="update_user" />
		</property>
	</class>
</hibernate-mapping>

3.dao层代码展示

package com.jyd.bms.dao;

import java.util.List;

import com.jyd.bms.bean.BaBusinessSource;
import com.jyd.bms.tool.exception.DAOException;

/**
 * @category 自动生成基础DAO
 * @author mjy
 */
public interface BaBusinessSourceDAO extends HibernateBase<BaBusinessSource> {
	public int getBaBusinessSourceCount(String condition) throws DAOException;

	public List<BaBusinessSource> getPagingBaBusinessSource(int firstResult, int maxResults, String condition)
			throws DAOException;

	public List<BaBusinessSource> getAllBaBusinessSource() throws DAOException;
}

4.service层代码生成

package com.jyd.bms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jyd.bms.bean.BaBusinessSource;
import com.jyd.bms.dao.BaBusinessSourceDAO;
import com.jyd.bms.tool.exception.DAOException;

/**
 * @category 自动生成基础service
 * @author mjy
 */
@Service("BaBusinessSourceService")
public class BaBusinessSourceService extends BaseService<BaBusinessSource> {
	@Autowired(required = true)
	private BaBusinessSourceDAO baBusinessSourceDAO;

	public int getBaBusinessSourceCount(String condition) throws DAOException {
		return baBusinessSourceDAO.getBaBusinessSourceCount(condition);
	}

	public List<BaBusinessSource> getPagingBaBusinessSource(int firstResult, int maxResults, String condition)
			throws DAOException {
		return baBusinessSourceDAO.getPagingBaBusinessSource(firstResult, maxResults, condition);
	}

	public List<BaBusinessSource> getAllBaBusinessSource() throws DAOException {
		return baBusinessSourceDAO.getAllBaBusinessSource();
	}

	@Override

	public void setDAO() {
		this.baseDAO = baBusinessSourceDAO;
	}
}

5.window层代码展示

此处代码有点多:

window主要分为界面文本框以及输入框控制方法enableTextbox,以及按钮控制方法enableButton,以及增删改查按钮监听事件,分页查修等等

package com.jyd.bms.window.basedata;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.South;
import org.zkoss.zul.Textbox;

import com.jyd.bms.bean.BaBusinessSource;
import com.jyd.bms.bean.User;
import com.jyd.bms.service.BaBusinessSourceService;
import com.jyd.bms.tool.exception.CreateException;
import com.jyd.bms.tool.exception.DAOException;
import com.jyd.bms.tool.exception.UpdateException;
import com.jyd.bms.tool.zk.BaseWindow;
import com.jyd.bms.tool.zk.GridPaging;
import com.jyd.bms.tool.zk.Listbox;
import com.jyd.bms.tool.zk.Messagebox;
import com.jyd.bms.tool.zk.PagingControlComponentModelList;
import com.jyd.bms.tool.zk.UserSession;

/**
 * @category Generated 2018-03-18 13:42:31 by GeneratedTool
 * @author mjy
 */

public class BaBusinessSourceWindow extends BaseWindow {
	private Button addButton;
	private Button editButton;
	private Button cancelButton;
	private Button saveButton;
	private GridPaging gridPaging;
	private Textbox conditionTextbox;
	private String condition = "";
	private South southPaging;
	private Label businessSourceLabel;
	private Textbox businessSourceTextbox;
	private Label remarkLabel;
	private Textbox remarkTextbox;
	private Listbox baBusinessSourceListbox;
	private BaBusinessSource baBusinessSource;
	private BaBusinessSourceService baBusinessSourceService;
	private List<BaBusinessSource> baBusinessSourcelist = new ArrayList<BaBusinessSource>();
	private static final Logger log = LoggerFactory.getLogger(BaBusinessSourceWindow.class);
	private int edit = 0;

	public BaBusinessSourceWindow() {
		this.menuId = "ba_business_source";
	}

	public Listitem getSelectItem() {
		return baBusinessSourceListbox.getSelectedItem();
	}

	public void initUI() {
		baBusinessSourceService = getBean("BaBusinessSourceService");
		baBusinessSourceListbox.setItemRenderer(new BaBusinessSourceRenderer());
	}

	@Override
	public void initData() {
		try {
			PagingControlComponentModelList pagingModelList = new PagingControlComponentModelList(
					baBusinessSourceService, "getPagingBaBusinessSource", new Object[] { condition });
			if (gridPaging == null) {
				gridPaging = new GridPaging(baBusinessSourceListbox, pagingModelList, 20);
			} else {
				gridPaging.setPagingControlComponentModel(pagingModelList, 20);
			}
			gridPaging.setTotalSize(baBusinessSourceService.getBaBusinessSourceCount(condition));
			gridPaging.setDetailed(true);
			gridPaging.setParent(southPaging);
		} catch (DAOException e) {
			log.error("BaBusinessSourceWindow", e);
			Messagebox.error("获取数据出错了!");
		} catch (Exception e) {
			log.error("AssetsTypeWindow", e);
			Messagebox.error("未知错误!");
		}
	}

	public void onClick$searchButton() throws SuspendNotAllowedException, InterruptedException {
		condition = conditionTextbox.getValue();
		initData();
	}

	public void onOKsearchButton() throws SuspendNotAllowedException, InterruptedException {
		onClick$searchButton();
	}

	public void enableTextbox(boolean flag) {
		businessSourceLabel.setVisible(!flag);
		businessSourceTextbox.setVisible(flag);
		remarkLabel.setVisible(!flag);
		remarkTextbox.setVisible(flag);
	}

	public void enableButton(String type) {
		if (type.equals("add")) {
			addButton.setVisible(false);
			editButton.setVisible(false);
			saveButton.setVisible(true);
			cancelButton.setVisible(true);
		}
		if (type.equals("update")) {
			addButton.setVisible(false);
			editButton.setVisible(false);
			saveButton.setVisible(true);
			cancelButton.setVisible(true);
		}
		if (type.equals("cancel")) {
			addButton.setVisible(true);
			editButton.setVisible(false);
			saveButton.setVisible(false);
			cancelButton.setVisible(false);
		}
		if (type.equals("save")) {
			addButton.setVisible(true);
			editButton.setVisible(false);
			saveButton.setVisible(false);
			cancelButton.setVisible(false);
		}
		if (type.equals("select")) {
			addButton.setVisible(true);
			editButton.setVisible(true);
			saveButton.setVisible(false);
			cancelButton.setVisible(false);
		}
	}

	public void clearTextbox() {
		businessSourceLabel.setValue("");
		businessSourceTextbox.setValue("");
		remarkLabel.setValue("");
		remarkTextbox.setValue("");
	}

	public void setBaBusinessSourceData(BaBusinessSource baBusinessSource) {
		baBusinessSource.setBusinessSource(businessSourceTextbox.getValue());
		baBusinessSource.setRemark(remarkTextbox.getValue());
	}

	public void setBaBusinessSourceValue(BaBusinessSource baBusinessSource) {
		businessSourceLabel.setValue(baBusinessSource.getBusinessSource());
		businessSourceTextbox.setValue(baBusinessSource.getBusinessSource());
		remarkLabel.setValue(baBusinessSource.getRemark());
		remarkTextbox.setValue(baBusinessSource.getRemark());
	}

	public void onSelect$baBusinessSourceListbox() throws SuspendNotAllowedException, InterruptedException {
		edit = -1;
		baBusinessSource = getSelectItem().getValue();
		clearTextbox();
		setBaBusinessSourceData(baBusinessSource);
		enableTextbox(false);
		enableButton("select");
	}

	public void onClick$cancelButton() {
		enableButton("cancel");
		enableTextbox(false);
		clearTextbox();
	}

	public void onClick$addButton() {
		edit = 0;
		enableTextbox(true);
		enableButton("add");
		clearTextbox();
	}

	public void onClick$editButton() {
		edit = -1;
		enableTextbox(true);
		enableButton("update");
	}

	public boolean checkInput() {
		boolean flag = true;
		if (businessSourceTextbox.getValue().equals("")) {
			businessSourceTextbox.focus();
			Messagebox.show("业务来源不能为空!");
			flag = false;
		}
		if (remarkTextbox.getValue().equals("")) {
			remarkTextbox.focus();
			Messagebox.show("备注不能为空!");
			flag = false;
		}
		return flag;
	}

	public void onClick$saveButton() {
		try {
			User sessionUser = UserSession.getUser();
			if (sessionUser == null) {
				Messagebox.show("长时间未操作,出于安全考虑,请重新登陆!");
				return;
			}
			Timestamp date = new Timestamp(System.currentTimeMillis());
			String user = sessionUser.getLoginName();
			if (!checkInput()) {
				return;
			}
			if (edit == 0) {
				baBusinessSource = new BaBusinessSource();
				setBaBusinessSourceValue(baBusinessSource);
				baBusinessSource.setCreateDate(date);
				baBusinessSource.setCreateUser(user);
				baBusinessSource.setUpdateDate(date);
				baBusinessSource.setUpdateUser(user);
				baBusinessSourceService.add(baBusinessSource);
			} else {
				setBaBusinessSourceValue(baBusinessSource);
				baBusinessSource.setUpdateDate(date);
				baBusinessSource.setUpdateUser(user);
				baBusinessSourceService.update(baBusinessSource);
			}
		} catch (CreateException e) {
			log.error("baBusinessSourceWindow", e);
		} catch (UpdateException e) {
			log.error("baBusinessSourceWindow", e);
		}
	}

	class BaBusinessSourceRenderer implements ListitemRenderer {
		public void render(Listitem arg0, Object arg1, int arg2) throws Exception {
			BaBusinessSource baBusinessSource = (BaBusinessSource) arg1;
			Listcell businessSourceCell = new Listcell();
			businessSourceCell.setParent(arg0);
			new Label(baBusinessSource.getBusinessSource()).setParent(businessSourceCell);
			Listcell remarkCell = new Listcell();
			remarkCell.setParent(arg0);
			new Label(baBusinessSource.getRemark()).setParent(remarkCell);
			arg0.setValue(baBusinessSource);
		}
	}
}

6.以及页面zul代码

<?page contentType="text/html;charset=UTF-8"?>
<zk xmlns:h="xhtml" xmlns:zk="zk">
	<window border="normal" width="100%" height="100%"
		use="com.jyd.bms.window.basedata.BaBusinessSourceWindow">
		<script src="/script/jquery-1.4.1.js" type="text/javascript" />
		<script src="/script/enter.js" type="text/javascript" />
		<borderlayout width="100%" height="100%">
			<center>
				<borderlayout width="100%" height="100%">
					<north style="background:#F0FAFF">
						<hbox pack="right" width="100%">
							<hbox>
								<textbox id="conditionTextbox"
									width="200px" />
								<button label="查询" id="searchButton" />
							</hbox>
						</hbox>
					</north>
					<center flex="true">
						<listbox id="baBusinessSourceListbox"
							vflex="true" fixedLayout="true">
							<listhead sizable="true">
								<listheader label="业务来源" />
								<listheader label="备注" />
								<listheader label="创建时间" />
								<listheader label="修改时间" />
								<listheader label="创建用户" />
								<listheader label="修改用户" />
							</listhead>
						</listbox>
					</center>
					<south id="southPaging"></south>
				</borderlayout>
			</center>
			<south height="60px" splittable="true">
				<borderlayout>
					<center flex="true">
						<h:table width="100%" cellpadding="1"
							cellspacing="1" class="tableBorderDark">
							<h:tr>
								<h:td width="5%"
									class="tdEvennoBorder">
									<hbox>
										<label value="业务来源" />
									</hbox>
								</h:td>
								<h:td width="45%"
									class="tdOddnoBorder">
									<label value=""
										id="businessSourceLabel" visible="true" />
									<textbox id="businessSourceTextbox"
										visible="false" />
								</h:td>
								<h:td width="5%"
									class="tdEvennoBorder">
									<hbox>
										<label value="备注" />
									</hbox>
								</h:td>
								<h:td width="45%"
									class="tdOddnoBorder">
									<label value="" id="remarkLabel"
										visible="true" />
									<textbox id="remarkTextbox"
										visible="false" />
								</h:td>
							</h:tr>
						</h:table>
					</center>
					<south>
						<hbox pack="center" width="100%">
							<hbox align="center">
								<button id="addButton" label="增加"
									visible="true" />
								<button id="editButton" label="修改"
									visible="false" />
								<button id="saveButton" label="保存"
									visible="false" />
								<button id="cancelButton" label="取消"
									visible="false" />
							</hbox>
						</hbox>
					</south>
				</borderlayout>
			</south>
		</borderlayout>
	</window>
</zk>

 

完整的一个带搜索,增加,修改,保存。的界面就出来了。

代码分享:yemao